PDA

View Full Version : Subclass QLabel



Globulus
6th August 2011, 17:58
I am new in QT...
So, my question is clear for most of you, but I still dont understand,
how can I subclass QLabel to make my own enterEvent, leaveEvent, mousePressEvent, mouseReleaseEvent, to extend QLabel functionality.
And then use it in my .ui application?

Dong Back Kim
6th August 2011, 18:22
I am new in QT...
So, my question is clear for most of you, but I still dont understand,
how can I subclass QLabel to make my own enterEvent, leaveEvent, mousePressEvent, mouseReleaseEvent, to extend QLabel functionality.
And then use it in my .ui application?

Hi there,

I reply here instead of the previous post.



class MyLabel : public QLabel
{
Q_OBJECT

public:
MyLabel(QWidget * parent = 0);
~MyLabel();

protected slots:

virtual void enterEvent ( QEvent * event );
virtual void leaveEvent ( QEvent * event );
virtual void mouseMoveEvent ( QMouseEvent * event );
virtual void mousePressEvent ( QMouseEvent * event );
virtual void mouseReleaseEvent ( QMouseEvent * event );
};

MyLabel::MyLabel(QWidget * parent) : QWidget(parent)
{
}

MyLabel::~MyLabel()
{
}

void MyLabel::enterEvent ( QEvent * event )
{
}

void MyLabel::leaveEvent ( QEvent * event )
{
}

void MyLabel::mouseMoveEvent ( QMouseEvent * event )
{
}

void MyLabel::mousePressEvent ( QMouseEvent * event )
{
}

void MyLabel::mouseReleaseEvent ( QMouseEvent * event )
{
}


In order to use this widget in the QtDesigner...

1. remember the header file name of this class (i.e. MyLabel.h in this case)
2. go to QtDesigner and put a QLabel or QWidget into the form
3. and the promote the QLabel or QWidget to MyLabel.

Hope this helps.

Regards,

Globulus
6th August 2011, 19:26
That was usefull!
Thanks! :)

Dong Back Kim
6th August 2011, 20:26
That was usefull!
Thanks! :)

My pleasure =)

nileshjarhad
29th July 2013, 13:15
cn u gv me complete code for this...
?

ChrisW67
29th July 2013, 22:30
That is "complete code for this." What is more, inheriting from a class and overriding or adding functions is pure C++ and not Qt specific.