View Full Version : custom widgets

24th April 2012, 23:01
I want to enhance some standard Elements (like QLineEdit, QLabel, QListWidget) with a set of additional properties. So I created a Base class with the additional functionality and defined my new widgets as follows:

class myBase {
enum myAttrib { attribA = 1, AttribB = 2, AttribC = 4};

class myQLabel : public QLabel, public myBase {

How can I get 'myAttrib' shown in the QtDesigner property editor of my new widget myQLabel? I tried to inherit myBase form QObject which I think is the right way to do, but then myQLabel depends on two classes of type QObject, which isn't supported.
I can put myAttrib into myQLabel (which works) but there must be a better solution.

I've also tried to use the domXML()-funktion to definen the Flag-property (with the 'set' datatyp-element), but QtDesigner ignored it silently.

Any hints?
Thanks a lot

25th April 2012, 05:40
If all you are adding is non-visible properties to your widget then you might as well just use the Promotion feature in Designer. Nothing special is required: drop a standard QLineEdit on your form and promote it to YourExtendedLineEdit.

If you have a widget that looks different then you might want to integrate it with Designer. There's a fully worked example of defining a new widget to Designer in Creating Custom Widgets for Qt Designer. You need to create a plugin and implement a couple of interfaces.

You'll need to get your widget designed properly first. As it is written above myAttrib is a private enum, so it will not be visible outside of an instance of myBase. You do not need multiple inheritance, but you will need macros to define the enum(s) and property (-ies) and expose the class

Something like:

#include <QLineEdit>
#include <QtDesigner/QDesignerExportWidget>

class QDESIGNER_WIDGET_EXPORT MyLineEdit: public QLineEdit
Q_PROPERTY(AttribEnum newAttrib READ newAttrib WRITE setNewAttrib)

MyLineEdit(QWidget *p = 0): QLineEdit(p), m_newAttrib(valueA)

enum AttribEnum { valueA , valueB,valueC };
void setNewAttrib(const AttribEnum value) { m_newAttrib = value; };
AttribEnum newAttrib() const { return m_newAttrib; };

AttribEnum m_newAttrib;

25th April 2012, 07:42
Hi ChrisW67,

in your example AttribEnum is a member of MyLineEdit. Like that it works of course. But that's not what I want. I have ~10 base elements I want to enhance. So every time I change 'AttribEnum' I have to change 10 classes (and hopefully don't forget one). Therefor I've created 'myBase'.

I have also tried to work with macros, but uic doesn't support macro-expanding