PDA

View Full Version : Subclassing QLabel to be used in QtDesigner



yoyega
23rd November 2009, 12:33
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:



Ui_pruebas01Class::setupUi(QDialog*)’:
ui_pruebas01.h:105: error: no matching function for call to ‘VerdLabel::VerdLabel(QDialog*&)’


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



VerdLabel::VerdLabel()
{
this->setText( "verdura" );
}



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:



VerdLabel::VerdLabel(QDialog *parent)
:QDialog(parent)
{
Q_D(QLabel);
d->init();
this->setText( "verdura" );
}


but in this case, the following error appears:



verdlabel.cpp: In constructor ‘VerdLabel::VerdLabel(QDialog*)’:
verdlabel.cpp:4: error: the type ‘QDialog’ is not a direct base of ‘VerdLabel’


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

Thanks,
yoyega

Coises
23rd November 2009, 13:34
Try this:


class VerdLabel : public QLabel {
Q_OBJECT
public:
VerdLabel (QWidget* parent = 0, Qt::WindowFlags f = 0) : QLabel("verdura", parent, f) {}
VerdLabel (const QString& text, QWidget* parent = 0, Qt::WindowFlags f = 0) : QLabel(text, parent, f) {}
/* other members and methods */
};

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.

yoyega
23rd November 2009, 14:03
Thank you very much for your reply, everything works now :)