#include "mainwindow.h"
#include "treemodel.h"
#include <QHeaderView>
#include <QFile>
#include <QHBoxLayout>
#include <QApplication>
#include <QDesktopWidget>
#include <QPainter>
#include <QListView>
#include <WindowsX.h>
#include "ItemDelegate.h"
#include "WinUser.h"
MainWindow::MainWindow(QWidget *parent) :
QWidget(parent),
m_height(30),
m_shadowSize(5),
m_menu(NULL)
{
this->setWindowFlags(Qt::FramelessWindowHint | Qt::Window);
this->setAttribute(Qt::WA_TranslucentBackground);
this->setWindowIcon(QIcon("./Resources/Images/icon.png"));
m_comboBox = new QComboBox(this);
QStringList strList;
strList << QStringLiteral("样式一") << QStringLiteral("样式二") << QStringLiteral("样式三");
for (int i = 0; i < strList.count(); ++i)
{
m_comboBox->addItem(strList.at(i));
}
m_comboBox->setFixedSize(100, 25);
m_comboBox->setView(new QListView());
connect(m_comboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(changeStyle(int)));
m_treeView = new QTreeView(this);
ItemDelegate *delegate = new ItemDelegate(this);
TreeModel *model = new TreeModel(this);
model->setXML(Util::exePath() + "/Resources/ToolsConfig.xml");
m_treeView->setHeaderHidden(true);
m_treeView->setAnimated(true);
m_treeView->setMouseTracking(true);
m_treeView->setExpandsOnDoubleClick(true);
m_treeView->setContextMenuPolicy(Qt::CustomContextMenu);
m_treeView->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
m_treeView->setModel(model);
delegate->setView(m_treeView);
m_treeView->setItemDelegate(delegate);
connect(delegate, SIGNAL(expanded(const QModelIndex &)), this, SLOT(expanded(const QModelIndex &)));
m_label = new QLabel(this);
m_closeButton = new QPushButton(this);
m_closeButton->setFixedSize(27, 22);
m_closeButton->setObjectName("closeButton");
m_closeButton->setToolTip(QStringLiteral("关闭"));
m_label->setObjectName("titleLabel");
m_label->setText(QStringLiteral("自定义TreeView"));
QHBoxLayout *titleLayout = new QHBoxLayout();
titleLayout->addWidget(m_label);
titleLayout->addStretch();
titleLayout->addWidget(m_closeButton);
titleLayout->setSpacing(0);
titleLayout->setContentsMargins(5, 0, 5, 0);
QHBoxLayout *styleLayout = new QHBoxLayout();
styleLayout->addWidget(m_comboBox);
styleLayout->addStretch();
styleLayout->setSpacing(0);
styleLayout->setContentsMargins(20, 0, 0, 0);
QHBoxLayout *centerLayout = new QHBoxLayout();
centerLayout->addWidget(m_treeView);
centerLayout->setSpacing(0);
centerLayout->setContentsMargins(25, 0, 25, 25);
QVBoxLayout *mainLayout = new QVBoxLayout();
mainLayout->addLayout(titleLayout);
mainLayout->addLayout(styleLayout);
mainLayout->addLayout(centerLayout);
mainLayout->setSpacing(20);
mainLayout->setContentsMargins(5, 3, 5, 7);
this->setLayout(mainLayout);
QDesktopWidget *pDesktop = QApplication::desktop();
m_rect = pDesktop->availableGeometry();
connect(m_treeView, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(treeViewCustomContextMenuRequested(QPoint)));
connect(m_closeButton, SIGNAL(clicked()), qApp, SLOT(quit()));
m_treeView->expandAll();
this->changeStyle(0);
}
MainWindow::~MainWindow()
{
}
void MainWindow::changeStyle(int index)
{
QString styleOne = "QTreeView{\
border: 1px solid lightgray;\
}\
QTreeView::item {\
height: 40px;\
border-radius: 2px;\
border: 1px solid transparent;\
background: transparent;\
color: black;\
}\
QTreeView::item:hover {\
border: 1px solid rgb(185, 215, 250);\
}\
QTreeView::item:selected {\
border: 1px solid rgb(170, 190, 230);\
background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,stop: 0 rgb(230, 240, 250), stop: 0.5 rgb(220, 235, 255), stop: 1.0 rgb(210, 230, 255));\
}\
QTreeView::branch:open:has-children {\
image: url(./Resources/Images/branchOpen.png);\
}\
QTreeView::branch:closed:has-children {\
image: url(./Resources/Images/branchClose.png);\
}";
QString styleTwo = "QTreeView{\
border: 1px solid lightgray;\
}\
QTreeView::item {\
height: 40px;\
border-radius: 2px;\
border: 1px solid transparent;\
background: transparent;\
color: black;\
}\
QTreeView::item:has-children {\
border: none;\
border-bottom: 1px solid lightgray;\
}\
QTreeView::item:hover {\
border: 1px solid rgb(170, 190, 230);\
background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,stop: 0 rgb(230, 240, 250), stop: 0.5 rgb(220, 235, 255), stop: 1.0 rgb(210, 230, 255));\
}\
QTreeView::item:selected {\
border: 1px solid rgb(170, 190, 230);\
background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,stop: 0 rgb(230, 240, 250), stop: 0.5 rgb(220, 235, 255), stop: 1.0 rgb(210, 230, 255));\
}\
QTreeView::branch:open:has-children {\
image: url(./Resources/Images/branchOpen2.png);\
}\
QTreeView::branch:closed:has-children {\
image: url(./Resources/Images/branchClose2.png);\
}";
QString styleThree = "QTreeView{\
border: 1px solid lightgray;\
}\
QTreeView::item {\
height: 25px;\
border: none;\
background: transparent;\
color: black;\
}\
QTreeView::item:hover {\
background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,stop: 0 rgb(255, 220, 130), stop: 0.5 rgb(255, 220, 130), stop: 1.0 rgb(255, 230, 150));\
}\
QTreeView::item:selected {\
background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,stop: 0 rgb(255, 200, 100), stop: 0.5 rgb(255, 230, 150), stop: 1.0 rgb(255, 240, 150));\
}\
QTreeView::branch:open:has-children {\
image: url(./Resources/Images/branchOpen.png);\
}\
QTreeView::branch:closed:has-children {\
image: url(./Resources/Images/branchClose.png);\
}";
if (index == 0)
{
m_treeView->setStyleSheet(styleOne);
}
else if (index == 1)
{
m_treeView->setStyleSheet(styleTwo);
}
else
{
m_treeView->setStyleSheet(styleThree);
}
}
void MainWindow::expanded(QModelIndex index)
{
bool bExpanded = m_treeView->isExpanded(index);
if (!bExpanded)
{
m_treeView->expand(index);
}
else
{
m_treeView->collapse(index);
}
}
void MainWindow::paintEvent(QPaintEvent *event)
{
QString strPath = QCoreApplication::applicationDirPath() + "/Resources/Images/windowShadow.png";
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing, true);
QRect titleRect = QRect(5, 3, this->width() - 10, m_height);
QPixmap m_shadow(strPath);
QRect bottom(5, 136, 200, 7);
QRect top(5, 0, 200, 3);
QRect left(0, 3, 5, 133);
QRect right(205, 3, 5, 133);
QRect topRight(205, 0, 5, 3);
QRect topLeft(0, 0, 5, 3);
QRect bottomLeft(0, 136, 5, 7);
QRect bottomRight(205, 136, 5, 7);
QRect tBottom(5, this->height() - 7, this->width() - 10, 7);
QRect tTop(5, 0, this->width() - 10, 3);
QRect tLeft(0, 3, 5, this->height() - 10);
QRect tRight(this->width() - 5, 3, 5, this->height() - 10);
QRect tTopLeft(0, 0, 5, 3);
QRect tTopRight(this->width() - 5, 0, 5, 3);
QRect tBottomLeft(0, this->height() - 7, 5, 7);
QRect tBottomRight(this->width() - 5, this->height() - 7, 5, 7);
painter.drawPixmap(tBottom, m_shadow, bottom);
painter.drawPixmap(tTop, m
没有合适的资源?快使用搜索试试~ 我知道了~
QT Treeview demo
共66个文件
png:40个
cpp:6个
h:6个
5星 · 超过95%的资源 需积分: 50 39 下载量 95 浏览量
2017-11-16
09:55:28
上传
评论
收藏 884KB ZIP 举报
温馨提示
QT Treeview 控件的小Demo,方便大家参考使用,本demo 还有对qss的一些运用。
资源推荐
资源详情
资源评论
收起资源包目录
TreeView.zip (66个子文件)
TreeView
treeitem.h 555B
itemdelegate.h 901B
resource.h 495B
treeitem.cpp 837B
Resources
toolicon
Sina.png 1KB
MJJT.png 5KB
C++.png 4KB
Qt.png 2KB
CSDN.png 864B
Images
icon.png 2KB
arrowBottomHover.png 1KB
arrowBottom.png 1KB
arrowTopHover.png 1KB
close.png 1KB
branchClose.png 4KB
arrowLeftHover.png 1KB
arrowRight.png 1KB
branchOpen.png 4KB
closeHover.png 1KB
arrowLeft.png 1KB
arrowRightHover.png 1KB
exeIcon.ico 4KB
closePressed.png 1KB
arrowTop.png 1KB
windowShadow.png 2KB
Style.qss 7KB
ToolsConfig.xml 3KB
Resource.qrc 817B
mainwindow.cpp 11KB
TreeView.pro.user 23KB
TreeView.pro.user.3.0-pre1 17KB
treemodel.cpp 4KB
Bin
Resources
toolicon
Sina.png 1KB
MJJT.png 5KB
C++.png 4KB
Qt.png 2KB
CSDN.png 864B
Images
icon.png 2KB
arrowBottomHover.png 1KB
arrowBottom.png 1KB
arrowTopHover.png 1KB
close.png 1KB
branchClose.png 4KB
arrowLeftHover.png 1KB
arrowRight.png 1KB
branchOpen.png 4KB
closeHover.png 1KB
arrowLeft.png 1KB
arrowRightHover.png 1KB
exeIcon.ico 4KB
closePressed.png 1KB
arrowTop.png 1KB
windowShadow.png 2KB
Style.qss 7KB
ToolsConfig.xml 3KB
treeviewd.pdb 3.04MB
TreeViewd.ilk 0B
TreeViewd.exe 176KB
util.cpp 2KB
main.cpp 589B
TreeView.rc 2KB
TreeView.pro 830B
mainwindow.h 874B
util.h 308B
treemodel.h 1KB
itemdelegate.cpp 3KB
共 66 条
- 1
资源评论
- 雒珣2019-12-05不错的资源,可以用,谢谢分享,我用的事QT5.5版本打开的,给其他人参考下
芹菜萝卜
- 粉丝: 2
- 资源: 5
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 冯璐阳 42105650—祝福.docx
- 基于多种算法及改进算法实现的移动机器人路径规划matlab源码(含A星算法+PRM+RRT的改进等).zip
- 布里斯托尔纸细分市场、总体规模、先进性、市占率行业分析报告2024年.docx
- Obi绳子插件,好用的很 6.5.4版本
- openjfx-22.0.1-windows-x64-bin-sdk.zip
- 基于ros和stm32f1的小车代码(含串口通信)+项目说明.zip
- 人体姿态估计-基于Tensorflow实现的人体姿态估计算法-附项目源码-优质项目分享.zip
- java实现所有算法大全
- JDBC DAO模式 (复习)
- Proteus仿真AT89C51电子密码锁
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功