当前位置:网站首页>【QT源代码复用】模拟QCompleter的弹窗方式
【QT源代码复用】模拟QCompleter的弹窗方式
2022-07-22 09:12:00 【氺月洞天】
#ifndef LDS_LINEEDIT_COMPLETER_H
#define LDS_LINEEDIT_COMPLETER_H
#include <QLineEdit>
class lds_lineedit_completer_inter;
class lds_lineedit_completer : public QLineEdit
{
Q_OBJECT
public:
explicit lds_lineedit_completer(QWidget *parent = nullptr);
void setCompleterTop(QWidget *popup);
private:
lds_lineedit_completer_inter *obj;
};
class lds_lineedit_completer_inter : public QObject{
Q_OBJECT
public:
explicit lds_lineedit_completer_inter(QObject *parent = nullptr);
~lds_lineedit_completer_inter();
void setPopup(QWidget *popup, QLineEdit *widget);
protected:
bool eventFilter(QObject *o, QEvent *e);
private:
void showPopup();
private:
QWidget *popup;
QLineEdit *widget;
bool eatFocusOut = false;
};
#endif // LDS_LINEEDIT_COMPLETER_H
#include "lds_lineedit_completer.h"
#include <QEvent>
#include <QKeyEvent>
lds_lineedit_completer::lds_lineedit_completer(QWidget *parent) : QLineEdit(parent)
{
obj = new lds_lineedit_completer_inter(this);
}
void lds_lineedit_completer::setCompleterTop(QWidget *popup)
{
obj->setPopup(popup, this);
}
lds_lineedit_completer_inter::lds_lineedit_completer_inter(QObject *parent)
: QObject(parent)
{
}
lds_lineedit_completer_inter::~lds_lineedit_completer_inter()
{
delete popup;
}
void lds_lineedit_completer_inter::setPopup(QWidget *popup, QLineEdit *widget)
{
this->popup = popup;
this->widget = widget;
widget->installEventFilter(this);
popup->installEventFilter(this);
Qt::FocusPolicy origPolicy = widget->focusPolicy();
popup->setParent(nullptr, Qt::Popup);
popup->setFocusPolicy(Qt::NoFocus);
popup->setFocusProxy(widget);
widget->setFocusPolicy(origPolicy);
}
bool lds_lineedit_completer_inter::eventFilter(QObject *o, QEvent *e)
{
if(eatFocusOut && o == widget && e->type() == QEvent::FocusOut) {
if (popup && popup->isVisible())
return true;
}
switch (e->type()) {
case QEvent::KeyPress :{
QKeyEvent *ke = static_cast<QKeyEvent *>(e);
const int key = ke->key();
switch (key) {
case Qt::Key_End:
case Qt::Key_Home:
case Qt::Key_Up:
case Qt::Key_Down:
case Qt::Key_PageUp:
case Qt::Key_PageDown:
return false;
case Qt::Key_F4:
if (ke->modifiers() & Qt::AltModifier)
popup->hide();
return false;
case Qt::Key_Backtab:
case Qt::Key_Escape:
popup->hide();
return false;
}
if(!popup->isVisible()) {
showPopup();
}
if(o != widget) {
eatFocusOut = false;
(static_cast<QObject *>(widget))->event(ke);
eatFocusOut = true;
}
break;
}
case QEvent::MouseButtonPress: {
if (!popup->underMouse()) {
popup->hide();
}
break;
}
case QEvent::InputMethod:
case QEvent::ShortcutOverride:
// QApplication::sendEvent(widget, e);
return false;
default:
return false;
}
return false;
}
void lds_lineedit_completer_inter::showPopup()
{
popup->move(widget->mapToGlobal(QPoint(0, 0)) - QPoint(0, popup->height()));
popup->show();
}
边栏推荐
- 05. Law of Demeter LOD
- centos7.5下添加gd库然后mysql拓展库没了mysql拓展的配置也没问题,phpinfo中就是没有mysql拓展
- 02. Dependency Inversion Principle
- 总结20220119
- Codeforces Round #799 (Div. 4)(8/8)
- Node generates and verifies tokens (typescript syntax)
- 07.合成复用原则(Composite/Aggregate Reuse Principle,CARP)
- 【SDIO】SD2.0协议分析总结(三)-- SD卡相关命令介绍
- Try kolla-ansible (by quqi99)
- 折腾凤凰系统 (by quqi99)
猜你喜欢
随机推荐
【Nordic】nRF52810 OTA升级(二)–DFU如何使用
ECSHOP configures wechat payment, and the wechat developer tool prompts "unbound web developer" when wechat payment pops up
Is it really necessary to define VO, Bo, Po, do, dto?
[SDIO] sd2.0 protocol analysis summary (I) -- Introduction to SD card basic probability
Js高级-基本数据类型与引用数据类型
[audio] analysis and summary of PCM audio data transmitted by I2S (II)
call()和apply()
华为手机锁定应用
amh多mysql版本共存?
Go 语言学习:Go 语言之旅(三)
函数的简介
【Audio】基于STM32 I2S移植WM8978 Audio Codec驱动
charm zaza functional test (by quqi99)
Reflection + annotation + generics
【STM32】STM32 SDIO SD卡读写测试(二)-- SD_Init之Power On阶段
PHP开发中csrf攻击的简单演示和防范
SQL injection
ecshop怎么在本地跑超级慢?
Try kolla-ansible (by quqi99)
Simple demonstration and prevention of CSRF attack in PHP development