#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"
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_shadow, top);
pa
没有合适的资源?快使用搜索试试~ 我知道了~
自定义QTreeView(详解)
共50个文件
png:20个
cpp:6个
h:6个
5星 · 超过95%的资源 需积分: 44 1.5k 下载量 188 浏览量
2014-12-23
18:40:01
上传
评论 33
收藏 1.1MB RAR 举报
温馨提示
详细介绍QTreeView的使用, 包括:模型/视图,自定义委托、自定义样式等操作 以及无边框界面的拖拽等操作!
资源推荐
资源详情
资源评论
收起资源包目录
TreeView.rar (50个子文件)
TreeView
treeitem.h 555B
itemdelegate.h 901B
resource.h 495B
treeitem.cpp 837B
Resource.qrc 817B
mainwindow.cpp 11KB
TreeView.pro.user 17KB
treemodel.cpp 4KB
Bin
TreeView.exe_manifest.res 704B
TreeView.pdb 1.68MB
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
TreeView.exe 76KB
TreeViewd.exe_manifest.rc 115B
TreeViewd.exe_manifest.res 704B
TreeView.ilk 1.13MB
TreeViewd.pdb 1.82MB
TreeViewd.ilk 1.73MB
TreeViewd.exe 144KB
TreeView.exe_manifest.rc 121B
TreeViewd.exe.embed.manifest 638B
util.cpp 2KB
main.cpp 589B
TreeView.rc 2KB
TreeView.pro 794B
mainwindow.h 874B
util.h 308B
treemodel.h 1KB
itemdelegate.cpp 3KB
共 50 条
- 1
一去丶二三里
- 粉丝: 6w+
- 资源: 26
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
- 5
- 6
前往页