Results 1 to 3 of 3

Thread: Subclassing QLabel to be used in QtDesigner

  1. #1
    Join Date
    Nov 2009
    Posts
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Subclassing QLabel to be used in QtDesigner

    Hi all,
    I am designing a dialog for which I need a label that changes some of its properties (color, text, etc...) according to some events. So I am subclassing QLabel in a class called VerdLabel to code the paintEvent function properly. The problem is that when I try to integrate this new class in QtCreator by promoting it from QLabel to VerdLabel the following error appears when compiling the code:

    Qt Code:
    1. Ui_pruebas01Class::setupUi(QDialog*)’:
    2. ui_pruebas01.h:105: error: no matching function for call to ‘VerdLabel::VerdLabel(QDialog*&)’
    To copy to clipboard, switch view to plain text mode 

    I am trying to use the following test code for the constructor:

    Qt Code:
    1. VerdLabel::VerdLabel()
    2. {
    3. this->setText( "verdura" );
    4. }
    To copy to clipboard, switch view to plain text mode 


    It looks like the 'Ui_pruebas01Class::setupUi(QDialog*)' is looking for a VerdLabel(QDialog*&) for the VerdLabel class but the constructor is defined as VerdLabel(), so I changed the code to:

    Qt Code:
    1. VerdLabel::VerdLabel(QDialog *parent)
    2. :QDialog(parent)
    3. {
    4. Q_D(QLabel);
    5. d->init();
    6. this->setText( "verdura" );
    7. }
    To copy to clipboard, switch view to plain text mode 

    but in this case, the following error appears:

    Qt Code:
    1. verdlabel.cpp: In constructor ‘VerdLabel::VerdLabel(QDialog*)’:
    2. verdlabel.cpp:4: error: the type ‘QDialog’ is not a direct base of ‘VerdLabel’
    To copy to clipboard, switch view to plain text mode 

    So I don't know how to solve this, so if anybody can help me I would really appreciate it.

    Thanks,
    yoyega

  2. #2
    Join Date
    Nov 2009
    Posts
    129
    Thanks
    4
    Thanked 29 Times in 29 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Subclassing QLabel to be used in QtDesigner

    Try this:
    Qt Code:
    1. class VerdLabel : public QLabel {
    2. Q_OBJECT
    3. public:
    4. VerdLabel (QWidget* parent = 0, Qt::WindowFlags f = 0) : QLabel("verdura", parent, f) {}
    5. VerdLabel (const QString& text, QWidget* parent = 0, Qt::WindowFlags f = 0) : QLabel(text, parent, f) {}
    6. /* other members and methods */
    7. };
    To copy to clipboard, switch view to plain text mode 
    in the header file.

    Of course, you can use declarations for the constructors and put the definitions in a cpp file if they get more complicated — the idea is to duplicate the same constructors that are defined for QLabel.

    QDialog is a subclass of QWidget, so the first constructor will match the call to ‘VerdLabel::VerdLabel(QDialog*&)’, and the initializer will set the initial properties of the QLabel from which your class is derived properly.

  3. #3
    Join Date
    Nov 2009
    Posts
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Subclassing QLabel to be used in QtDesigner

    Thank you very much for your reply, everything works now

Similar Threads

  1. Trouble with QLabel
    By dany_MB in forum Newbie
    Replies: 3
    Last Post: 14th August 2009, 08:21
  2. Replies: 3
    Last Post: 17th July 2008, 07:43
  3. [SOLVED] subclassing qlabel
    By mickey in forum Newbie
    Replies: 10
    Last Post: 4th June 2008, 14:43
  4. QLabel ScaledContents ignored by style sheet?
    By WinchellChung in forum Newbie
    Replies: 3
    Last Post: 27th February 2008, 14:50
  5. QLabel size policy
    By Caius Aérobus in forum Qt Programming
    Replies: 3
    Last Post: 7th December 2007, 17:57

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.