[Qt-interest] How does Qt designer manage resizing widgets at design time?

K. Frank kfrank29.c at gmail.com
Mon Mar 22 21:06:03 CET 2010


To All -

File this one perhaps under "egregious Qt hackery."

On Sun, Mar 21, 2010 at 8:45 PM, K. Frank <kfrank29.c at gmail.com> wrote:
> ...
> On Sun, Mar 21, 2010 at 8:06 PM, Andreas Pakulat <apaku at gmx.de> wrote:
>> On 21.03.10 18:46:43, K. Frank wrote:
>>> >> Is there any (easy) way to get
>>> >> my place-holder widget (in my test case, a QWidget) to mimic in Qt designer
>>> >> more closely QTableWidget (or my custom widget)?
>>> > ...
>>> Again, this is certainly good enough for my practical needs, but I would
>>> like to follow up on Sean's comment, and ask if it is possible to tweak
>>> my QWidget place-holder so that behaves more realistically in designer.
>> ...

Well, against my better judgment, I played around with this some more,
and against my better judgment, I tried the following:

   #include "ui_test_widget.h"
   class TestWidget : public QTableWidget, private Ui::TestWidget {
     Q_OBJECT
     // ...
   };

That is, I changed the base class of my custom widget (TestWidget) to
QTableWidget from QWidget.  This now let's me use a QTableWidget as
the place-holder for my custom widget in Qt designer, and promote it to
TestWidget.

What's odd (or perhaps better described as "screwed up") about this is
that test_widget.ui (which uic turns into ui_test_widget.h) comes from
using designer to drop a QWidget onto an empty form, and then add
various child widgets to it (an actual QTableWidget, and some labels and
check boxes).

Superficial tests suggest that everything is working as "expected."  In any
event my custom widget, promoted from QTableWidget, resizes like a
QTableWidget, and at run time, my custom widget seems to have the
correct behavior.  At least I don't see any obvious crossed wires.

I don't believe any of the QTableWidget (beyond its QWidget piece) from
which TestWidget derives is actually being used -- I assume that it just
goes along for the ride as excess baggage.

I don't think that I will use this in my real code -- it makes me queasy, and
it feels like I'm cheating somehow.

But it is amusing that

   class TestWidget : public QTableWidget, private Ui::TestWidget

works (seems to work), even though Ui::TestWidget is based on QWidget
and not on QTableWidget.  I guess the fact that QTableWidget derives
from QWidget is good enough.

Anyway, this is my subversive contribution to the list for the day...

If anyone can offer insight into what is really going on under the hood, please
chime in.

Thanks.


K. Frank


>> You could try to play with the stretch factors and/or the other
>> size-values. But unless you provide a qt designer plugin for your custom
>> widget you probably won't get the same behaviour as with a real
>> qtablewidget. The reason is that the place-holder doesn't have the same
>> implementation for the sizeHint and other size-related methods.
>>
>> Andreas
>
> Yes, this makes sense.  If I want the realistic behavior at design time, then
> a plugin is the way to go.
> ...




More information about the Qt-interest-old mailing list