PDA

View Full Version : mainwindow layout vs widget layout



fatecasino
13th December 2010, 13:39
hi!
I am new to Qt and was very happy when i created my first own widgets. Now i am trying to create something bigger using the idea of having "mainwindows" with toolbars, menus, etc.

In Qwidgets I used the QGroupBox,QVBoxLayout,etc to arrange the design (buttons,sliders, Qwtplot areas,etc) of my widget layout. Now I tried to do the same with the mainwindow and it does not work. Reading the documentation I found out that in mainwindow class I cannot directly add Qwidgets, but I have to create a main widget and use the setCentralWidget(). I got a bit confused but just to get it right, the procedure should be like this?

create mainwindow
create main widget
create several other smallerQwidgets
use QGroupbox/QVBoxLayout to arrange smallerQwidgets in the main widget
setCentralWidget(mainWidget)

stampede
13th December 2010, 17:19
Just code / design your widgets as usual, and when you want to display them in QMainWindow call the "setCentralWidget(my_widget)" method.


I am new to Qt and was very happy when i created my first own widgets
Let "MyWidget" be your custom widget that made you happy. If you want to use it with QMainWindow you dont need to change anything in MyWidget, just call QMainWindow::setCentralWidget(my_widget), where my_widget is an instance of MyWidget.

Of course if you want to use menus, toolbars ect. it will be convenient for you to create a subclass of QMainWindow, but I think you know that.

Remember that QMainWindow is taking ownership of "my_widget", so when you change central widget to another one dont delete old pointer, object will be removed automatically.

I think you can find some examples of QMainWindow usage in QT samples.

fatecasino
14th December 2010, 14:45
thanks,
I started working on this with some inheritance reading.
I have created a mainwindow
I have created a widget
I set the widget as central widget of the mainWindow
and..
I created a my2dplot class to plot 2d plots (worked)
then
I tried to copy bode example mouse tracking and zoomer and I ended up like this:

http://www.qtcentre.org/threads/36950-qwt-plots-amp-inheritance