Results 1 to 2 of 2

Thread: Newbie - Confused about the central widget

  1. #1
    Join Date
    Aug 2009
    Location
    United States
    Posts
    45
    Thanks
    20
    Qt products
    Qt4
    Platforms
    Windows

    Default Newbie - Confused about the central widget

    Hi,

    I've been reading through the GUI programming with Qt4 and really need help understanding the central widget. Here's my code:


    Qt Code:
    1. #include <QMainWindow>
    2. #include <QtGui>
    3.  
    4. class MainWindow : public QMainWindow
    5. {
    6. public:
    7. MainWindow(); // Constructor
    8. };
    9.  
    10.  
    11. MainWindow::MainWindow()
    12. {
    13. QSpinBox *spinBox = new QSpinBox;
    14. QVBoxLayout *mainLayout = new QVBoxLayout();
    15. mainLayout->addWidget(spinBox);
    16. this->centralWidget()->setLayout(mainLayout);
    17. }
    18.  
    19. int main(int argc, char *argv[])
    20. {
    21. QApplication app(argc, argv); // Create application. Arguments as parameters
    22. MainWindow *mainWindow = new MainWindow; // Create the application window
    23. mainWindow->show(); // Show the application window
    24. return app.exec();
    25. }
    To copy to clipboard, switch view to plain text mode 

    This is my program simplified, all in one file. Although it compiles just fine, the program will crash upon being run.

    I really have been trying to figure out how to do this correctly and would really appreciate if someone could point in the right direction!

    Thanks

  2. #2
    Join Date
    Nov 2006
    Location
    Saudi Arabia
    Posts
    18
    Thanks
    2
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Newbie - Confused about the central widget

    you should first set the central widget because this call
    this->centralWidget()

    will return 0.

    here the fix:

    Qt Code:
    1. MainWindow::MainWindow()
    2. {
    3. QWidget* widget = new QWidget; // parent widget
    4. QSpinBox *spinBox = new QSpinBox;
    5. //... use any widget you want.
    6.  
    7. // set the layout
    8. QVBoxLayout *mainLayout = new QVBoxLayout(widget);
    9. mainLayout->addWidget(spinBox);
    10.  
    11. setCentralWidget(widget);
    12. }
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to SudaNix for this useful post:

    N3wb (20th September 2009)

Similar Threads

  1. Replies: 3
    Last Post: 19th February 2012, 11:40
  2. QDockWidget inside another widget in the center?
    By Antebios in forum Qt Programming
    Replies: 1
    Last Post: 16th February 2010, 07:06
  3. Replies: 2
    Last Post: 23rd March 2009, 17:26
  4. Replies: 2
    Last Post: 7th June 2008, 13:12
  5. Central Widget of QMainWindow
    By sumsin in forum Qt Programming
    Replies: 3
    Last Post: 13th March 2006, 18:32

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.