Results 1 to 14 of 14

Thread: How to put 2 Widgets in a MainWindow ?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Unhappy Re: How to put 2 Widgets in a MainWindow ?

    For a start you need to set the QMainWindow's central widget... there can be only one. To get more than one widget into the main window central area you need to encapsulate them.

    Qt Code:
    1. #include <QtGui>
    2.  
    3. class mainwindow: public QMainWindow {
    4. public:
    5. mainwindow(QWidget *p = 0): QMainWindow(p) {
    6. QWidget *central = new QWidget(this); // a central widget
    7. QLabel *widget1 = new QLabel("one widget", this);
    8. widget1->setFrameStyle(QFrame::Box | QFrame::Plain);
    9. QLabel *widget2 = new QLabel("two widget", this);
    10. widget2->setFrameStyle(QFrame::Box | QFrame::Plain);
    11.  
    12. QVBoxLayout *layout = new QVBoxLayout(this);
    13. layout->addWidget(widget1);
    14. layout->addWidget(widget2);
    15.  
    16. central->setLayout(layout);
    17. setCentralWidget(central); // you were missing this
    18. }
    19.  
    20. };
    21.  
    22. int main(int argc, char *argv[])
    23. {
    24. QApplication app(argc, argv);
    25.  
    26. mainwindow m;
    27. m.show();
    28.  
    29. return app.exec();
    30. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jun 2010
    Posts
    19
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to put 2 Widgets in a MainWindow ?

    ok, thanks !!
    when I put this code:
    Qt Code:
    1. mainwindow::mainwindow(QWidget*p)
    2. {
    3.  
    4. double *buffer=NULL;
    5.  
    6. plot=new Plot(100,250,buffer,buffer+1,this);
    7. data = new Data(250,buffer,this);
    8.  
    9. QWidget *central = new QWidget(0); // a central widget.
    10.  
    11. QVBoxLayout *layout = new QVBoxLayout(this);
    12. layout->addWidget(plot);
    13. layout->addWidget(data);
    14. central->setLayout(layout);
    15. setCentralWidget(central);
    16.  
    17. data->on_watcher();
    18.  
    19. }
    To copy to clipboard, switch view to plain text mode 

    I have exaclty what I want, but suddently the program shuts down with a code -1073741819 and I still get the error about the QLayout that Zlatomir talked about:

    Qt Code:
    1. Starting C:\Users\Genevieve\Desktop\DETECTION\debug\DETECTION.exe...
    2. QLayout: Attempting to add QLayout "" to mainwindow "", which already has a layout
    3. C:\Users\Genevieve\Desktop\DETECTION\debug\DETECTION.exe exited with code -1073741819
    To copy to clipboard, switch view to plain text mode 

    When I debbuged with Visual it says:
    Unhandled exception at 0x004056bc in DETECTION.exe: 0xC0000005: Access violation reading location 0x00000004.
    Last edited by gen_mass; 30th June 2010 at 09:06.

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to put 2 Widgets in a MainWindow ?

    Make line 11 read:
    Qt Code:
    1. QVBoxLayout *layout = new QVBoxLayout(central);
    To copy to clipboard, switch view to plain text mode 
    and the warning should go away.

    The access violation is probably related to the buffer pointer; using it while it is NULL, accessing the "buffer+1" memory location, or trying to use the storage for the buffer pointer itself after it goes out of scope.

  4. #4
    Join Date
    Jun 2010
    Posts
    19
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to put 2 Widgets in a MainWindow ?

    Ok, eveything works now!!!

    just a last thing.. I want my plot to take 2/3 of the window andmy QTableWidget 1/3, is it possible to that with a QVBoxLayout ?

    Thanks for your help !!!

  5. #5
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to put 2 Widgets in a MainWindow ?

    You can use something like this:
    Qt Code:
    1. QSizePolicy PlotPolicy = plot->sizePolicy();
    2. PlotPolicy.setVerticalStretch(1); //don't remember the exact "formula", try with 1
    3. plot->setSizePolicy(PlotPolicy);
    To copy to clipboard, switch view to plain text mode 
    if that doesn't do the right proportions, try this:
    Qt Code:
    1. QSizePolicy PlotPolicy = plot->sizePolicy();
    2. PlotPolicy.setVerticalStretch(2); //don't remember the exact "formula", so play with this values
    3. plot->setSizePolicy(PlotPolicy);
    4.  
    5. QSizePolicy DataPolicy = data->sizePolicy();
    6. DataPolicy.setVerticalStretch(1);
    7. plot->setSizePolicy(DataPolicy);
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jun 2010
    Posts
    19
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to put 2 Widgets in a MainWindow ?

    Thanks Zlatomir !
    I used your second method and it works well !!!

    A last last question...is it possible to center my QTableWidget with that QVBoxLayout?
    I want to do that because when I resize my window manually with a mouse, the QTableWidget stays align at the left.
    Is there a similar function to
    Qt Code:
    1. QTableWidget::setAlignement(Qt::AlignCenter)
    To copy to clipboard, switch view to plain text mode 
    that exists ?
    I search the documentation, but couldn't find any easy way to do this

Similar Threads

  1. Replies: 5
    Last Post: 18th April 2010, 23:31
  2. resizing widgets when user resizes mainwindow
    By Mystical Groovy in forum Qt Programming
    Replies: 13
    Last Post: 21st December 2009, 00:03
  3. Replies: 2
    Last Post: 31st July 2009, 09:18
  4. Child Widgets In MainWindow
    By RY in forum Newbie
    Replies: 3
    Last Post: 4th October 2008, 08:39
  5. scrolled widgets containers in Mainwindow
    By antonio.r.tome in forum Qt Programming
    Replies: 1
    Last Post: 24th October 2006, 14:40

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
  •  
Qt is a trademark of The Qt Company.