這兩個(gè)功能,有很多辦法可以實(shí)現(xiàn),這里記一下筆者常用的。
新建一個(gè)C++類:mouseHover
mouseHover.h
#ifndef MOUSEHOVER_H
#define MOUSEHOVER_H
#include <QObject>
class mouseHover : public QObject
{
Q_OBJECT
public:
mouseHover();
signals:
void signal_sendBtnObj(QObject*, QString);
protected:
bool eventFilter(QObject *obj, QEvent *event) override;
};
#endif // MOUSEHOVER_H
mouseHover.cpp
#include "mousehover.h"
#include <QEvent>
#include <QDebug>
mouseHover::mouseHover()
{
}
bool mouseHover::eventFilter(QObject *obj, QEvent *event)
{
if (event->type() == QEvent::Enter){//鼠標(biāo)進(jìn)入控件
emit signal_sendBtnObj(obj, obj->objectName());
return true;
}else if (event->type() == QEvent::Leave){//鼠標(biāo)離開控件
emit signal_sendBtnObj(obj, "mouse out");
return true;
}else{
return QObject::eventFilter(obj, event);
}
}
新建一個(gè)Qt界面類:QRImage
QRImage.h
#ifndef QRIMAGE_H
#define QRIMAGE_H
#include <QWidget>
namespace Ui {
class QRImage;
}
class QRImage : public QWidget
{
Q_OBJECT
public:
explicit QRImage(QWidget *parent = nullptr);
~QRImage();
public slots:
void slots_showQR(QString);
private:
Ui::QRImage *ui;
};
#endif // QRIMAGE_H
QRImage.cpp
#include "qrimage.h"
#include "ui_qrimage.h"
#include <QDebug>
QRImage::QRImage(QWidget *parent) :
QWidget(parent),
ui(new Ui::QRImage)
{
ui->setupUi(this);
setWindowFlags(Qt::FramelessWindowHint);//無邊框
}
QRImage::~QRImage()
{
delete ui;
}
void QRImage::slots_showQR(QString mes)
{
if (mes == "quit")
this->close();
else if (mes == "BAIDU")
ui->label->setText("www.baidu.com");
}
主界面
MainWindow.h
#include "mousehover.h"
#include "qrimage.h"
QLabel *label_mes;//一個(gè)標(biāo)簽
QStatusBar *qstBar;//狀態(tài)欄
QRImage *qr;//懸浮窗口
signals:
void signal_showQR(QString);
public slots:
void slot_BtnObj(QObject*, QString);
MainWindow.cpp文章來源:http://www.zghlxwxcb.cn/news/detail-460464.html
/// 鼠標(biāo)懸停
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
qr = new QRImage;//實(shí)例化
connect(this, &MainWindow::signal_showQR, qr, &QRImage::slots_showQR);
mouseHover *mmHover = new mouseHover();//實(shí)例化
connect(mmHover, &mouseHover::signal_sendBtnObj, this, &MainWindow::slot_BtnObj);//連接信號(hào)槽
//狀態(tài)欄
qstBar = this-->statusBar();
label_mes = new QLabel("將鼠標(biāo)移動(dòng)到這里", this);
label_mes->installEventFilter(mmHover);
qstBar->addWidget(label_mes);//添加到狀態(tài)欄左側(cè)
qstBar->addPermanentWidget(label_mes);//添加到狀態(tài)欄右側(cè)
void MainWindow::slot_BtnObj(QObject *objm QString objName)
{
if (objName == "mouse out"){
label_mes->setText("鼠標(biāo)已離開");
emit signal_showQR("quit");
}else{
if (objName == label_mes){
label_mes->setText("鼠標(biāo)已進(jìn)入");
qr->show();
emit signal_showQR("BAIDU");
}
}
}
}
效果
文章來源地址http://www.zghlxwxcb.cn/news/detail-460464.html
到了這里,關(guān)于Qt鼠標(biāo)懸停+懸浮窗口的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!