前言
想要了解QT中的QPushButoon類必須先了解QT的大致繼承關(guān)系,這樣我們才能更好的實(shí)現(xiàn)或者了解QPushButton按鈕控件,大致繼承關(guān)系如下:
我們可以看到QpushButton繼承自—>QAbstractButton繼承自---->QWidget。
這樣我們就可以查找我們想要實(shí)現(xiàn)的功能相關(guān)信息。
單擊信號(hào):checked。
connect(ui->ok, &QPushButton::clicked, this, &mainWin::slot_clicked);
雖然我們實(shí)現(xiàn)了連接單擊信號(hào),但是我們發(fā)現(xiàn)在QT幫助文檔中,QPushButton中是沒(méi)有clicked這個(gè)信號(hào)的。其實(shí)是QAbstractButton中的信號(hào)。
雙擊信號(hào):mouseDoubleClickEvent。
通過(guò)上文,我們知道單擊信號(hào)是繼承QAbstractButton,那么雙擊信號(hào)是繼承QWidget中,而且有很多都是虛函數(shù),需要使用者自我實(shí)現(xiàn)。下面就是雙擊信號(hào)實(shí)現(xiàn)過(guò)程
添加自定義類,繼承自QPushButton。
.h代碼
#ifndef MYPUSHBUTTON_H
#define MYPUSHBUTTON_H
#include <QPushButton>
namespace Ui {
class MyPushButton;
}
class MyPushButton : public QPushButton
{
Q_OBJECT
public:
explicit MyPushButton(QWidget *parent = nullptr);
~MyPushButton();
protected:
///重寫鼠標(biāo)雙擊事件
void mouseDoubleClickEvent(QMouseEvent *event) override;
private:
Ui::MyPushButton *ui;
};
#endif // MYPUSHBUTTON_H
重寫mouseDoubleClickEvent函數(shù)。
.cpp代碼
#include "MyPushButton.h"
#include "ui_MyPushButton.h"
#include <QMouseEvent>
#include <QMessageBox>
MyPushButton::MyPushButton(QWidget *parent) :
QPushButton(parent),
ui(new Ui::MyPushButton)
{
ui->setupUi(this);
}
MyPushButton::~MyPushButton()
{
delete ui;
}
void mouseDoubleClickEvent(QMouseEvent *event)
{
if (event->buttons() == Qt::LeftButton)
{
QMessageBox::information(NULL, "DoubleClick", "double click", QMessageBox::Yes);
}
}
我們就實(shí)現(xiàn)了雙擊事件。
其實(shí)我們還有很多功能想要實(shí)現(xiàn)的話,就必須去QWidget中查找,下面我給大家列舉常用的幾個(gè)接口:文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-570343.html
名稱 | 常用接口 | 實(shí)現(xiàn)方式 | 繼承自何處 |
---|---|---|---|
鼠標(biāo)按下 | mousePressEvent | 重寫 | QWidget |
– | – | – | |
鼠標(biāo)移動(dòng) | mouseMoveEvent | 重寫 | QWidget |
– | – | – | |
鼠標(biāo)釋放 | mouseReleaseEvent | 重寫 | QWidget |
– | – | – | |
鼠標(biāo)移出 | leaveEvent | 重寫 | QWidget |
– | – | – | |
鼠標(biāo)移入 | enterEvent | 重寫 | QWidget |
那么其他的,自己去QWidget查看把。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-570343.html
到了這里,關(guān)于QPushButton按鈕控件常用信號(hào),雙擊,單擊等。的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!