Results 1 to 14 of 14

Thread: How to put 2 Widgets in a MainWindow ?

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

    Default How to put 2 Widgets in a MainWindow ?

    Hi,

    I want to put 2 widgets (a QwtPlot and a QTableWidget) on the same mainwindow with a QVBoxLayout, but I see nothing when I try with 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,p);
    7. data = new Data(250,buffer,p);
    8.  
    9. QVBoxLayout* layout = new QVBoxLayout(p);
    10.  
    11. layout->addWidget(plot);
    12. layout->addWidget(data);
    13.  
    14. //Start of on_watcher
    15. data->on_watcher();
    16.  
    17. }
    To copy to clipboard, switch view to plain text mode 

    And here's my construcors:

    data .h:
    Qt Code:
    1. Data(int _periode,double* &_voies,QWidget*parent=0);
    To copy to clipboard, switch view to plain text mode 

    data.cpp:
    Qt Code:
    1. Data::Data(int _periode,double * &_voies,QWidget *parent):QTableWidget(parent),periode( _periode )
    To copy to clipboard, switch view to plain text mode 

    plot.h:
    Qt Code:
    1. Plot( int duree, int periode,double * _time, double * _voie, QWidget * p = NULL );
    To copy to clipboard, switch view to plain text mode 

    plot.cpp:
    Qt Code:
    1. Plot::Plot(int duree,int periode,double* _time,double* _voie,QWidget*p): QwtPlot(p)
    To copy to clipboard, switch view to plain text mode 

    Thanks for your help !

  2. #2
    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 ?

    Try this:
    Qt Code:
    1. QVBoxLayout* layout = new QVBoxLayout(this); //so that the layout can re-parent your widgets with this (the window itself) not the parent of the window (witch is most likely 0)
    To copy to clipboard, switch view to plain text mode 

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

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

    Ok, if I change my mainwindow.cpp to this:
    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. QVBoxLayout* layout = new QVBoxLayout(this);
    10. data->on_watcher();
    11. }
    To copy to clipboard, switch view to plain text mode 

    I see my QTableWidget in the left corner, and no plot at all...

  4. #4
    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 ?

    In the code from last post you didn't add the widgets to layout.

    Did you did that in your actual project? (You still need to do that)

    Qt Code:
    1. mainwindow::mainwindow(QWidget*p)
    2. {
    3.  
    4. double *buffer=NULL;
    5.  
    6. plot=new Plot(100,250,buffer,buffer+1,p);
    7. data = new Data(250,buffer,p);
    8.  
    9. QVBoxLayout* layout = new QVBoxLayout(this);
    10.  
    11. layout->addWidget(plot);
    12. layout->addWidget(data);
    13.  
    14. //Start of on_watcher
    15. data->on_watcher();
    16.  
    17. }
    To copy to clipboard, switch view to plain text mode 

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

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

    oopps , I meant:

    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. QVBoxLayout* layout = new QVBoxLayout(this);
    10.  
    11. layout->addWidget(plot);
    12. layout->addWidget(data);
    13.  
    14. data->on_watcher();
    15.  
    16. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    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 ?

    Are you sure this is what you want
    Qt Code:
    1. plot=new Plot(100,250,buffer,buffer+1,this); // the buffer pointer is NULL and you are adding 1 to that???
    To copy to clipboard, switch view to plain text mode 

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

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

    That's not the problem....buffer acts as a chart...everything works fine in there....when I do a setCentralWidget(plot) I see my plot and my program does what it's supposed to..
    Altought, now, I want to add a QTableWidget, which is created and filled in my data class, and I don't know how to put both on my MainWindow

  8. #8
    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 ?

    Do you get some warnings about mainWindow allready have a layout?

    Try to add a QWidget *test = new QWidget(0); make it the parent of the layout QVBoxLayout* layout = new QVBoxLayout(test); and setCentralWidget(test);

  9. #9
    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 

  10. #10
    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.

  11. #11
    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.

  12. #12
    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 !!!

  13. #13
    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 

  14. #14
    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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.