views:

31

answers:

1

How can I in code of the custom Qt widget know that it is currently instantiated in Qt designer?

Use case:

I build a complex custom widget that has several child widgets like QPushButton, QLabel etc.

As application logic require, when widget is created most of those sub component are not visible but in design time when I put it on a form I would like to see them.

To be able to play with style sheet at design time. Currently what I get is a empty is only a result of constructor - minimal view (actually empty in my case).

What I am looking for is to be able to do something like

MyQWidget::(QWidget *parent)
{
 ....
   if(isRunningInDesigner())
   {
      myChildWidget1->setVisible(true);
      myChildWidget2->setVisible(true);
      myChildWidget3->setVisible(true);
   }
   else
   {
      myChildWidget1->setVisible(false);
      myChildWidget2->setVisible(false);
      myChildWidget3->setVisible(false);
   }
....
}

So what should I put in to this bool isRunningInDesigner() ?

A: 

From the Qt Designer manual:

To give custom widgets special behavior in Qt Designer, provide an implementation of the initialize() function to configure the widget construction process for Qt Designer specific behavior. This function will be called for the first time before any calls to createWidget() and could perhaps set an internal flag that can be tested later when Qt Designer calls the plugin’s createWidget() function.

Those are methods from the QDesignerCustomWidgetInterface plugin interface. In short: you tell the widget to behave differently when Qt Designer asks your plugin to create instances of your custom widget.

andref