Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Promote to custom widget in a namespace

I have MyCustomWidget in a namespace MyNameSpace

namespace MyNameSpace{
    class MyCustomWidget : public QWidget{
    };
}

How do I promote a QWidget to MyCustomWidget in my UI form? It doesn't seem to accept a custom namespace.

like image 279
Dat Chu Avatar asked Sep 01 '10 19:09

Dat Chu


2 Answers

Type the name of the class with the namespace included: My::PushButton. It works. Note that:

  • Qt Designer will try and guess the header name: my_pushbutton.h. Change it if it is wrong.
  • You should check the include paths in your project to determine if a global include for the promoted widget will work
like image 138
andref Avatar answered Oct 12 '22 11:10

andref


My custom plugin works good using the following code. In this example, the namespace is not only used in the plugin but also in the class which inherits the plugin.

Header for the plugin --- it could be used in QDesigner

namespace plugin { 

class MyCustomPlugin: public QObject, public QDesignerCustomWidgetInterface
{
    Q_OBJECT
    Q_INTERFACES(QDesignerCustomWidgetInterface)

    public:
    MyCustomPlugin(QObject *parent = 0);

    bool isContainer() const;
    bool isInitialized() const;
    QIcon icon() const;
    QString domXml() const;
    QString group() const;
    QString includeFile() const;
    QString name() const;
    QString toolTip() const;
    QString whatsThis() const;
    QWidget *createWidget(QWidget *parent);
    void initialize(QDesignerFormEditorInterface *core);

    private:
    bool initialized;
};

}

cpp file for that plugin

Implement the class as usual, but the following methods must take into account the namespace:

QString MyCustomPlugin::domXml() const
{
   return "<widget class=\"plugin::MyCustomClass\" name=\"mycustomclass\">\n"
   ...
}

QString MyCustomPlugin::name() const
{
   return "plugin::MyCustomClass";
}

QWidget *MyCustomPlugin::createWidget(QWidget *parent)
{
   return new plugin::MyCustomClass(parent);
}

Q_EXPORT_PLUGIN2(mycustomplugin, plugin::MyCustomPlugin)

MyCustomClass

namespace plugin { 

class QDESIGNER_WIDGET_EXPORT MyCustomClass: public MyCustomPlugin
{
   Q_OBJECT

   public:
      MyCustomClass(QWidget * parent = 0) {}
}; 

}
like image 20
Tarod Avatar answered Oct 12 '22 12:10

Tarod