Add mouse moved out of the event

mylabel.cpp

#include "mylabel.h"

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

}
MyLabel::~MyLabel()
{

}
void MyLabel::enterEvent(QEvent *e)
{
setText("Ok, mouse is on");
}
void MyLabel::leaveEvent(QEvent *e)
{
setText(" ");
}

1
2
3
4
5
6
7
8
9
October
November
December
13
14
15
16
17
18
19
mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "mybutton.h"

MainWindow :: MainWindow (QWidget * parent):
QMainWindow (parent),
although (new Ui :: MainWindow)
{
ui-> setupUi (this);

= the MyButton new new Button1 (the this);
Button2 the MyButton new new = (the this);
button1-> the setText ( "One");
button2-> the setText ( "TWO");
button1-> setGeometry (QRect (200,200,80,30)) ; // set the position and size
button2-> setGeometry (QRect (300,200,80,30) ); // set the position and size
button1-> show (); // display control
button2-> show (); // display controls

= new new MyLabel myLabel (the this);
myLabel-> setStyleSheet ( "background-Color: RGB (100,150,150)"); // set the background color
myLabel-> setGeometry (QRect (100,100,300,80) ); // set the position and size
myLabel -> show (); // display controls
}

MainWindow::~MainWindow()
{
delete ui;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
main.cpp

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();

a.exec return ();
}
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
effects the final realization of the figure, the mouse button when the button font color is yellow, the mouse button when the button is not black font; mouse when the display text on myLabel, absence is not displayed.
(Seemingly did not show screenshots of the mouse pointer)

 

Promote to Method: The above method is only code can also be used to enhance (promote to) methods, such as in the written mylabel.h and mylabel.cpp file, add a QLabel control (as in the UI interface class MyLabel inherited from QLabel), then right-click the control and select "promote to":

Enter your new class, and the class header file is located, click on "Add", select the check box, and then click the lower right corner Promote

You can see it has become a category MyLabel categories:

For the convenience of observation, we can change what the background color (remove the text):

The results are shown:


There is also the question: If I put a mouse event or function button QLabel's write setVisuable (true) and setVisuable (false), is to let the control display or disappear, there will be flashing when the pointer moves the problem.
For example, change this:

mylabel.cpp

#include "mylabel.h"

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

}
MyLabel::~MyLabel(http://www.my516.com)
{

}
void MyLabel::enterEvent(QEvent *e)
{
setVisuable(false);
}
void MyLabel::leaveEvent(QEvent *e)
{
setVisuable(true);
}
--------------------- 

Guess you like

Origin www.cnblogs.com/ly570/p/11277955.html