Qt make clickable label making a clickable label control

Source: Internet
Author: User


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:


#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.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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

Tags Index: