Results 1 to 6 of 6

Thread: QLayout and QMainWindow

  1. #1
    Join Date
    Oct 2015
    Posts
    10
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QLayout and QMainWindow

    Hello. I recently started to work with Qt and Qwt. I can't find my mistake by myself. Please help me. There is the code:
    I know that my mistakes in somewhere here:

    .h file
    Qt Code:
    1. ...
    2. class MainWindow : public
    3. {
    4. Q_OBJECT
    5. QWidget *centralWidget;
    6. public:
    7. MainWindow(QWidget *parent = 0);
    8. ...
    To copy to clipboard, switch view to plain text mode 
    .cpp

    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent):
    2. QMainWindow(parent) {
    3. ...
    4. ...
    5. void MainWindow::setPlotButton() {
    6. button = new QPushButton("push"),
    7. button->setCheckable(true);
    8.  
    9. connect(button, SIGNAL(toggled(bool)), this, SLOT(toggled(bool)));
    10.  
    11. QHBoxLayout *plotsLayout = new QHBoxLayout;
    12. plotsLayout->setSpacing(10);
    13. plotsLayout->addWidget(funPlot);
    14.  
    15. QHBoxLayout *buttonsLayout = new QHBoxLayout ;
    16. buttonsLayout->addWidget(button);
    17.  
    18. QVBoxLayout *widgetLayout = new QVBoxLayout;
    19. widgetLayout->addLayout(plotsLayout);
    20. widgetLayout->addLayout(buttonsLayout);
    21.  
    22. setLayout(widgetLayout);
    23. ...
    24. }
    To copy to clipboard, switch view to plain text mode 

    I recieve a message "QWidget::setLayout: Attempting to set QLayout "" on MainWindow "", which already has a layout". What I should change?
    Last edited by Cat; 19th October 2015 at 13:04.

  2. #2
    Join Date
    Jan 2011
    Posts
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QLayout and QMainWindow

    Use setCentralWidget function to add centralWidget.




    ---sory about bad english.

  3. #3
    Join Date
    Oct 2015
    Posts
    10
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QLayout and QMainWindow

    Do you mean to use setCentralWidget() instead of setLayout(widgetLayout)?

  4. #4
    Join Date
    Oct 2015
    Posts
    10
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QLayout and QMainWindow

    Please, can you explain it in more detail?

  5. #5
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QLayout and QMainWindow

    This is not how QMainWindow is meant to be used. You need to put all the widgets inside a "container" widget, and set this container as the QMainWindow's central widget. For instance:
    Qt Code:
    1. QWidget *centralWidget = new QWidget(this /* the main window */);
    2. QVBoxLayout *widgetLayout = new QVBoxLayout(centralWidget);
    3. QHBoxLayout *buttonsLayout = new QHBoxLayout;
    4. button = new QPushButton("push", centralWidget);
    5. buttonsLayout->addWidget(button);
    6. widgetLayout->addLayout(buttonsLayout);
    7. // ...
    8. setCentralWidget(centralWidget);
    To copy to clipboard, switch view to plain text mode 
    IMHO Qt's API for managing layouts, widgets, containment and ownership is messy and counter-intuitive. Whenever I need to do this by hand, I just do a mock up with Designer and have a look at the generated code.

  6. #6
    Join Date
    Jan 2011
    Posts
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QLayout and QMainWindow

    yes, setCentralWideget() add widget in mainwindow; setLayout add layout for mainwindow. they are different.

    --sory about bad english.

Similar Threads

  1. Replies: 3
    Last Post: 13th November 2011, 09:12
  2. Replies: 2
    Last Post: 29th June 2011, 16:45
  3. Replies: 0
    Last Post: 17th November 2010, 18:07
  4. QLayout
    By sudeepdua in forum Qt Programming
    Replies: 2
    Last Post: 21st November 2006, 11:46
  5. A bug of QLayout
    By cocalele in forum Qt Programming
    Replies: 6
    Last Post: 26th April 2006, 07:10

Tags for this Thread

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.