PDA

View Full Version : Custom widget, dealing with resize the right way?



Ishark
22nd May 2008, 13:49
Hello,
I'm designing a new custom widget (very simple, just a groupbox with some stuff in it, but since I need many copies I've decided to create a widget).

What is the right way to deal with resizing?

Right now when I resize the widget, the groupbox inside it stays the same size, which of course is not what I want. I've tried to reimplement resizeEvent to grab the event and resize the groupbox. It works, but then I can squeeze the groupbox down to size 0, which messes everything up when putting the widget inside a Layout (it gets squeezed to size 0 by the spacers).

Is there a full example of creating a custom widget and implementing it in designer?
I've looked at the "world time clock" one on the Qt site, but it's very very minimal.... for example nowhere it cites the Q_PROPERTY and Q_ENUM macros. I found out they still work in Qt4 just by cut'n'paste from the old Qt3 widget.

wysota
22nd May 2008, 14:34
You need a layout for all widgets containing other widgets, starting from the top-most one. If your widget is derived from QWidget, you need to apply a layout on it as well (and on the groupbox). If it is derived directly from the groupbox, you need to apply a layout on the groupbox (and possibly on other widgets acting as containers).

Ishark
23rd May 2008, 09:52
Thanks for your answer. It seems to be working fine....
Just to make sure I'm doing things right. My widget is derived from QWidget.

In the constructor:
- I create a GroupBox.
- I create a GridLayout for the GroupBox and populate it with stuff.
- I do GroupBox->setLayout(GridLayout).
- I create a HBoxLayout and addWidget(GroupBox) to it.
- I do a this->setLayout(HBoxLayout).

For the 2nd part of my question: I've searched for Q_PROPERTY and it only appears in the Qt 3.3 documentation, but it seems to work fine in Qt4. Is it supposed to exist and be documented somewhere?

wysota
23rd May 2008, 13:49
Why don't you derive from QGroupBox? You'd have one level of hierarchy less...

Q_PROPERTY is documented. Open Assistant and type in Q_PROPERTY into the index tab.

Ishark
23rd May 2008, 14:45
Right, I'll derive directly from GroupBox.
I've never used assistant and I've installed it. It does indeed document Q_PROPERTY.... is the documentation found there identical to the website?

I'm also having a very weird behaviour with my widget. If I use a GroupBox to group my buttons/labels and redefine mousePressEvent to trap mouse events (not the one on buttons of course) then I never get a single event. If I replace the GroupBox with a Frame then I get the events. Is there some fundamental difference between the two? They used to work the same way in Qt3.

I'll try to derive directly from GroupBox to see if it changes anything.

EDIT: it does, when deriving from GroupBox I get the events as expected.

jpn
25th May 2008, 08:22
I've never used assistant and I've installed it.
It's shipped together with Qt.


It does indeed document Q_PROPERTY.... is the documentation found there identical to the website?
Yes.