当前位置:网站首页>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
边栏推荐
- Opencv 以指定格式保存图片
- Programming example of STM32 state machine -- fully automatic washing machine (Part 1)
- Unity quickly builds urban scenes
- LeetCode·每日一题·剑指 Offer || 115.重建序列·拓扑排序
- YOLOv3: An Incremental Improvement
- QT signal transmission between multi-level objects signal transmission between multi-level nested class objects
- [STL]优先级队列priority_queue
- Win11更改磁盘驱动器号的方法
- STM32——PWM学习笔记
- Istio三之VirtualService、Gateway、DestinationRule配置使用
猜你喜欢

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

How to install with USB flash disk?

Remember SQL optimization once

YOLOv3: An Incremental Improvement

实现一个方法,找出数组中的第k大和第m大的数字相加之和

Opencv saves pictures in the specified format

Win11 hide input method status bar method
![[untitled]](/img/6f/a2cd98af7a8de469e5311422b48afe.png)
[untitled]

称霸薪酬榜!什么行业大有“钱”途?
![[STL]优先级队列priority_queue](/img/79/d13913cbb9d98f936a9501633b38bf.png)
[STL]优先级队列priority_queue
随机推荐
Opening method of win11 microphone permission
Multithreaded programming
[STL]优先级队列priority_queue
tensorflow中tf.Variable()函数的用法
Opencv annotates the image (picture frame + writing)
ES6 set and map
snownlp库各功能及用法
Pit trodden when copying list: shallow copy and deep copy
Opencv saves pictures in the specified format
2022-07-21 第四小组 多态
Implement a method to find the sum of the number k and m in the array
Summary of Huawei virtualization fusioncompute knowledge points
Unity快速搭建城市场景
How to install with USB flash disk?
实现一个方法,找出数组中的第k大和第m大的数字相加之和
Win11隐藏输入法状态栏方法
[SQL] CASE表达式
[sql] usage of self connection
There are a group of students in the class who have got the test results in Chinese and mathematics. Please select the students whose total score is the first
Qt 信号在多层次对象间传递 多层嵌套类对象之间信号传递