Results 1 to 3 of 3

Thread: custom widgets

  1. #1
    Join Date
    Apr 2012
    Qt products

    Default custom widgets

    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

  2. #2
    Join Date
    Mar 2009
    Brisbane, Australia
    Thanked 1,587 Times in 1,516 Posts
    Qt products
    Qt4 Qt5
    Unix/X11 Windows
    Wiki edits

    Default Re: custom widgets

    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:
    Qt Code:
    1. #include <QLineEdit>
    2. #include <QtDesigner/QDesignerExportWidget>
    4. class QDESIGNER_WIDGET_EXPORT MyLineEdit: public QLineEdit
    5. {
    6. Q_OBJECT
    7. Q_ENUMS(AttribEnum)
    8. Q_PROPERTY(AttribEnum newAttrib READ newAttrib WRITE setNewAttrib)
    10. public:
    11. MyLineEdit(QWidget *p = 0): QLineEdit(p), m_newAttrib(valueA)
    12. {
    13. }
    15. enum AttribEnum { valueA , valueB,valueC };
    16. void setNewAttrib(const AttribEnum value) { m_newAttrib = value; };
    17. AttribEnum newAttrib() const { return m_newAttrib; };
    19. private:
    20. AttribEnum m_newAttrib;
    21. };
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Apr 2012
    Qt products

    Talking Re: custom widgets

    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

Similar Threads

  1. Replies: 1
    Last Post: 18th July 2012, 09:59
  2. Custom widgets, where must I put them ?
    By tonnot in forum Newbie
    Replies: 4
    Last Post: 22nd October 2010, 08:56
  3. Custom Widgets
    By walden02 in forum Qt Programming
    Replies: 3
    Last Post: 23rd December 2009, 11:43
  4. Replies: 0
    Last Post: 15th May 2009, 15:38
  5. Help with custom widgets!!
    By superteny in forum Qt Programming
    Replies: 4
    Last Post: 29th April 2009, 12:43

Tags for this Thread


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.