How to place two or more widgets where ever we want on the QMainWindow constructor or on the Application in Qt
-
Hi Guys,
I Have a mainwindow.cpp file in that QMainWindow class is inherits the QMainWindow class and I Have plot.cpp file in this file I have Plot class which inherits the QwtPlot class.
Now In the QMainWindow class constructor, I would like to add Plot object and QwtSlider object, by creating the QHboxLayout
constructor code:MainWindow::MainWindow(QWidget *parent): QMainWindow(parent),ui(new Ui::MainWindow),plot(0) { ui->setup(this); plot=new Plot(this); // This is one of the subclass of QwtPlot QwtSlider *Slider=new QwtSlider(this);// QwtSlider class QHBoxLayout *mainLayout=new QHBoxLayout(this); mainLayout->addWidget(plot,60,Qt::AlignCenter); mainLayout->addWidget(slider); }
Problem is application is not displaying the Plot widget in the center and Qwtslider is also attached to the Menubar.
how to place them where ever we want on the mainwindow of the application?
-
Hi
Should you not add mainLayout to centralWidget ?
centralWidget()->setLayout(mainLayout); -
Hi,
QMainWindow already has a layout. The one that allows him to do its magic with dock widget, toolbars, etc.
Therefore:
@thippu said in How to place two or more widgets where ever we want on the QMainWindow constructor or on the Application in Qt:
QHBoxLayout *mainLayout=new QHBoxLayout(this);
won't work and should even show you a warning telling you that you are trying to set a layout on a widget that already has one.
@mrjj's suggestions is the right one.
-
@SGaist said in How to place two or more widgets where ever we want on the QMainWindow constructor or on the Application in Qt:
Hi,
QMainWindow already has a layout. The one that allows him to do its magic with dock widget, toolbars, etc.
Therefore:
@thippu said in How to place two or more widgets where ever we want on the QMainWindow constructor or on the Application in Qt:
QHBoxLayout *mainLayout=new QHBoxLayout(this);
won't work and should even show you a warning telling you that you are trying to set a layout on a widget that already has one.
@mrjj's suggestions is the right one.
Yes, There was the warning.