Skip to content

Commit e0aa362

Browse files
committed
[Qt框架版本更新及代码优化]: 对项目中的Qt版本进行了更新,并对相关代码进行了优化
- 更新Qt版本: 将Qt版本从6.9.0更新至6.9.1,修改了相关配置和脚本中的路径 - 优化buttondelegate: 改进按钮绘制逻辑,移除冗余包含,增强代码可读性 - 调整comboboxdelegate: 优化组合框创建逻辑,采用现代C++变量声明方式 - 重构displaydata: 将DisplayInfo类重命名为Display,引入DisplayList类型定义 - 改进displaytablemodel: 引入PIMPL模式,隐藏实现细节,减少编译依赖 - 优化displaytableview: 调整表格视图初始化逻辑和上下文菜单创建方式 - 重构mainwindow: 采用PIMPL模式隐藏实现细节,优化数据模型操作逻辑 - 调整progressbardelegate: 移除未使用成员变量,优化进度条绘制逻辑 - 改进richtextitemdelegate: 提高富文本绘制效率,优化文本显示和布局
1 parent 9b632ec commit e0aa362

20 files changed

Lines changed: 262 additions & 254 deletions

.github/actions/install-dependencies/action.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ inputs:
99
qt_ver:
1010
description: 'qt version'
1111
required: false
12-
default: '6.9.0'
12+
default: '6.9.1'
1313
type: string
1414

1515
runs:

.vscode/settings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
},
55
"cmake.generator": "Ninja",
66
"cmake.environment": {
7-
"PATH": "C:\\Qt\\6.9.0\\msvc2022_64\\bin;${env:PATH};"
7+
"PATH": "C:\\Qt\\6.9.1\\msvc2022_64\\bin;${env:PATH};"
88
}
99
}

TableViewModel/buttondelegate.cpp

Lines changed: 39 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
#include "buttondelegate.h"
22
#include "displaydata.hpp"
33

4-
#include <QApplication>
5-
#include <QMouseEvent>
6-
#include <QPainter>
74
#include <QtWidgets>
85

96
ButtonDelegate::ButtonDelegate(QObject *parent)
107
: QStyledItemDelegate(parent)
11-
, m_buttonPtr(new QStyleOptionButton)
128
{}
139

1410
ButtonDelegate::~ButtonDelegate() {}
@@ -17,21 +13,28 @@ void ButtonDelegate::paint(QPainter *painter,
1713
const QStyleOptionViewItem &option,
1814
const QModelIndex &index) const
1915
{
20-
int w = qMin(option.rect.width(), option.rect.height()) / 10.0;
21-
m_buttonPtr->rect = option.rect.adjusted(w, w, -w, -w);
22-
m_buttonPtr->text = index.model()->data(index).toString();
23-
m_buttonPtr->state |= QStyle::State_Enabled;
16+
auto copyOption = option;
17+
initStyleOption(&copyOption, index);
2418

25-
painter->save();
19+
const auto *widget = option.widget;
20+
auto *style = widget ? widget->style() : QApplication::style();
2621

27-
if (option.state & QStyle::State_Selected) {
28-
painter->fillRect(option.rect, option.palette.highlight());
29-
painter->setBrush(option.palette.highlightedText());
30-
}
22+
auto rect = option.rect;
23+
auto margin = qMin(rect.width(), rect.height()) / 10.0;
3124

32-
QPushButton button;
33-
qApp->style()->drawControl(QStyle::CE_PushButton, m_buttonPtr.data(), painter, &button);
25+
QStyleOptionButton optionButton;
26+
if (widget) {
27+
optionButton.initFrom(widget);
28+
}
29+
optionButton.state = copyOption.state;
30+
optionButton.direction = copyOption.direction;
31+
optionButton.rect = rect.adjusted(margin, margin, -margin, -margin);
32+
optionButton.fontMetrics = copyOption.fontMetrics;
33+
optionButton.palette = copyOption.palette;
34+
optionButton.text = copyOption.text;
3435

36+
painter->save();
37+
style->drawControl(QStyle::CE_PushButton, &optionButton, painter, widget);
3538
painter->restore();
3639
}
3740

@@ -40,38 +43,33 @@ bool ButtonDelegate::editorEvent(QEvent *event,
4043
const QStyleOptionViewItem &option,
4144
const QModelIndex &index)
4245
{
43-
int w = qMin(option.rect.width(), option.rect.height()) / 10.0;
44-
46+
auto type = event->type();
4547
switch (event->type()) {
46-
case QEvent::MouseButtonPress: {
47-
QMouseEvent *mouseEvent = (QMouseEvent *) event;
48-
if (option.rect.adjusted(w, w, -w, -w).contains(mouseEvent->pos())) {
49-
m_buttonPtr->state |= QStyle::State_Sunken;
50-
}
51-
} break;
5248
case QEvent::MouseButtonRelease: {
53-
QMouseEvent *mouseEvent = (QMouseEvent *) event;
54-
if (option.rect.adjusted(w, w, -w, -w).contains(mouseEvent->pos())) {
55-
m_buttonPtr->state &= (~QStyle::State_Sunken);
49+
auto data = model->data(index, Qt::UserRole).value<DisplayInfo>();
50+
auto details = tr("Title: %1\nNumber: %2\nState: %3\nProcess: %4\nRichText: %5")
51+
.arg(data.title())
52+
.arg(data.number())
53+
.arg(data.state())
54+
.arg(data.process())
55+
.arg(data.richText() + "x");
5656

57-
auto data = model->data(index, Qt::UserRole).value<DisplayInfo>();
58-
auto details = tr("Title: %1\nNumber: %2\nState: %3\nProcess: %4\nRichText: %5")
59-
.arg(data.title())
60-
.arg(data.number())
61-
.arg(data.state())
62-
.arg(data.process())
63-
.arg(data.richText() + "x");
64-
65-
auto w = qobject_cast<QWidget *>(model->parent());
66-
if (w) {
67-
QDialog dialog(w);
68-
QHBoxLayout *layout = new QHBoxLayout(&dialog);
69-
layout->addWidget(new QLabel(details, &dialog));
70-
dialog.exec();
57+
auto *widget = const_cast<QWidget *>(option.widget);
58+
if (widget) {
59+
while (widget->parentWidget()) {
60+
widget = widget->parentWidget();
7161
}
62+
63+
QDialog dialog(widget);
64+
dialog.setMinimumSize(400, 250);
65+
auto *label = new QLabel(details, &dialog);
66+
label->setWordWrap(true);
67+
auto *layout = new QHBoxLayout(&dialog);
68+
layout->addWidget(label);
69+
dialog.exec();
7270
}
7371
} break;
7472
default: break;
7573
}
76-
return true;
74+
return QStyledItemDelegate::editorEvent(event, model, option, index);
7775
}

TableViewModel/buttondelegate.h

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
#ifndef BUTTONDELEGATE_H
2-
#define BUTTONDELEGATE_H
1+
#pragma once
32

43
#include <QStyledItemDelegate>
54

@@ -18,9 +17,4 @@ class ButtonDelegate : public QStyledItemDelegate
1817
QAbstractItemModel *model,
1918
const QStyleOptionViewItem &option,
2019
const QModelIndex &index) -> bool override;
21-
22-
private:
23-
QScopedPointer<QStyleOptionButton> m_buttonPtr;
2420
};
25-
26-
#endif // BUTTONDELEGATE_H

TableViewModel/comboboxdelegate.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,21 @@ QWidget *ComboBoxDelegate::createEditor(QWidget *parent,
88
{
99
static const QStringList items{tr("open"), tr("close")};
1010

11-
auto comboBox = new QComboBox(parent);
11+
auto *comboBox = new QComboBox(parent);
1212
comboBox->addItems(items);
1313
return comboBox;
1414
}
1515

1616
void ComboBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
1717
{
18-
auto comboBox = qobject_cast<QComboBox *>(editor);
18+
auto *comboBox = qobject_cast<QComboBox *>(editor);
1919
comboBox->setCurrentIndex(index.data(Qt::EditRole).toInt());
2020
}
2121

2222
void ComboBoxDelegate::setModelData(QWidget *editor,
2323
QAbstractItemModel *model,
2424
const QModelIndex &index) const
2525
{
26-
auto comboBox = qobject_cast<QComboBox *>(editor);
26+
auto *comboBox = qobject_cast<QComboBox *>(editor);
2727
model->setData(index, comboBox->currentText(), Qt::EditRole);
2828
}

TableViewModel/displaydata.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,6 @@ class DisplayInfo
8181
QExplicitlySharedDataPointer<DisplayData> d_ptr;
8282
};
8383

84+
using DisplayInfoList = QList<DisplayInfo>;
85+
8486
Q_DECLARE_METATYPE(DisplayInfo)

TableViewModel/displaytablemodel.cc

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,39 @@
44
#include <QApplication>
55
#include <QStyle>
66

7+
class DisplayTableModel::DisplayTableModelPrivate
8+
{
9+
public:
10+
explicit DisplayTableModelPrivate(DisplayTableModel *q)
11+
: q_ptr(q)
12+
{
13+
auto metaEnums = QMetaEnum::fromType<Property>();
14+
for (int i = 0; i < metaEnums.keyCount(); ++i) {
15+
headerDatas << metaEnums.key(i);
16+
}
17+
}
18+
19+
DisplayTableModel *q_ptr;
20+
21+
DisplayInfoList datas;
22+
QStringList headerDatas;
23+
};
24+
725
DisplayTableModel::DisplayTableModel(QObject *parent)
826
: QAbstractTableModel(parent)
27+
, d_ptr(new DisplayTableModelPrivate(this))
28+
{}
29+
30+
DisplayTableModel::~DisplayTableModel() {}
31+
32+
auto DisplayTableModel::rowCount(const QModelIndex &) const -> int
933
{
10-
auto metaEnums = QMetaEnum::fromType<Property>();
11-
for (int i = 0; i < metaEnums.keyCount(); ++i) {
12-
m_headerDatas << metaEnums.key(i);
13-
}
34+
return d_ptr->datas.size();
35+
}
36+
37+
auto DisplayTableModel::columnCount(const QModelIndex &) const -> int
38+
{
39+
return d_ptr->headerDatas.size();
1440
}
1541

1642
auto DisplayTableModel::data(const QModelIndex &index, int role) const -> QVariant
@@ -22,7 +48,7 @@ auto DisplayTableModel::data(const QModelIndex &index, int role) const -> QVaria
2248
auto row = index.row();
2349
auto col = index.column();
2450

25-
const auto &data = m_datas.at(row);
51+
const auto &data = d_ptr->datas.at(row);
2652
switch (role) {
2753
case Qt::TextAlignmentRole: return Qt::AlignCenter;
2854
case Qt::CheckStateRole:
@@ -70,7 +96,7 @@ auto DisplayTableModel::setData(const QModelIndex &index, const QVariant &value,
7096
auto row = index.row();
7197
auto col = index.column();
7298

73-
auto data = m_datas.at(row);
99+
auto data = d_ptr->datas.at(row);
74100
switch (role) {
75101
case Qt::CheckStateRole:
76102
if (ID == col) {
@@ -97,14 +123,14 @@ auto DisplayTableModel::setData(const QModelIndex &index, const QVariant &value,
97123
auto DisplayTableModel::headerData(int section, Qt::Orientation orientation, int role) const
98124
-> QVariant
99125
{
100-
if (section < 0 || section >= m_headerDatas.size() || orientation != Qt::Horizontal) {
126+
if (section < 0 || section >= d_ptr->headerDatas.size() || orientation != Qt::Horizontal) {
101127
return {};
102128
}
103129
switch (role) {
104130
case Qt::TextAlignmentRole: return Qt::AlignCenter;
105131
case Qt::WhatsThisRole:
106132
case Qt::ToolTipRole:
107-
case Qt::DisplayRole: return m_headerDatas.at(section);
133+
case Qt::DisplayRole: return d_ptr->headerDatas.at(section);
108134
default: break;
109135
}
110136
return {};
@@ -123,3 +149,15 @@ Qt::ItemFlags DisplayTableModel::flags(const QModelIndex &index) const
123149
}
124150
return flags;
125151
}
152+
153+
void DisplayTableModel::setDatas(const DisplayInfoList &datas)
154+
{
155+
beginResetModel();
156+
d_ptr->datas = datas;
157+
endResetModel();
158+
}
159+
160+
DisplayInfoList DisplayTableModel::datas() const
161+
{
162+
return d_ptr->datas;
163+
}

TableViewModel/displaytablemodel.hpp

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,24 @@ class DisplayTableModel : public QAbstractTableModel
1212
Q_ENUM(Property);
1313

1414
explicit DisplayTableModel(QObject *parent = nullptr);
15+
~DisplayTableModel() override;
1516

16-
[[nodiscard]] auto rowCount(const QModelIndex & = QModelIndex()) const -> int
17-
{
18-
return m_datas.size();
19-
}
20-
[[nodiscard]] auto columnCount(const QModelIndex & = QModelIndex()) const -> int
21-
{
22-
return m_headerDatas.size();
23-
}
17+
[[nodiscard]] auto rowCount(const QModelIndex & = QModelIndex()) const -> int;
18+
[[nodiscard]] auto columnCount(const QModelIndex & = QModelIndex()) const -> int;
2419

2520
[[nodiscard]] auto data(const QModelIndex &index, int role = Qt::DisplayRole) const -> QVariant;
26-
auto setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) -> bool;
21+
[[nodiscard]] auto setData(const QModelIndex &index,
22+
const QVariant &value,
23+
int role = Qt::EditRole) -> bool;
2724
[[nodiscard]] auto headerData(int section,
2825
Qt::Orientation orientation,
2926
int role = Qt::DisplayRole) const -> QVariant;
3027
[[nodiscard]] auto flags(const QModelIndex &index) const -> Qt::ItemFlags;
3128

32-
void setDatas(const QVector<DisplayInfo> &datas)
33-
{
34-
beginResetModel();
35-
m_datas = datas;
36-
endResetModel();
37-
}
29+
void setDatas(const DisplayInfoList &datas);
30+
[[nodiscard]] auto datas() const -> DisplayInfoList;
3831

3932
private:
40-
QVector<DisplayInfo> m_datas;
41-
QStringList m_headerDatas;
33+
class DisplayTableModelPrivate;
34+
QScopedPointer<DisplayTableModelPrivate> d_ptr;
4235
};

TableViewModel/displaytableview.cc

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ class DisplayTableView::DisplayTableViewPrivate
2020
model = new DisplayTableModel(q_ptr);
2121

2222
menu = new QMenu(q_ptr);
23-
menu->addAction(QObject::tr("Insert"), q_ptr, &DisplayTableView::insertItem);
24-
menu->addAction(QObject::tr("Remove"), q_ptr, &DisplayTableView::removeItem);
25-
menu->addAction(QObject::tr("Rename"), q_ptr, [this] {
23+
menu->addAction(DisplayTableView::tr("Insert"), q_ptr, &DisplayTableView::insertItem);
24+
menu->addAction(DisplayTableView::tr("Remove"), q_ptr, &DisplayTableView::removeItem);
25+
menu->addAction(DisplayTableView::tr("Rename"), q_ptr, [this] {
2626
q_ptr->edit(q_ptr->currentIndex().siblingAtColumn(DisplayTableModel::TITLE));
2727
});
2828
}
@@ -44,18 +44,22 @@ DisplayTableView::DisplayTableView(QWidget *parent)
4444
setShowGrid(true);
4545
setWordWrap(false);
4646
setAlternatingRowColors(true);
47-
verticalHeader()->setVisible(false);
48-
verticalHeader()->setDefaultSectionSize(35);
49-
horizontalHeader()->setStretchLastSection(true);
50-
horizontalHeader()->setDefaultSectionSize(120);
51-
horizontalHeader()->setMinimumSectionSize(60);
52-
horizontalHeader()->setSectionResizeMode(QHeaderView::Interactive);
47+
5348
setSelectionBehavior(QAbstractItemView::SelectRows);
5449
setSelectionMode(QAbstractItemView::ExtendedSelection);
5550
setContextMenuPolicy(Qt::DefaultContextMenu);
5651
setSortingEnabled(true);
5752
setIconSize(QSize(20, 20));
5853

54+
auto *verticalHeader = this->verticalHeader();
55+
verticalHeader->setVisible(false);
56+
verticalHeader->setDefaultSectionSize(35);
57+
auto *horizontalHeader = this->horizontalHeader();
58+
horizontalHeader->setStretchLastSection(true);
59+
horizontalHeader->setDefaultSectionSize(120);
60+
horizontalHeader->setMinimumSectionSize(60);
61+
horizontalHeader->setSectionResizeMode(QHeaderView::Interactive);
62+
5963
setItemDelegateForColumn(DisplayTableModel::STATE, new ComboBoxDelegate(this));
6064
setItemDelegateForColumn(DisplayTableModel::PROCESS, new ProgressBarDelegate(this));
6165
setItemDelegateForColumn(DisplayTableModel::RATING, new StarDelegate(this));
@@ -65,7 +69,7 @@ DisplayTableView::DisplayTableView(QWidget *parent)
6569

6670
DisplayTableView::~DisplayTableView() = default;
6771

68-
void DisplayTableView::setDatas(const QVector<DisplayInfo> &datas)
72+
void DisplayTableView::setDatas(const DisplayInfoList &datas)
6973
{
7074
d_ptr->model->setDatas(datas);
7175
}

TableViewModel/displaytableview.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class DisplayTableView : public QTableView
1111
explicit DisplayTableView(QWidget *parent = nullptr);
1212
~DisplayTableView() override;
1313

14-
void setDatas(const QVector<DisplayInfo> &datas);
14+
void setDatas(const DisplayInfoList &datas);
1515

1616
protected:
1717
void contextMenuEvent(QContextMenuEvent *event) override;

0 commit comments

Comments
 (0)