Results 1 to 7 of 7

Thread: QMainWindow: problem changing centralWidget

  1. #1
    Join Date
    Mar 2006
    Posts
    142
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QMainWindow: problem changing centralWidget

    Hello,
    I get a segmentation fault on the setCentralWidget() call in the SetBackground() slot, that is when the QMainWindow attempts to change the central widget. I do know that it tries to delete the widget but do not understand how to cope with that. Please help!

    class myClass : public QMainWindow
    {
    Q_OBJECT

    QImage fond;
    QLabel *wfond;
    };

    myClass::myClass()
    : QMainWindow()
    {
    this->fond.load("somepic.jpg");
    this->wfond = new QLabel;
    this->wfond->setPixmap(QPixmap::fromImage(this->fond));
    this->setCentralWidget(this->wfond);
    }

    void
    myClass::SetBackground(QImage im)
    {
    this->fond = im;
    this->wfond = new QLabel;
    this->wfond->setPixmap(QPixmap::fromImage(this->fond));
    this->setCentralWidget(this->wfond);
    }

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QMainWindow: problem changing centralWidget

    Did you try debugging? What does the backtrace say?

  3. #3
    Join Date
    May 2006
    Location
    Bangalore,India
    Posts
    235
    Thanks
    7
    Thanked 25 Times in 24 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: QMainWindow: problem changing centralWidget

    try to use QImage pointer.
    class myClass : public QMainWindow
    {
    Q_OBJECT

    QImage *fond; //use pointer
    QLabel *wfond;
    };

    myClass::myClass()
    : QMainWindow()
    {
    fond = new QImage; //allocate here.
    this->fond->load("somepic.jpg");
    ...
    }

    why you using this pointer everywhere?

  4. #4
    Join Date
    Mar 2006
    Posts
    142
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QMainWindow: problem changing centralWidget

    Quote Originally Posted by rajesh View Post
    try to use QImage pointer.
    class myClass : public QMainWindow
    {
    Q_OBJECT

    QImage *fond; //use pointer
    QLabel *wfond;
    };

    myClass::myClass()
    : QMainWindow()
    {
    fond = new QImage; //allocate here.
    this->fond->load("somepic.jpg");
    ...
    }

    why you using this pointer everywhere?
    Here is my new code, modified according to your advice:

    class myClass : public QMainWindow
    {
    Q_OBJECT

    QImage *fond;
    QLabel *wfond;
    };

    myClass::myClass()
    : QMainWindow()
    {
    this->fond = new QImage("somepic.jpg");
    this->wfond = new QLabel;
    this->wfond->setPixmap(QPixmap::fromImage(*this->fond));
    this->setCentralWidget(this->wfond);
    }

    void
    myClass::SetBackground(QImage im)
    {
    this->fond = new QImage(im);
    this->wfond = new QLabel;
    this->wfond->setPixmap(QPixmap::fromImage(*this->fond));
    this->setCentralWidget(this->wfond);
    }

    but stil the same segmentation fault on setCentralWidget() !

  5. #5
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QMainWindow: problem changing centralWidget

    Quote Originally Posted by rajesh View Post
    try to use QImage pointer.
    QImage is an implicitly shared class. There is no advantage in allocating it on the heap. It only makes things unnecessarily more complicated.

    Caius Aérobus: Why not just change the pixmap? There is no need to create a new QLabel every time.
    J-P Nurmi

  6. #6
    Join Date
    May 2006
    Location
    Bangalore,India
    Posts
    235
    Thanks
    7
    Thanked 25 Times in 24 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: QMainWindow: problem changing centralWidget

    just for testing , try following code, is it working or not?
    void myClass::SetBackground(QImage im)
    {
    QLabel *center =
    new QLabel("test");
    setCentralWidget(center);
    }

  7. #7
    Join Date
    Mar 2006
    Posts
    142
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QMainWindow: problem changing centralWidget

    Hmmm, I suppose I have fixed the problem. I use Qt 4.1 and I tried it on another system on which Qt 4.3 is installed and... everything works fine! So probably a bug in Qt 4.1. I will upgrade ASAP.
    Thanks to all of you for trying to help me!
    Best regards.

Similar Threads

  1. QMainWindow resize problem
    By edb in forum Qt Programming
    Replies: 5
    Last Post: 12th January 2007, 11:31
  2. Problem closing a QMainWindow in Qt4.2
    By ian in forum Qt Programming
    Replies: 11
    Last Post: 17th October 2006, 01:49

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.