当前位置:网站首页>QT笔记——临时的悬浮窗口
QT笔记——临时的悬浮窗口
2022-07-26 03:10:00 【旧街凉风°】
需求:当我们的鼠标进入到一个按钮上时,我们需要出现一个临时的窗口,并且可以操控临时窗口
一开始我们的解决思路是: 我们写下 进入鼠标的事件,然后再写离开的事件
就可以很轻松的完成,但是在实际操作过程中发现了一些问题,它并不能操作到临时窗口
解决方案:在离开的时候 我们使用定时器TimerEvent事件 ,然后通过鼠标在 我们需要显示的区域的时候显示,离开我们需要的区域的时候 我们进行隐藏,并且杀死定时器
CustomItemWidget.h
#pragma once
#include <QWidget>
#include "ui_CustomItemWidget.h"
#include "CustomItemSubWidget.h"
#include <QMessageBox>
#include <QDebug>
#include <QTimerEvent>
#include <QEvent>
#include <QRect>
#include <QTimer>
#include <QPoint>
class CustomItemWidget : public QWidget
{
Q_OBJECT
public:
CustomItemWidget(QWidget* parent = Q_NULLPTR);
~CustomItemWidget();
private:
void InitItemWidget();
protected:
virtual void timerEvent(QTimerEvent* event) Q_DECL_OVERRIDE;
virtual bool eventFilter(QObject* watched, QEvent* event)Q_DECL_OVERRIDE;
private:
Ui::CustomItemWidget ui;
int m_timerID;
CustomItemSubWidget *m_customItemSubWidget; //显示子界面
};
CustomItemWidget.cpp
#include "CustomItemWidget.h"
CustomItemWidget::CustomItemWidget(QWidget* parent)
: QWidget(parent)
{
ui.setupUi(this);
InitItemWidget();
}
CustomItemWidget::~CustomItemWidget()
{
}
void CustomItemWidget::InitItemWidget()
{
ui.pushButton_add->installEventFilter(this);
m_customItemSubWidget = new CustomItemSubWidget();
}
bool CustomItemWidget::eventFilter(QObject* watched, QEvent* event)
{
if (watched == ui.pushButton_add)
{
if (event->type() == QEvent::Enter)
{
m_customItemSubWidget->show();
QPoint btnPos(this->mapToGlobal(QPoint(ui.pushButton_add->x(), ui.pushButton_add->y() + ui.pushButton_add->height() / 2)));
m_customItemSubWidget->move(btnPos - QPoint(m_customItemSubWidget->width(), m_customItemSubWidget->height() / 2));
m_timerID = this->startTimer(1000);
return true;
}
}
return QWidget::eventFilter(watched, event);
}
void CustomItemWidget::timerEvent(QTimerEvent* event)
{
int tmp = event->timerId();
if (tmp == m_timerID)
{
if ((m_customItemSubWidget != nullptr) && (m_customItemSubWidget->isVisible()))
{
QPoint mousePoint = QCursor::pos();
QPoint btnPos(this->mapToGlobal(QPoint(ui.pushButton_add->x(), ui.pushButton_add->y() + ui.pushButton_add->height() / 2)));
QPoint rectWidgetTopLeft(btnPos - QPoint(m_customItemSubWidget->width(), m_customItemSubWidget->height() / 2));
QPoint rectWidgetBottomRight(btnPos + QPoint(0, m_customItemSubWidget->height() / 2));
QRect rectWidget(rectWidgetTopLeft, rectWidgetBottomRight);
QPoint rectPushButtonTopLeft(btnPos - QPoint(0, ui.pushButton_add->height() / 2));
QPoint rectPushButtonBottomRight(btnPos + QPoint(ui.pushButton_add->width(), ui.pushButton_add->height() / 2));
QRect rectPushButton(rectPushButtonTopLeft, rectPushButtonBottomRight);
if (!rectWidget.contains(mousePoint) && !rectPushButton.contains(mousePoint))
{
m_customItemSubWidget->hide();
killTimer(tmp);
}
}
else
{
killTimer(tmp);
}
}
}
CustomItemSubWidget .h
#pragma once
#include <QWidget>
#include "ui_CustomItemSubWidget.h"
#include <QComboBox>
class CustomItemSubWidget : public QWidget
{
Q_OBJECT
public:
CustomItemSubWidget(QWidget* parent = Q_NULLPTR);
~CustomItemSubWidget();
private:
void InitComboBoxData();
private:
Ui::CustomItemSubWidget ui;
};
CustomItemSubWidget .cpp
#include "CustomItemSubWidget.h"
CustomItemSubWidget::CustomItemSubWidget(QWidget* parent)
: QWidget(parent)
{
ui.setupUi(this);
InitComboBoxData();
this->setWindowFlags(windowFlags() | Qt::FramelessWindowHint | Qt::Tool);
}
CustomItemSubWidget::~CustomItemSubWidget()
{
}
void CustomItemSubWidget::InitComboBoxData()
{
ui.comboBox_Color->addItem("1");
ui.comboBox_Combination->addItem("1");
ui.comboBox_ManufactureCombination->addItem("1");
ui.comboBox_Manufacturer->addItem("1");
ui.comboBox_MaterialScience->addItem("1");
ui.comboBox_Type->addItem("1");
}
成功效果图:
参考博客:
QT悬浮窗口相关
边栏推荐
猜你喜欢
![[SQL] CASE表达式](/img/05/1bbb0b5099443f7ce5f5511703477e.png)
[SQL] CASE表达式

Standardize your own debug process

Influence of middle tap change on ZVS oscillation circuit

Opencv 以指定格式保存图片

How to reinstall win7 system?

Classic interview questions -- three characteristics of OOP language

c语言分层理解(c语言函数)

使用anaconda配置gpu版本的tensorflow(30系列以下显卡)

重装Win7系统如何进行?

Digital commerce cloud DMS dealer management system solution: DMS system realizes business Omni channel and sales data collection
随机推荐
Method of manually cloning virtual machine in esxi6.7
Istio三之VirtualService、Gateway、DestinationRule配置使用
Oxycon 2022 network capture frontier conference is about to open!
C language layered understanding (C language function)
万维网、因特网和互联网的区别
Canvas - ECG design and how to clean the canvas
Chen Yili, China Academy of communications technology: cost reduction and efficiency increase are the greatest value of Enterprise Cloud native applications
LeetCode·
复制列表时踩过的坑:浅拷贝与深拷贝
Leetcode · daily question · sword finger offer | | 115. reconstruction sequence · topological sorting
canvas——矩形的绘制——柱状图的制作
Canvas - drawing pictures - dynamic drawing production
Swin Transformer【Backbone】
班级里有一群学生考试结果出来了,考了语文和数学两门,请筛选出总分是第一的同学
ENVI_ Idl: create HDF5 file and write data (take writing GeoTIFF file to HDF file as an example) + detailed parsing
Matlab simulation of vertical handover between MTD SCDMA and TD LTE dual networks
Type the URL to the web page display. What happened during this period?
Classic interview questions -- three characteristics of OOP language
一篇文章让你理解 云原生 容器化相关
GoLang日志编程系统