QT 為QPushButton、QLabel添加鼠標移入移出事件
**要實現的效果:**鼠標移入QPushButton時與移出時按鈕變換字體顏色,鼠標移入QLabel時顯示上面的文字,移出時不顯示。
**方法:**由於Qt自帶的QPushButton和QLabel沒有鼠標事件這一屬性,我們需要重新定義兩個類別,分別繼承自QPushButton和QLabel,然后在新的類別里面再重寫鼠標移入移出事件。
下面我新建了一個名字是 a 的工程,然后在頭文件和源文件里分別添加mybutton.h mylabel.h和mybutton.cpp mylabel.cpp,分別新定義了MyButton類和MyLabel類。
工程文件列表如下:
mybutton.h
#ifndef MYBUTTON_H
#define MYBUTTON_H
#include<QPushButton>
#include<QEvent>
class MyButton :public QPushButton
{
Q_OBJECT;
public:
MyButton(QWidget *parent = 0);
~MyButton();
public:
void enterEvent(QEvent *e); //鼠標進入事件
void leaveEvent(QEvent *e);//鼠標離開事件
};
#endif // MYBUTTON_H
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
mylabel.h
#ifndef MYLABEL_H
#define MYLABEL_H
#include<QLabel>
#include<QEvent>
class MyLabel :public QLabel
{
Q_OBJECT;
public:
MyLabel(QWidget *parent = 0);
~MyLabel();
public:
void enterEvent(QEvent *e);//鼠標進入事件
void leaveEvent(QEvent *e);//鼠標離開事件
};
#endif // MYLABEL_H
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include "mybutton.h"
#include "mylabel.h"
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
MyButton *button1, *button2;
MyLabel *myLabel;
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
mybutton.cpp
#include"mybutton.h"
MyButton::MyButton(QWidget* parent) :QPushButton(parent)
{
}
MyButton::~MyButton()
{
}
void MyButton::enterEvent(QEvent *e)
{
setStyleSheet("color:rgb(255,255,0)"); //設置背景顏色
}
void MyButton::leaveEvent(QEvent *e)
{
setStyleSheet("color:rgb(0,0,0)"); //設置背景顏色
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
10
11
12
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),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
button1= new MyButton(this);
button2= new MyButton(this);
button1->setText("one");
button2->setText("two");
button1->setGeometry(QRect(200,200,80,30)); //設置位置和大小
button2->setGeometry(QRect(300,200,80,30));//設置位置和大小
button1->show(); //顯示控件
button2->show(); //顯示控件
myLabel= new MyLabel(this);
myLabel->setStyleSheet("background-color:rgb(100,150,150)"); //設置背景顏色
myLabel->setGeometry(QRect(100,100,300,80)); //設置位置和大小
myLabel->show(); //顯示控件
}
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();
return a.exec();
}
1
2
3
4
5
6
7
8
9
10
11
最后實現的效果如圖,鼠標在按鈕上時按鈕上字體顏色為黃色,鼠標不在按鈕上時按鈕字體為黑色;鼠標在myLabel上時顯示文字,不在時不顯示。
(貌似截屏沒有顯示出鼠標指針位置)
Promote to 方法: 上面是只用代碼的方法,也可以用提升(promote to)的方法,比如在寫好了mylabel.h和mylabel.cpp文件后,在UI界面上添加一個QLabel控件(因為MyLabel類繼承自QLabel),然后右鍵單擊該控件,選擇"promote to":
輸入自己新建的類,以及該類所在的頭文件,點擊”Add“,勾選復選框,再點擊右下角 Promote
可以看到它的類別已經成了MyLabel類:
為方便觀察,我們可以把背景顏色改一下(去掉了文字):
結果如圖:
還存在的問題:如果我把按鈕或者QLabel的鼠標事件函數里寫上setVisuable(true)和setVisuable(false),就是讓控件顯示或者消失,就會出現指針移動時閃爍的問題。
比如改動一下:
mylabel.cpp
#include "mylabel.h"
MyLabel::MyLabel(QWidget* parent) :QLabel(parent)
{
}
MyLabel::~MyLabel()
{
}
void MyLabel::enterEvent(QEvent *e)
{
setVisuable(false);
}
void MyLabel::leaveEvent(QEvent *e)
{
setVisuable(true);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
這樣當鼠標指針在label上移動時,控件會一直在閃爍,對於按鈕控件也是如此,不知道為什么。
有知道的歡迎交流一下!
---------------------
