[Qt-interest] Same widget in two Designer plugins - problem

Frank Hemer frank at hemer.org
Fri Sep 30 14:44:55 CEST 2011


On Friday 30 September 2011 14:32:25 André Somers wrote:
> Hi,
>
> I have created my own slider widget. I have also developed a designer
> plugin for it. That all works nicely. However, as soon as I want to mimick
> the behaviour of the way QSlider is integrated into designer, I run into
> problems. I would like to have a separate vertical and horizontal slider
> item in the list of widgets, but I can not figure out how to do that. If I
> put another name than the actual class name in the name() reimplementation,
> I get complaints from designer and I do not get a visual widget if I drag &
> drop the widget to a form. However, if I use the same name (and just a
> different description and icon), I get into other problems: only one of the
> sliders is showing.
>
> Am I missing something here? How do the standard widgets manage to use
> human readable names (not the class name) for their widgets without issue?
> Why can QSlider appear twice, but own slider not?

You need to have individual implementation classes for the two slider types 
thus you can write in the createWidget reimpl:

QWidget * SSliderPlugin::createWidget (QWidget * parent) {
   if (m_orientation == Qt::Horizontal) {
      return new SHorizontalSlider (parent);
   } else {
      return new SVerticalSlider (parent);
   }
}

and in name:

QString SSliderPlugin::name () const {// very important to be created in 
designer
   if (m_orientation == Qt::Horizontal) {
      return QLatin1String("SHorizontalSlider");
   } else {
      return QLatin1String("SVerticalSlider");
   }
}

and in domXml:

QString SSliderPlugin::domXml () const {// very important to be created in 
designer; note the class and name info!!!
   QString xml ("<widget class=\"%1\" name=\"sSlider\">\n"
         "</widget>\n");
   return xml.arg (m_orientation == 
Qt::Horizontal ? "SHorizontalSlider" : "SVerticalSlider");
}

and when registering the plugins:

   m_plugins.append (new SSliderPlugin (this, Qt::Horizontal));
   m_plugins.append (new SSliderPlugin (this, Qt::Vertical));

To have two impl classes for one plugin, thats easy:

class SHorizontalSlider : public SSlider {
   Q_OBJECT

 public:
   SHorizontalSlider (QWidget * parent = 0)
      : SSlider (Qt::Horizontal, parent) {}
   virtual ~SHorizontalSlider () {}
};

class SVerticalSlider : public SSlider {
   Q_OBJECT

 public:
   SVerticalSlider (QWidget * parent = 0)
      : SSlider (Qt::Vertical, parent) {}
   virtual ~SVerticalSlider () {}
};

Note: code above is untested -

Have fun
Frank



More information about the Qt-interest-old mailing list