The label label control in QT is not limited to the display of static text, in fact, it is very powerful, because it has setpixmap () member function, it can be used when the Display Picture window, but also real-time display camera captured pictures, Then its support for mouse events is not as powerful as qwidget, many times we want to Qlabel capture mouse clicks or double-click events, which itself cannot be implemented, we have to write a new class to achieve the functions we need, which is the strength of C + +, The inheritance feature allows us to add new features indefinitely based on the original. So here we want Qlabel to capture the double-click event of the mouse, we need to create a new class named Qtclickablelabel, which inherits from Qlabel:
Qtclickablelabel.h
#ifndef Qtclickablelabel_h#defineQtclickablelabel_h#include<QLabel>classQtclickablelabel: Publicqlabel{Q_object Public: ExplicitQtclickablelabel (Qwidget *parent =0); ExplicitQtclickablelabel (ConstQString &text ="", Qwidget *parent =0); ~Qtclickablelabel (); Signals:voidclicked ();protected: voidMousedoubleclickevent (Qmouseevent *Event);};#endif //Qtclickablelabel_h
Qtclickablelabel.cpp
" qtclickablelabel.h " Qtclickablelabel::qtclickablelabel (const QString &text, Qwidget *parent) : Qlabel ( Parent) { this,*parent) : Qlabel (parent) {}qtclickablelabel::~ Qtclickablelabel () {} void Qtclickablelabel::mousedoubleclickevent (Qmouseevent *event) { emit clicked ();}
If you want to implement capturing mouse click events, simply add the following code:
void Qtclickablelabel::mousepressevent (Qmouseevent *event) { emit clicked ();}
Qt make clickable label making a clickable label control