当前位置:网站首页>Make qcombobox drop down a tree structure list
Make qcombobox drop down a tree structure list
2022-06-09 12:44:00 【litanyuan】
background
In the process of project development, we need to make QComboBox Drop down a tree list , Directly through setModel and setView Set up combox Control can realize , However, when you click the node arrow button, the drop-down box will also be hidden . So it needs to be realized again QComboBox To achieve their own control .
①. adopt QTreeComboBoxView Subclass QTreeWidget class , To rewrite mousePressEvent Events themselves implement the expansion and contraction of nodes , Otherwise, there will be event conflicts .
②. adopt QTreeListComboBox Subclass QComboBox class , To rewrite hidePopup Method , Judge whether the conditions are met .
QTreeComboBoxView
class QTreeComboBoxView : public QTreeWidget
{
Q_OBJECT
public:
QTreeComboBoxView(QWidget *parent = Q_NULLPTR) :QTreeWidget(parent) {
}
~QTreeComboBoxView() {
};
protected:
void mousePressEvent(QMouseEvent *event) override;
signals:
void treeMousePressed(bool inItem);
};
void QTreeComboBoxView::mousePressEvent(QMouseEvent *event)
{
auto curIndex = currentIndex();
auto rect = this->visualRect(curIndex);
auto buttonRect = QRect(rect.left() - 20, rect.top(), 20, rect.height());
if (buttonRect.contains(event->pos()))
{
if (isExpanded(curIndex)) setExpanded(curIndex, false);
else setExpanded(curIndex, true);
emit treeMousePressed( true );
}
else
emit treeMousePressed(false);
}
QTreeListComboBox
class QTreeListComboBox : public QComboBox
{
Q_OBJECT
public:
QTreeListComboBox( QWidget *parent = Q_NULLPTR);
~QTreeListComboBox() {
};
protected:
void hidePopup() override;
private:
bool canHidePopup = true;// Allow shrinkage
public:
QTreeComboBoxView * displayTreeWidget = nullptr;
private:
Ui::QTreeListComboBox ui;
};
QTreeListComboBox::QTreeListComboBox( QWidget *parent)
: QComboBox(parent)
{
ui.setupUi(this);
displayTreeWidget = new QTreeComboBoxView(this);
displayTreeWidget->setSelectionMode(QAbstractItemView::SingleSelection);
displayTreeWidget->setHeaderHidden(true);
this->setModel(displayTreeWidget->model());
this->setView(displayTreeWidget);
connect(displayTreeWidget, &QTreeComboBoxView::treeMousePressed, [&](bool inItem) {
canHidePopup = !inItem;
});
connect(this, static_cast<void(QComboBox::*)(int)>(&QComboBox::activated), this, [&](int index) {
hidePopup();
});
}
void QTreeListComboBox::hidePopup()
{
if (canHidePopup) QComboBox::hidePopup();
}
Set the grouping node to disable selection
item->setFlags(item->flags() & ~Qt::ItemIsSelectable);
Code example
auto tree = ui.comboBox->displayTreeWidget;
for (int i = 1;i < 4;i++ )
{
QTreeWidgetItem* item = new QTreeWidgetItem(tree, {
QString::number(i) });
item->setFlags(item->flags() & ~Qt::ItemIsSelectable);
for (int j = 1; j < 5; j++)
{
QTreeWidgetItem* sub = new QTreeWidgetItem(item, {
QString("%1.%2").arg(i).arg(j) });
}
}

边栏推荐
- go语言time.Format的坑
- Gaussdb (for redis) new feature release: prefix search thousand fold promotion and cluster multi tenancy isolation
- flutter Dio示例
- MySQL 乐观锁、悲观锁、多粒度锁
- GaussDB(for Redis)新特性发布:前缀搜索千倍提升与集群版多租隔离
- curator - 节点类型
- How does PostgreSQL speed up the recovery of transaction IDS through the vacuum
- 推荐的十个Flutter插件
- Detailed explanation of LP mobile mining system development ecosystem
- curator - 创建客户端
猜你喜欢

UDP reliability practices

谁说Redis不能存大key

.NET基础知识快速通关8

Safari的Favorites项不显示在主页上

5. < tag backtracking and cutting problems > lt.93 Restore IP address

C#/VB.NET 在PDF表格中添加条形码

ThreadPoolExecutor from mastery to entry

What is a hard real-time database system?

GameFi新的启程,AQUANEE将于6.9日登陆Gate以及BitMart

Range method returns the object conversion method
随机推荐
死锁的排查工具有哪些?
GDI+ 中图片的绘制
【代码学习】批量提取论文(pdf)的第一页
UDP reliability practices
LR11 installation error: vc2005 is missing on this computer_ sp1_ with_ atl_ fix_ Redist, please install all missing required components, and then run this installation again.
. Net basic knowledge quick pass 10
leetcode 1332. Remove Palindromic Subsequences(删掉回文子串)
Anonymous inner classes and local variables
curator - 创建客户端
You have to understand the underlying MySQL isolation level
道友,你在项目中使用Redis跟三圣母有啥关系?
Error in library(patRoon) : 不存在叫‘patRoon‘这个名字的程辑包
Scientific research paper writing
保姆级教程:如何成为Apache Linkis文档贡献者
keil5mdk安装(免费)
Redis data structure and introduction
他说国外LEAD不好做了
Gson, fastjason, Jackson serialization differences
2. < tag backtracking, combination and pruning > lt.216 Total number of combinations|||
什么是硬实时数据库系统?