Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3353694
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:10:56+00:00 2026-05-18T02:10:56+00:00

I have difficulties understanding a problem I encountered during instantiation of a QT class

  • 0

I have difficulties understanding a problem I encountered during instantiation of a QT class but I have the feeling that it might be general oop thing. The problem was that it only worked after I used a pointer to the object but didn’t work with just the object variable itself.

In main() I create an instance of a widget:

Board board;
board.show();

Board.h:

class Board : public QWidget
{

    Q_OBJECT

public:
    Board(QWidget* parent = 0);
    virtual ~Board();
};

Board.cpp

Board::Board(QWidget* parent) :
    QWidget(parent)
{

    QGraphicsScene* boardScene = new QGraphicsScene(this);
    boardScene->setSceneRect(this->rect());

    QGraphicsItem* item2 = new QGraphicsPixmapItem(QPixmap("test.jpg"));
    item2->setPos(100,100);
    boardScene->addItem(item2);


    QGraphicsView boardView (boardScene, this);

Now the problem was in the last line. The test picture (item2) was only shown after I changed the last line so that it was a pointer:

QGraphicsView* boardView = new QGraphicsView (boardScene, this);

Why doesn’t the object variable work? Is this due to some internal QT thing or am I missing something? I also painted the background of boardScene and I saw the color, so I know it was still “alive”.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T02:10:57+00:00Added an answer on May 18, 2026 at 2:10 am

    Because in C++, stack variables are automatically destructed when they go out of scope. In your case, boardView will be destructed when the Board constructor returns, so the test picture doesn’t show at all (since boardView doesn’t exist anymore).

    On the other hand, the lifetime of heap-allocated objects are not bound to the scope they were created in, so it stays alive even after the constructor returns. That’s why the test picture was shown only with your second code snippet.

    Note that QGraphicsView (indirectly) inherits from the QWidget class which in turn inherits from the QObject class. Since QObjects form object trees, boardView will be properly destructed when the parent is destroyed as well, so you don’t need to worry about destroying it once you’re done with it (of course, you still have to worry about destroying the root of the object tree).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having difficulties understanding bzr init-repo . I have 3 projects that I want
I am reading the book: Intel Threading Building Blocks. I often have difficulties understanding
I have some difficulties with understanding BindingSource's behaviour. Let's look at following example: Creating
I know this question had been asked a hundred times, but I have difficulties
I have a data set of item difficulties that correspond to items on a
I have the following regex in a C# program, and have difficulties understanding it:
I have some difficulties understanding how the access method Do not move cursor automatically
could someone explain the code in catch_exceptions? i have difficulties to understand. thanks class
I've spent a day trying to a simple compiler program but still have difficulties
I have difficulties in understanding why we get the output of this code: <?php

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.