QTreeWidgetQTreeWidget

zz/2024/6/13 21:31:50

QTreeWidget
1.常用属性
2.标题设置
3.内容插入
4.内容选择
5.拖动和删除
6.信号事件
7.样式qss

QTreeWidget 属性

1.header()->setVisible(true)
2.setSortingEnabled点击标题排序
3.setAnimated 动画展开
4.setVerticalScrollBarPolicy setHorizontalScrollBarPolicy滚动条
5.selectionMode 单选,、多选,Ctrl,Shift
6.setSelectionBehavior 选择行,列,一项

QTreeWidget 标题

1.QTreeWidgetItem \*h = null;
2.tree->setHeaderItem(new QTreeWidgetItem());
3.h = tree->headerItem();
4.h->setText(0,QStringLiteral("标题1"))

QTreeWidget插入读取

1.insertTopLevelItem(int index,QTreeWidgetItem \*item)
2.addTopLevelItem(QTreeWidgetItem* item);
3.new QTreeWidgetItem(treeWidget);
4.QTreeWidgetItem \*topLevelItem(int index) const
5.int topLevelItemCount() const;
6.void setItemWidget(QTreeWidgetItem* Item, int column,QWidget \*widget);
7.QList<QTreeWidgetItem*> selectedItems() const
8.void setColumnCount(int columns)

QTreeWidgetItem


1.void addChild(QTreeWidgetItem* child)
2.void addChildren(const QList<QTreeWidgetItem *> &children);
3.void setText(int column,const QString &text)
4.QTreeWidgetItem \*parent() const
5.QTreeWidgetItem \*treeWidget() const

QTreeWidget 槽函数


1.void collapseltem(const QTreeWidgetItem \*item);
2.void expandItem(const QTreeWidgetItem \*item);

QTreeWidget 信号函数


1.currentItemChanged(QTreeWidgetItem \*current QTreeWidgetItem \*previous)
2.itemActivated(QTreeWidgetItem \*item, int column)
3.itemChanged(QTreeWidgetItem \*item, int column)
4.itemClicked(QTreeWidgetItem \*item, int column)
5.itemCollapsed(QTreeWidgetItem \*item);
6.itemDoubleClicked(QTreeWidgetItem \*item,int column);
7.itemEntered(QTreeWidgetItem \*item,int column);
8.itemExpanded(QTreeWidgetItem \*item);
9.itemPressed(QTreeWidgetItem \*item,int column);
10.itemSelectionChanged()

QTreeWidget样式
1.行列样式(颜色交替)
2.选中和悬停样式
3.收缩和展开图片替换
4.标题样式

QTreeWidget 行列样式


setAlternatingRowColors(true)
QTreeView{background-color:#FEFFF7;alternate-background-color:#CBE8D9;
}QTreeWidget 选中和悬停样式QTreeView::item{border:1px solid #C5DAFF;
}QTreeView::item::hover{background:#C5DAFF;
}QTreeView::item::selected{background:#DEBBE8;
}QTreeView::branch:has-sibings:!adjoins-item{border-image:url(vline.png)0;
}QTreeView::branch:has-sibings:adjoins-item{border-image:url(branch-more.png) 0;
}QTreeView::branch:!has-children:!has-siblings:adjoins-item{border-image:url(branch-end.png) 0;
}QTreeView::branch:has-children:!has-siblings:closed,
QTreeView::branch:closed:has-children:has-siblings{border-image:none;image:url(branch-closed.png);
}QTreeView::branch:open:has-children:!has-siblings,
QTreeView::branch:open:has-children:has-siblings{border-image:none;image:url(branch-open.png);
}QTreeWidget标题样式QHeaderView::section{background-color:#FFEOD8;border:1px solid #C5DAFF;
}

widget.cpp


#include "widget.h"
#include "ui_widget.h"
#include <QPushButton>Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);//清理测试的标题,不清理数据//ui->treeWidget->setColumnCount(0); //不会清理ui->treeWidget->setHeaderItem(new QTreeWidgetItem());//清理测试数据ui->treeWidget->clear();//ui->treeWidget->setColumnCount(3);//设置标题,设置三列ui->treeWidget->headerItem()->setText(0,"head1");ui->treeWidget->headerItem()->setText(1,"head2");ui->treeWidget->headerItem()->setText(2,"head3");ui->treeWidget->headerItem()->setText(3,"head4");qDebug()<<"ColumnCount "<<ui->treeWidget->columnCount();//插入数据//结尾插入ui->treeWidget->addTopLevelItem(new QTreeWidgetItem());ui->treeWidget->topLevelItem(0)->setText(0,"tree node1");ui->treeWidget->topLevelItem(0)->setText(1,"tree node1 col2");ui->treeWidget->addTopLevelItem(new QTreeWidgetItem({"tree node2 col1","tree node2 col2"}));//开头插入ui->treeWidget->insertTopLevelItem(0,new QTreeWidgetItem({"tree node1-1 cell","tree node-1 col2"}));//结尾插入ui->treeWidget->insertTopLevelItem(ui->treeWidget->topLevelItemCount(),new QTreeWidgetItem({"tree node and cell"}));//结尾插入QTreeWidgetItem *node = new QTreeWidgetItem(ui->treeWidget);node->setText(0,"new ");//插入五个节点ui->treeWidget->topLevelItem(0)->addChild(new QTreeWidgetItem({"child1"}));ui->treeWidget->topLevelItem(0)->addChild(new QTreeWidgetItem({"child2"}));ui->treeWidget->topLevelItem(0)->addChild(new QTreeWidgetItem({"child3"}));ui->treeWidget->topLevelItem(0)->child(1)->addChild(new QTreeWidgetItem({"grandSon"}));ui->treeWidget->topLevelItem(1)->addChild(new QTreeWidgetItem({"2 - child1"})); ui->treeWidget->topLevelItem(1)->addChild(new QTreeWidgetItem({"2 - child2"}));//插入图标ui->treeWidget->setIconSize(QSize(60,60));ui->treeWidget->topLevelItem(0)->setIcon(0,QIcon("test.jpg"));//插入WidgetQPushButton * but = new QPushButton("Test");ui->treeWidget->setItemWidget(ui->treeWidget->topLevelItem(1),0,but);//    ui->treeWidget->
}void Widget::Click(QTreeWidgetItem *item)
{qDebug()<<"Click "<<item->text(0);
}void Widget::Double(QTreeWidgetItem *item)
{qDebug()<<"Double "<<item->text(0);
}void Widget::Hover(QTreeWidgetItem *item)
{qDebug()<<"Hover "<<item->text(0);//ui->treeWidget->expandItem(item);
}void Widget::Expand(QTreeWidgetItem *item)
{qDebug()<<"Expand "<<item->text(0);//清空历史数据while(item->child(0)>0){item->removeChild(item->child(0));}item->addChild(new QTreeWidgetItem({"new tree1"}));item->addChild(new QTreeWidgetItem({"new tree2"}));item->addChild(new QTreeWidgetItem({"new tree3"}));
}Widget::~Widget()
{delete ui;
}

ui_widget.h


#ifndef UI_WIDGET_H
#define UI_WIDGET_H#include <QtCore/QVariant>
#include <QtWidgets/QApplication>
#include <QtWidgets/QHeaderView>
#include <QtWidgets/QTreeWidget>
#include <QtWidgets/QWidget>QT_BEGIN_NAMESPACEclass Ui_Widget
{
public:QTreeWidget *treeWidget;void setupUi(QWidget *Widget){if (Widget->objectName().isEmpty())Widget->setObjectName(QString::fromUtf8("Widget"));Widget->resize(800, 600);treeWidget = new QTreeWidget(Widget);new QTreeWidgetItem(treeWidget);new QTreeWidgetItem(treeWidget);QTreeWidgetItem *__qtreewidgetitem = new QTreeWidgetItem(treeWidget);QTreeWidgetItem *__qtreewidgetitem1 = new QTreeWidgetItem(__qtreewidgetitem);QTreeWidgetItem *__qtreewidgetitem2 = new QTreeWidgetItem(__qtreewidgetitem1);QTreeWidgetItem *__qtreewidgetitem3 = new QTreeWidgetItem(__qtreewidgetitem2);QTreeWidgetItem *__qtreewidgetitem4 = new QTreeWidgetItem(__qtreewidgetitem3);QTreeWidgetItem *__qtreewidgetitem5 = new QTreeWidgetItem(__qtreewidgetitem4);new QTreeWidgetItem(__qtreewidgetitem5);new QTreeWidgetItem(treeWidget);new QTreeWidgetItem(treeWidget);QTreeWidgetItem *__qtreewidgetitem6 = new QTreeWidgetItem(treeWidget);QTreeWidgetItem *__qtreewidgetitem7 = new QTreeWidgetItem(__qtreewidgetitem6);new QTreeWidgetItem(__qtreewidgetitem7);treeWidget->setObjectName(QString::fromUtf8("treeWidget"));treeWidget->setGeometry(QRect(40, 40, 461, 421));treeWidget->setStyleSheet(QString::fromUtf8("QTreeWidget{\n"
"   alternate-background-color: rgb(255, 255, 164);\n"
"   background-color:#FFEDE7;\n"
"}\n"
"\n"
"QTreeWidget::item:hover{\n"
"   color: rgb(78, 154, 6);\n"
"}\n"
"\n"
"QTreeWidget::item:selected{\n"
"   background-color:#DEDDEA;\n"
"}\n"
"\n"
"QTreeWidget::branch{\n"
"   \n"
"   /*background-color: rgb(92, 53, 102);*/\n"
"}\n"
"\n"
"QHeaderView::selection{\n"
"   background-color: rgb(46, 52, 54);\n"
"   border:1px solid    #c5daff;\n"
"}\n"
"\n"
"QTreeView::branch:has-sibings:!adjoins-item{\n"
"    border-image:url(vline.png) 0;\n"
"}\n"
"\n"
"QTreeView::branch:has-sibings:adjoins-item{\n"
"    border-image:url(branch-more.png) 0;\n"
"}\n"
"\n"
"QTreeView::branch:!has-children:!has-siblings:adjoins-item{\n"
"    border-image:url(branch-end.png) 0;\n"
"}\n"
"\n"
"QTreeView::branch:has-children:!has-siblings:closed,\n"
"QTreeView::branch:closed:has-children:has-siblings{\n"
"    border-image:none;image:url(branch-closed.png);\n"
"}\n"
"\n"
"QTreeView::branch:open:has-children:!has-siblings,\n"
"QTreeView::bran""ch:open:has-children:has-siblings{\n"
"    border-image:none;\n"
"    image:url(branch-open.png);\n"
"}\n"
""));treeWidget->setTabKeyNavigation(false);treeWidget->setDragEnabled(false);treeWidget->setAnimated(true);treeWidget->setAllColumnsShowFocus(false);treeWidget->setWordWrap(false);treeWidget->header()->setCascadingSectionResizes(false);treeWidget->header()->setHighlightSections(false);treeWidget->header()->setProperty("showSortIndicator", QVariant(true));retranslateUi(Widget);QObject::connect(treeWidget, SIGNAL(itemClicked(QTreeWidgetItem*,int)), Widget, SLOT(Click(QTreeWidgetItem*)));QObject::connect(treeWidget, SIGNAL(itemDoubleClicked(QTreeWidgetItem*,int)), Widget, SLOT(Double(QTreeWidgetItem*)));QObject::connect(treeWidget, SIGNAL(itemEntered(QTreeWidgetItem*,int)), Widget, SLOT(Hover(QTreeWidgetItem*)));QObject::connect(treeWidget, SIGNAL(itemExpanded(QTreeWidgetItem*)), Widget, SLOT(Expand(QTreeWidgetItem*)));QMetaObject::connectSlotsByName(Widget);} // setupUivoid retranslateUi(QWidget *Widget){Widget->setWindowTitle(QCoreApplication::translate("Widget", "Widget", nullptr));QTreeWidgetItem *___qtreewidgetitem = treeWidget->headerItem();___qtreewidgetitem->setText(9, QCoreApplication::translate("Widget", "\346\226\260\345\273\272\345\210\227", nullptr));___qtreewidgetitem->setText(8, QCoreApplication::translate("Widget", "\346\226\260\345\273\272\345\210\227", nullptr));___qtreewidgetitem->setText(7, QCoreApplication::translate("Widget", "\346\226\260\345\273\272\345\210\227", nullptr));___qtreewidgetitem->setText(6, QCoreApplication::translate("Widget", "\346\226\260\345\273\272\345\210\227", nullptr));___qtreewidgetitem->setText(5, QCoreApplication::translate("Widget", "\346\226\260\345\273\272\345\210\227", nullptr));___qtreewidgetitem->setText(4, QCoreApplication::translate("Widget", "\346\226\260\345\273\272\345\210\227", nullptr));___qtreewidgetitem->setText(3, QCoreApplication::translate("Widget", "\346\226\260\345\273\272\345\210\227", nullptr));___qtreewidgetitem->setText(2, QCoreApplication::translate("Widget", "3", nullptr));___qtreewidgetitem->setText(1, QCoreApplication::translate("Widget", "2", nullptr));___qtreewidgetitem->setText(0, QCoreApplication::translate("Widget", "1", nullptr));const bool __sortingEnabled = treeWidget->isSortingEnabled();treeWidget->setSortingEnabled(false);QTreeWidgetItem *___qtreewidgetitem1 = treeWidget->topLevelItem(0);___qtreewidgetitem1->setText(2, QCoreApplication::translate("Widget", "13", nullptr));___qtreewidgetitem1->setText(1, QCoreApplication::translate("Widget", "12", nullptr));___qtreewidgetitem1->setText(0, QCoreApplication::translate("Widget", "11", nullptr));QTreeWidgetItem *___qtreewidgetitem2 = treeWidget->topLevelItem(1);___qtreewidgetitem2->setText(0, QCoreApplication::translate("Widget", "\346\226\260\345\273\272\351\241\271\347\233\256", nullptr));QTreeWidgetItem *___qtreewidgetitem3 = treeWidget->topLevelItem(2);___qtreewidgetitem3->setText(0, QCoreApplication::translate("Widget", "\346\226\260\345\273\272\351\241\271\347\233\256", nullptr));QTreeWidgetItem *___qtreewidgetitem4 = ___qtreewidgetitem3->child(0);___qtreewidgetitem4->setText(0, QCoreApplication::translate("Widget", "\346\226\260\345\273\272\345\255\220\351\241\271\347\233\256", nullptr));QTreeWidgetItem *___qtreewidgetitem5 = ___qtreewidgetitem4->child(0);___qtreewidgetitem5->setText(0, QCoreApplication::translate("Widget", "\346\226\260\345\273\272\345\255\220\351\241\271\347\233\256", nullptr));QTreeWidgetItem *___qtreewidgetitem6 = ___qtreewidgetitem5->child(0);___qtreewidgetitem6->setText(0, QCoreApplication::translate("Widget", "\346\226\260\345\273\272\345\255\220\351\241\271\347\233\256", nullptr));QTreeWidgetItem *___qtreewidgetitem7 = ___qtreewidgetitem6->child(0);___qtreewidgetitem7->setText(0, QCoreApplication::translate("Widget", "\346\226\260\345\273\272\345\255\220\351\241\271\347\233\256", nullptr));QTreeWidgetItem *___qtreewidgetitem8 = ___qtreewidgetitem7->child(0);___qtreewidgetitem8->setText(0, QCoreApplication::translate("Widget", "\346\226\260\345\273\272\345\255\220\351\241\271\347\233\256", nullptr));QTreeWidgetItem *___qtreewidgetitem9 = ___qtreewidgetitem8->child(0);___qtreewidgetitem9->setText(0, QCoreApplication::translate("Widget", "\346\226\260\345\273\272\345\255\220\351\241\271\347\233\256", nullptr));QTreeWidgetItem *___qtreewidgetitem10 = treeWidget->topLevelItem(3);___qtreewidgetitem10->setText(0, QCoreApplication::translate("Widget", "\346\226\260\345\273\272\351\241\271\347\233\256", nullptr));QTreeWidgetItem *___qtreewidgetitem11 = treeWidget->topLevelItem(4);___qtreewidgetitem11->setText(0, QCoreApplication::translate("Widget", "\346\226\260\345\273\272\351\241\271\347\233\256", nullptr));QTreeWidgetItem *___qtreewidgetitem12 = treeWidget->topLevelItem(5);___qtreewidgetitem12->setText(2, QCoreApplication::translate("Widget", "23", nullptr));___qtreewidgetitem12->setText(1, QCoreApplication::translate("Widget", "22", nullptr));___qtreewidgetitem12->setText(0, QCoreApplication::translate("Widget", "21", nullptr));QTreeWidgetItem *___qtreewidgetitem13 = ___qtreewidgetitem12->child(0);___qtreewidgetitem13->setText(2, QCoreApplication::translate("Widget", "32", nullptr));___qtreewidgetitem13->setText(1, QCoreApplication::translate("Widget", "31", nullptr));___qtreewidgetitem13->setText(0, QCoreApplication::translate("Widget", "\346\226\260\345\273\272\345\255\220\351\241\271\347\233\256", nullptr));QTreeWidgetItem *___qtreewidgetitem14 = ___qtreewidgetitem13->child(0);___qtreewidgetitem14->setText(2, QCoreApplication::translate("Widget", "42", nullptr));___qtreewidgetitem14->setText(1, QCoreApplication::translate("Widget", "41", nullptr));___qtreewidgetitem14->setText(0, QCoreApplication::translate("Widget", "\346\226\260\345\273\272\345\255\220\351\241\271\347\233\256", nullptr));treeWidget->setSortingEnabled(__sortingEnabled);} // retranslateUi};namespace Ui {class Widget: public Ui_Widget {};
} // namespace UiQT_END_NAMESPACE#endif // UI_WIDGET_H

http://www.ngui.cc/zz/2727414.html

相关文章

JDK 和 JRE 的区别

JRE(Java Runtime Enviroment) 是 Java 的运行环境。面向 Java 程序的使用者&#xff0c;而不是开发者。如果你仅下载并安装了JRE&#xff0c;那么你的系统只能运行 Java 程序。JRE 是运行 Java 程序所必须环境的集合&#xff0c;包含JVM标准实现及 Java 核心类库。它包括 Java…

通信__协议的那点事!!

我们的“协议”&#xff1a; 到目前为止&#xff0c;我们已经简单了解了通信的基本模型&#xff0c;Server—Client模型&#xff0c;这里以简单聊天工具为例&#xff1a;1、服务端启动——2、客户端启动&#xff0c;并试图与服务端建立连接——3、服务端根据条件&#xff08;通…

《Google三大论文》_The Google File System

关于预期和实现&#xff1a; 在Google的面向大规模的数据密集型的分布式运用中&#xff0c;GFS都是关键和基础。同时&#xff0c;开源的hadoop File System 与GFS也是很相似的。 与传统的文件系统相比&#xff0c;GFS在设计上有以下几种特点&#xff1a; 一、组件失效被认为是常…

日记 or 小小说 :想进腾讯的师弟师妹们,别学我 (三)

建议您先看&#xff1a;日记 or 小小说 &#xff1a;想进腾讯的师弟师妹们&#xff0c;别学我 &#xff08;一&#xff09;&#xff08;二&#xff09; ~~~~~~~~~~~~~~~~~~~~~我是分割符号~~~~~~~~~ 每一个值得被历史铭记的时刻总是会出现一些不能用科学来解释的现象的&#xf…

日记 or 小小说 :想进腾讯的师弟师妹们,别学我 (四)

请原谅我用这样的标题将你们的注意力引来&#xff0c;其实这只是因为我想不到更好的标题了。 突然想起来文章可能会涉及到个人隐私&#xff08;虽然我一直宣称是小说&#xff0c;文中人物是虚构的&#xff0c;但骗自己有意思么&#xff1f;&#xff09;&#xff0c;再加上最近发…

突然发现的HDFS与GFS在实现上的一些不同

GFS&#xff1a;Google File System HDFS&#xff1a;Hadoop Distribute File System 首先&#xff0c;有一点要确认的是&#xff0c;作为GFS的一个最重要的实现&#xff0c;HDFS设计目标和GFS是高度一致的。在架构、块大小、元数据等的实现上&#xff0c;HDFS与GFS大致一致。但…

在SP公司打工,服务器端技术很重要

所以&#xff0c;既然要做点事情&#xff0c;先从Tomcat开始:) 新手&#xff0c;不知道怎么写blog&#xff0c;大家看了多包涵&#xff0c;自己努力ing

[FW:] Writing big files to ServletOutputStream

Problem when writing big files to ServletOutputStream 转自&#xff1a; http://www.theserverside.com/discussions/thread.tss?thread_id29261 Posted By: Anand Rajagopal on October 06, 2004 10:18 AM I am using the following code to write files to servlet outp…

创建动态菜单的JSP自定义标签(A Custom JSP Tag Library for Dynamic Menus)

转自&#xff1a; http://www.onjava.com/pub/a/onjava/2003/04/09/dynamic_taglib.html

对收藏夹中AspectJ和Spring结合使用例子(What the teacher said (using AspectJ with Spring part II). )的补充

config.xml简化为&#xff1a;<?xml version"1.0" encoding"UTF-8"?><!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id"…