Qlabel adding clicked Events

Source: Internet
Author: User
Tags emit

QT Development will know that Qlabel is not the default clicked event, but Qt has a good set of signal/slot mechanism, and QT is based on C + + object-oriented thinking to design, then we can easily define some classes by ourselves, Rewrite some of the methods of Qlineedit or Qlabel to implement methods that are not, such as clicked events. I'll add clicked events to the Qlabel to illustrate.

QT inside has a key word emit, this keyword meaning is to trigger a signal, specific emit more detailed use method, can Baidu, this article does not do specific explanation, just know emit is trigger a signal method can.

Let's start by saying how to add the clicked () event to Qlabel. First we need a class that inherits Qlabel, which we define as: MyLabel1. Then analyze the clicked () event, this event is because we clicked the mouse click on the next Qlabel, And then the trigger, then exactly, we think of a mousepressevent event in Qt, so we can rewrite the Mousepressevent event and then trigger the custom one clicked () signal, This will enable the click Qlabel to send a clicked () signal, the code is as follows:

MyLabel1.h

#ifndef mylabel1_
#define Mylabel1_

#include <QLabel>
#include <QObject>
#include <QMouseEvent>
#include <QWidget>
#include <QDebug>
#include <QFileDialog>
#include <QMessageBox>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlError>

#include "Struct.h"


Class Mylabel1:public Qlabel
{
Q_object
Public
MyLabel1 (qwidget * parent = 0);
~mylabel1 () {}

Signals:
void clicked (bool, QString);
void mousemoved (QString);

Public Slots:
void slotclicked (bool m_bleftpress, QString accessibledescription);
void slotmousemoved (QString accessibledescription);

Protected
void Mousepressevent (Qmouseevent * event);
void Mousemoveevent (Qmouseevent * event);

Private
BOOL m_bleftpress;

};

#endif

MyLabel1.cpp

#include "MyLabel1.h"

Mylabel1::mylabel1 (Qwidget * parent)
: Qlabel (parent)
{
M_bleftpress = true;

Setmousetracking (TRUE);
}

void mylabel1::slotclicked (bool, QString)
{
Qdebug () << "Clicked";
}

void Mylabel1::slotmousemoved (QString)
{
Qdebug () << "slotmousemoved";
}

void Mylabel1::mousepressevent (Qmouseevent * Event)
{
Trigger the clicked signal if clicked
if (Event->button () = = Qt::leftbutton)
{
M_bleftpress = true;
Emit clicked (M_bleftpress, This->accessibledescription ());
}
Else
{
M_bleftpress = false;
Emit clicked (M_bleftpress, This->accessibledescription ());
}

Pass the event to the parent class for processing
Qlabel::mousepressevent (event);
}

void Mylabel1::mousemoveevent (Qmouseevent * Event)
{
Emit mousemoved (This->accessibledescription ());

Pass the event to the parent class for processing
Qlabel::mousemoveevent (event);
}

With the above code, our MyLabel1 has the ability to emit clicked signals, Then we can put him as a component into a qwidget or other container (this part of the content can be in the UI program to promote the normal label through the control to MyLabel1), In the Qwidget class we provide a slot function to handle the mylabel1clicked signal, and then bind the mylabel1clicked signal with the corresponding handler function inside the Qwidget constructor, so that we can achieve the desired effect.

Widget.h

#ifndef Widget_h
#define Widget_h

#include <QWidget>

Namespace Ui {
Class Widget;
}

Class Widget:public Qwidget
{
Q_object

Public
Explicit Widget (Qwidget *parent = 0);
~widget ();

Private
Ui::widget *ui;

Private Slots:
Define slot function processing Lineedit clicked
void handlelmylabel1clicked ();
void handlelabelclicked ();
};

#endif//Widget_h

Widget.cpp

#include "widget.h"
#include "ui_widget.h"
 
Widget::widget (Qwidget *parent):
     qwidget (parent),
    ui (new Ui::widget)
{
    ui- >SETUPUI (this);
    //in the widget's constructor binds the Lineedit clicked Signal
    connect (ui-> Mylabel1_1, SIGNAL (clicked ()), this, SLOT (handlelineeditclicked ()));
    connect (Ui->mylabel1_2, SIGNAL (clicked ()), this, SLOT (handlelabelclicked ()));
}
 
Widget::~widget ()
{
    delete ui;
}
 
void widget::handlelineeditclicked () {
    //in process Lineedit In the clicked function we change the lineEdit background color to red
    ui->lineedit->setstylesheet ("Background-color: Red ");
}
 
void widget::handlelabelclicked () {
    ui->label->setstylesheet ("Background-color:green");
}

In this way, our myLabel1 will have the function of clicked.

Reference: http://www.jyguagua.com/?p=668

Qlabel adding clicked Events

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.