当前位置:网站首页>QT notes - temporary floating window
QT notes - temporary floating window
2022-07-26 03:14:00 【Cool breeze in the old street °】
demand : When our mouse goes to a button , We need a temporary window , And you can control the temporary window
At the beginning, our solution was : Let's write down The event of entering the mouse , Then write the event of leaving
It can be easily completed , But some problems have been found in the actual operation , It doesn't work on temporary windows
Solution : At the time of leaving We use a timer TimerEvent event , Then use the mouse in When we need to display the area , When we leave the area we need We hide , And kill the timer
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; // Display sub interface
};
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");
}
Effect picture of success :
Reference blog :
QT Floating window correlation
边栏推荐
- 2022-07-21 第四小组 修身课 学习笔记(every day)
- [C language] deeply understand integer lifting and arithmetic conversion
- [sql] case expression
- js中数组排序的方法有哪些
- tensorflow中tf.Variable()函数的用法
- Functions and usage of snownlp Library
- Managing databases in a hybrid cloud: eight key considerations
- How to correctly calculate the CPU utilization of kubernetes container
- canvas——心电图的设计,以及如何清理画布
- canvas——绘制曲线——挂钟,饼图,五角星
猜你喜欢

STM32——PWM学习笔记

Implement a method to find the sum of the number k and m in the array
![[SQL] 自连接的用法](/img/92/92474343b4b4e6ea60453b4799cb55.jpg)
[SQL] 自连接的用法

Leetcode · daily question · sword finger offer | | 115. reconstruction sequence · topological sorting

FPGA_Vivado软件初次使用流程_超详细

Swin Transformer【Backbone】

Use eventlog analyzer for log forensics analysis
![[sql] usage of self connection](/img/92/92474343b4b4e6ea60453b4799cb55.jpg)
[sql] usage of self connection

如何正确计算 Kubernetes 容器 CPU 使用率

万维网、因特网和互联网的区别
随机推荐
万维网、因特网和互联网的区别
Canvas - ECG design and how to clean the canvas
URDF 语法详解
C language layered understanding (C language function)
Installation and operation of orb-slam2 under ROS
JVM内存模型解析
Unknown-Aware Object Detection:Learning What You Don’t Know from Videos in the Wild(CVPR 2022)
File operation (I) -- File introduction and file opening and closing methods
【C语言】深入理解 整型提升 和 算术转换
LeetCode·
[tensorflow & pytorch] image data enhancement API
tensorflow中tf.Variable()函数的用法
JVM memory model parsing
Swin Transformer【Backbone】
Cloud native guide what is cloud native infrastructure
els 注册窗口类、创建窗口类、显示窗口
canvas——矩形的绘制——柱状图的制作
OxyCon 2022 网络抓取前沿大会即将开启!
Safety margin of mass consumption
els 初始化窗口类