Skip to content

Commit 803474d

Browse files
committed
TreeView
1 parent f4e1659 commit 803474d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+1756
-1084
lines changed

Qt-Examples.pro

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ SUBDIRS += \
2121
SimpleUdp \
2222
SlipButton \
2323
SqlTabview \
24-
TabViewModel \
24+
TableViewModel \
2525
Thread \
2626
TreeViewModel
2727

README.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -80,18 +80,21 @@
8080

8181
## 十九、[SqlTabview](SqlTabview/)——SQLite数据库调用,模型方法;
8282

83-
## 二十、[TabViewModel](TabViewModel/)——表格视图,DeleGate(Button and ProgressBar)(MVC);
83+
## 二十、[TableViewModel](TableViewModel/)——表格视图,DeleGate(Button and ProgressBar)(MVC);
8484

8585
套用了旧的样式(懒),具体可以自己调整;
8686

87-
<div align=center><img src="TabViewModel/picture/TabViewModelDelegate.png"></div>
87+
<div align=center><img src="TableViewModel/picture/TabViewModelDelegate.png"></div>
8888

8989
## 二十一、[Thread](Thread/)——多线程例子,四种写法,控件更新在子线程;
9090

9191
<div align=center><img src="Thread/picture/Thread.png"></div>
9292

9393
## 二十二、[TreeViewModel](TreeViewModel/)——树形视图(MVC),QtCreator源码;
9494

95+
<div align=center><img src="TreeViewModel/picture/TreeView.png"></div>
96+
<div align=center><img src="TreeViewModel/picture/ListView.png"></div>
97+
9598
## 二十三、[Scripts](Scripts/)——脚本
9699

97100
1. [macos](Scripts/macos/)——macos 通用编译打dmg包脚本(`python`/`dmgbuild`);

TabViewModel/main.cpp

-24
This file was deleted.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

TableViewModel/main.cpp

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include "mainwindow.h"
2+
3+
#include <QApplication>
4+
#include <QFile>
5+
6+
int main(int argc, char *argv[])
7+
{
8+
QApplication a(argc, argv);
9+
10+
// 套用了旧的样式,具体细节自己调整
11+
// const QString filePath("D:/Mine/CODE/Qt/Qt-App/resource/qss/base/Common.css");
12+
// if(QFile::exists(filePath)){
13+
// QFile file(filePath);
14+
// if(file.open(QFile::ReadOnly)){
15+
// qApp->setStyleSheet(file.readAll());
16+
// file.close();
17+
// }
18+
// }
19+
20+
MainWindow w;
21+
w.show();
22+
23+
return a.exec();
24+
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

TabViewModel/studenttablemodel.cpp TableViewModel/studenttablemodel.cpp

+13-13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#include "studenttablemodel.h"
22

3+
#include <QApplication>
4+
#include <QStyle>
5+
36
enum Property { ID, NAME, AGE, GENDER, ACHIEVEMENT, MENUBUTTON, PROCESS, RICHTEXT };
47

58
QVariant StuedentTableModel::data(const QModelIndex &index, int role) const
@@ -14,9 +17,13 @@ QVariant StuedentTableModel::data(const QModelIndex &index, int role) const
1417
switch (role) {
1518
case Qt::TextAlignmentRole: return Qt::AlignCenter;
1619
case Qt::CheckStateRole:
17-
switch (col) {
18-
case ID: return stu.checked();
19-
default: break;
20+
if (ID == col) {
21+
return stu.checked() ? Qt::Checked : Qt::Unchecked;
22+
}
23+
break;
24+
case Qt::DecorationRole:
25+
if (ID == col) {
26+
return qApp->style()->standardIcon(QStyle::SP_ComputerIcon);
2027
}
2128
break;
2229
case Qt::WhatsThisRole:
@@ -34,12 +41,7 @@ QVariant StuedentTableModel::data(const QModelIndex &index, int role) const
3441
case RICHTEXT: return stu.richText();
3542
default: break;
3643
}
37-
case Qt::UserRole: {
38-
switch (col) {
39-
case MENUBUTTON: return QVariant::fromValue(stu);
40-
default: break;
41-
}
42-
}
44+
case Qt::UserRole: return QVariant::fromValue(stu);
4345
}
4446
default: break;
4547
}
@@ -57,12 +59,10 @@ bool StuedentTableModel::setData(const QModelIndex &index, const QVariant &value
5759
Student stu = m_students.at(row);
5860
switch (role) {
5961
case Qt::CheckStateRole:
60-
switch (col) {
61-
case ID:
62-
stu.setChecked(!stu.checked());
62+
if (ID == col) {
63+
stu.setChecked(value.toInt());
6364
emit dataChanged(index, index);
6465
return true;
65-
default: break;
6666
}
6767
break;
6868
case Qt::EditRole:
File renamed without changes.

TabViewModel/stuedenttable.cpp TableViewModel/stuedenttable.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ StudentsTable::StudentsTable(QWidget *parent)
3535
setItemDelegateForColumn(6, new ProgressBarDelegate(this));
3636
setItemDelegateForColumn(7, new RichTextItemDelegate(this));
3737
setSortingEnabled(true);
38+
setIconSize(QSize(20, 20));
3839

3940
initMenu();
4041
}
File renamed without changes.

TreeViewModel/TreeViewModel.pro

+19-18
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,32 @@ QT += core gui
22

33
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
44

5-
CONFIG += c++11
5+
CONFIG += c++17
66

7-
# The following define makes your compiler emit warnings if you use
8-
# any Qt feature that has been marked deprecated (the exact warnings
9-
# depend on your compiler). Please consult the documentation of the
10-
# deprecated API in order to know how to port your code away from it.
11-
DEFINES += QT_DEPRECATED_WARNINGS
12-
13-
# You can also make your code fail to compile if it uses deprecated APIs.
7+
# You can make your code fail to compile if it uses deprecated APIs.
148
# In order to do so, uncomment the following line.
15-
# You can also select to disable deprecated APIs only up to a certain version of Qt.
169
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
1710

1811
SOURCES += \
19-
main.cpp \
20-
mainwindow.cpp \
21-
studentmodel.cpp \
22-
treeitem.cpp \
23-
treemodel.cpp
12+
checkboxheaderview.cc \
13+
fileinfo.cc \
14+
fileitem.cc \
15+
listview.cc \
16+
main.cc \
17+
mainwindow.cc \
18+
normaltreeview.cc \
19+
treemodel.cc \
20+
treeview.cc
2421

2522
HEADERS += \
26-
mainwindow.h \
27-
studentmodel.h \
28-
treeitem.h \
29-
treemodel.h
23+
checkboxheaderview.hpp \
24+
fileinfo.hpp \
25+
fileitem.hpp \
26+
listview.hpp \
27+
mainwindow.hpp \
28+
normaltreeview.hpp \
29+
treemodel.hpp \
30+
treeview.hpp
3031

3132
# Default rules for deployment.
3233
qnx: target.path = /tmp/$${TARGET}/bin

TreeViewModel/checkboxheaderview.cc

+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
#include "checkboxheaderview.hpp"
2+
3+
#include <QtWidgets>
4+
5+
class CheckBoxHeaderView::CheckBoxHeaderViewPrivate
6+
{
7+
public:
8+
CheckBoxHeaderViewPrivate(QWidget *parent)
9+
: owner(parent)
10+
{
11+
checkBox = new QCheckBox(owner);
12+
checkBox->setVisible(false);
13+
}
14+
15+
QWidget *owner;
16+
17+
QCheckBox *checkBox;
18+
bool checked = false;
19+
bool pressed = false;
20+
bool tristate = false;
21+
bool noChange = false;
22+
QRect checkBoxRect = QRect(20, 6, 18, 18);
23+
};
24+
25+
CheckBoxHeaderView::CheckBoxHeaderView(Qt::Orientation orientation, QWidget *parent)
26+
: QHeaderView(orientation, parent)
27+
, d_ptr(new CheckBoxHeaderViewPrivate(this))
28+
{
29+
setMouseTracking(true);
30+
setSectionsClickable(true);
31+
setDefaultAlignment(Qt::AlignVCenter);
32+
setSortIndicatorShown(false);
33+
setFixedHeight(30);
34+
setStyleSheet(
35+
"QHeaderView{background: #F4F7FC;color: #222222;border-top: 1px solid#F7F7F7;}"
36+
"QHeaderView::section:first{background: #F4F7FC;padding-left:50px;color: #222222;}"
37+
"QHeaderView::section{border: 1px solid #F7F7F7;background: "
38+
"#F4F7FC;padding-left:10px;color: #222222;}");
39+
}
40+
41+
CheckBoxHeaderView::~CheckBoxHeaderView() {}
42+
43+
void CheckBoxHeaderView::setState(int state)
44+
{
45+
if (state == Qt::PartiallyChecked) {
46+
d_ptr->tristate = true;
47+
d_ptr->noChange = true;
48+
} else {
49+
d_ptr->noChange = false;
50+
}
51+
52+
d_ptr->checked = (state != Qt::Unchecked);
53+
updateSection(0);
54+
}
55+
56+
void CheckBoxHeaderView::mousePressEvent(QMouseEvent *event)
57+
{
58+
int index = logicalIndexAt(event->pos());
59+
if (index == 0 && event->button() & Qt::LeftButton
60+
&& d_ptr->checkBoxRect.contains(event->pos())) {
61+
d_ptr->pressed = true;
62+
return;
63+
}
64+
QHeaderView::mousePressEvent(event);
65+
}
66+
67+
void CheckBoxHeaderView::mouseReleaseEvent(QMouseEvent *event)
68+
{
69+
if (d_ptr->pressed) {
70+
if (d_ptr->tristate && d_ptr->noChange) {
71+
d_ptr->checked = true;
72+
d_ptr->noChange = false;
73+
} else {
74+
d_ptr->checked = !d_ptr->checked;
75+
}
76+
77+
updateSection(0);
78+
79+
Qt::CheckState state;
80+
state = d_ptr->checked ? Qt::Checked : Qt::Unchecked;
81+
82+
emit stateChanged(state);
83+
} else {
84+
QHeaderView::mouseReleaseEvent(event);
85+
}
86+
87+
d_ptr->pressed = false;
88+
}
89+
90+
void CheckBoxHeaderView::paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const
91+
{
92+
painter->save();
93+
QHeaderView::paintSection(painter, rect, logicalIndex);
94+
painter->restore();
95+
96+
if (logicalIndex == 0) {
97+
paintCheckBox(painter, rect);
98+
}
99+
}
100+
101+
void CheckBoxHeaderView::paintCheckBox(QPainter *painter, const QRect &rect) const
102+
{
103+
QStyleOptionButton option;
104+
option.initFrom(this);
105+
106+
if (d_ptr->checked)
107+
option.state |= QStyle::State_Sunken;
108+
109+
if (d_ptr->tristate && d_ptr->noChange)
110+
option.state |= QStyle::State_NoChange;
111+
else
112+
option.state |= d_ptr->checked ? QStyle::State_On : QStyle::State_Off;
113+
114+
//QCheckBox checkBox;
115+
option.iconSize = QSize(18, 18);
116+
option.rect = QRect(rect.x() + 20, 6, 18, 18);
117+
d_ptr->checkBoxRect = option.rect;
118+
style()->drawPrimitive(QStyle::PE_IndicatorCheckBox, &option, painter, d_ptr->checkBox);
119+
}

TreeViewModel/checkboxheaderview.hpp

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#ifndef CHECKBOXHEADERVIEW_HPP
2+
#define CHECKBOXHEADERVIEW_HPP
3+
4+
#include <QHeaderView>
5+
6+
class CheckBoxHeaderView : public QHeaderView
7+
{
8+
Q_OBJECT
9+
public:
10+
explicit CheckBoxHeaderView(Qt::Orientation orientation = Qt::Horizontal,
11+
QWidget *parent = nullptr);
12+
~CheckBoxHeaderView();
13+
14+
void setState(int state);
15+
16+
signals:
17+
void stateChanged(bool);
18+
19+
protected:
20+
void mousePressEvent(QMouseEvent *event) override;
21+
void mouseReleaseEvent(QMouseEvent *event) override;
22+
void paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const override;
23+
24+
private:
25+
void paintCheckBox(QPainter *painter, const QRect &rect) const;
26+
27+
class CheckBoxHeaderViewPrivate;
28+
QScopedPointer<CheckBoxHeaderViewPrivate> d_ptr;
29+
};
30+
31+
#endif // CHECKBOXHEADERVIEW_HPP

TreeViewModel/fileinfo.cc

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include "fileinfo.hpp"
2+
3+
QString bytesToString(qint64 bytes)
4+
{
5+
static const double KB = 1024 * 1.0;
6+
static const double MB = KB * 1024;
7+
static const double GB = MB * 1024;
8+
static const double TB = GB * 1024;
9+
10+
if (bytes / TB >= 1) {
11+
return QString("%1TB").arg(QString::number(bytes / TB, 'f', 1));
12+
} else if (bytes / GB >= 1) {
13+
return QString("%1GB").arg(QString::number(bytes / GB, 'f', 1));
14+
} else if (bytes / MB >= 1) {
15+
return QString("%1MB").arg(QString::number(bytes / MB, 'f', 1));
16+
} else if (bytes / KB >= 1) {
17+
return QString("%1KB").arg(qint64(bytes / KB));
18+
}
19+
return QString("%1B").arg(bytes);
20+
}

0 commit comments

Comments
 (0)