Skip to content

Commit 46796cf

Browse files
committed
[更新依赖和重构代码]: 升级Qt版本至6.9.1,优化TableViewModel模块实现
- 依赖升级: 将Qt版本从6.9.0更新至6.9.1,涉及GitHub Actions配置、CMake脚本及打包脚本调整 - GitHub Actions优化: 升级actions/checkout到v5版本,CodeQL工作流增加dependabot分支过滤 - TableViewModel重构: - 使用智能指针管理私有成员,实现DisplayTableModelPrivate和MainWindowPrivate - 修复ButtonDelegate的点击事件逻辑,使用标准样式绘制按钮 - 优化ProgressBarDelegate绘制逻辑,移除冗余QProgressBar成员 - 调整RichTextItemDelegate文本居中显示逻辑 - 模型接口优化: - 将QVector<DisplayInfo>替换为DisplayInfoList类型 - 增强模型数据访问接口,增加datas()获取方法 - 界面细节调整: - 修复表格视图的上下文菜单定位问题 - 优化对话框尺寸和文本换行显示 - 统一委托类中widget指针获取方式
1 parent 9b632ec commit 46796cf

24 files changed

Lines changed: 270 additions & 258 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:

.github/workflows/cmake.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ jobs:
3737
- "Ninja"
3838

3939
steps:
40-
- uses: actions/checkout@v4
40+
- uses: actions/checkout@v5
4141
with:
4242
fetch-depth: 1
4343

.github/workflows/codeql.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ on:
1010
- 'LICENSE'
1111
- '*.pro'
1212
- 'README*'
13+
branches-ignore:
14+
- 'dependabot/**'
1315
pull_request:
1416
paths-ignore:
1517
- '**/picture/**'
@@ -19,6 +21,8 @@ on:
1921
- 'LICENSE'
2022
- '*.pro'
2123
- 'README*'
24+
branches-ignore:
25+
- 'dependabot/**'
2226

2327
schedule:
2428
- cron: '0 0 1 * *'
@@ -30,7 +34,7 @@ jobs:
3034
runs-on: ubuntu-latest
3135

3236
steps:
33-
- uses: actions/checkout@v4
37+
- uses: actions/checkout@v5
3438
with:
3539
fetch-depth: 1
3640

.github/workflows/qmake.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ jobs:
3535
- ubuntu-latest
3636

3737
steps:
38-
- uses: actions/checkout@v4
38+
- uses: actions/checkout@v5
3939
with:
4040
fetch-depth: 1
4141

.github/workflows/readme.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212
build:
1313
runs-on: ubuntu-latest
1414
steps:
15-
- uses: actions/checkout@v4
15+
- uses: actions/checkout@v5
1616
- name: Setup Node.js
1717
uses: actions/setup-node@v4
1818
# ISO Langusge Codes: https://cloud.google.com/translate/docs/languages

.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)

0 commit comments

Comments
 (0)