PDA

View Full Version : maximizing own widget on parent



masoroso
21st April 2006, 09:32
Hi all,

I've made a class based on QWidget and it needs another QWidget as parent.

Map::Map( QWidget *parent ) : QWidget(parent)
Now I designed a window in QtDesigner and I subclassed the ui code and on a frame that I placed using QtDesigner the map is placed:

map = new Map(ui.frameMain);
Next step would be to draw on the map using the paint event. However.. if I draw a line from the topleft to bottomright corner of the map it only takes up a small part of the parent (the frame).

How can I make the map always use the available space of the frame?

Thanks in advance!
Rob

ps. I tried setSizePolicy(QSizePolicy::Expanding,QSizePolicy:: Expanding); in the constructor but that didn't work

jacek
21st April 2006, 10:10
Add a layout to ui.frameMain.

masoroso
21st April 2006, 10:53
It works.. thanks jacek!

this is what I added to the constructor of the main window



map = new Map(ui.frameMain);
m_layoutMain = new QHBoxLayout;
m_layoutMain->addWidget(map);
ui.frameMain->setLayout(m_layoutMain);


I still got one question though.. is it possible to access layouts that were create using QtDesigner? I added one to the main form but after using this code

ui.frameMain->layout()->addWidget(map);
the application crashes at creation.

Again.. thanks in advance :)

zlatko
21st April 2006, 11:00
Check pointers that you use..have you add widget after create it ?
Also check what return ui.frameMain->layout()

jacek
21st April 2006, 11:10
is it possible to access layouts that were create using QtDesigner? I added one to the main form
It should be possible.


but after using this code [...] the application crashes at creation.
Did you invoke ui.setupUi() before that line?

masoroso
21st April 2006, 12:10
It should be possible.


this is the code:


MainForm::MainForm(QWidget *parent)
: QMainWindow(parent)
{
//setup signals and slots created in QtDesigner
ui.setupUi(this);

//create a map
map = new Map(ui.frameMain);
//ui.frameMain->layout()->addWidget(map); //not sure why this didn't work :-/
QLayout* l = ui.frameMain->layout();

qDebug() << l;


The debug line gives 'QObject(0x0)'.. so it's empty

EDIT..
if I use a button and then try to access the layout it does work ([2148] DEBUG: in loadFile QLayout: QHBoxLayout(0x11874e0)
) so I guess I need to access the layout at form showup and not at creation? Any advice on which event to use then (a QMainWindow event or a timer?)

jacek
21st April 2006, 14:43
Are there any widgets on that ui.frameMain widget (except for that map)? Maybe you haven't set any layout for it in the Qt Designer?

masoroso
21st April 2006, 16:09
Are there any widgets on that ui.frameMain widget (except for that map)? Maybe you haven't set any layout for it in the Qt Designer?

I have a dockwidget, a layout and a frame on the form. I first dropped the layout on the form (QHboxLayout) and then dropped the frame on it. The dockwidget is placed left of the other widgets and the dockwindow is not part of the layout.

What I do not understand is why it is not possible to access the layout because you cannot give layouts a name in QtDesigner. If you look at the generated ui code the layout *has* a name so you could use this name in the subclassing code. :confused:

I presume it is possible to access the layouts by using the layout() function but I also presume this is only possible if the window has been made visible because then they are actualy created.. ? It remembered me of Delphi's onCreate and onShow event of a TForm which als had these `problems'. Now I only need to find a event that occurs only once at startup (or add a boolean to trigger the setting of the layout..).

I'll puzzle some more

jacek
21st April 2006, 17:12
I have a dockwidget, a layout and a frame on the form. I first dropped the layout on the form (QHboxLayout) and then dropped the frame on it. The dockwidget is placed left of the other widgets and the dockwindow is not part of the layout.
If I got you right, you haven't set any layout for the frameMain frame, only for its parent.

BTW. Layouts listed in the "Widget Box" are not pure layouts, but rather widgets with a given layout. Better use "Lay Out ..." actions from the Form menu/toolbar.