Skip to content

Commit fa551a3

Browse files
committed
优化FramelessWindow的示例代码
1 parent ee755ec commit fa551a3

File tree

2 files changed

+32
-17
lines changed

2 files changed

+32
-17
lines changed

example/Window/FramelessWindow/widget.cpp

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,29 @@ Widget::Widget(QWidget *parent)
1010
{
1111
this->setWindowTitle("示例程序");
1212
ui->setupUi(this);
13-
connect(ui->base, &QPushButton::clicked, this, std::bind(&Widget::showExample, this, 0));
14-
connect(ui->simple, &QPushButton::clicked, this, std::bind(&Widget::showExample, this, 1));
15-
connect(ui->standard, &QPushButton::clicked, this, std::bind(&Widget::showExample, this, 2));
16-
connect(ui->areo, &QPushButton::clicked, this, std::bind(&Widget::showExample, this, 3));
17-
connect(ui->fully_transparent, &QPushButton::clicked, this, std::bind(&Widget::showExample, this, 4));
18-
connect(ui->system_color, &QPushButton::clicked, this, std::bind(&Widget::showExample, this, 5));
19-
connect(ui->mica, &QPushButton::clicked, this, std::bind(&Widget::showExample, this, 6));
20-
connect(ui->mica_alt, &QPushButton::clicked, this, std::bind(&Widget::showExample, this, 7));
21-
connect(ui->acrylic, &QPushButton::clicked, this, std::bind(&Widget::showExample, this, 8));
22-
connect(ui->custom_titlebar, &QPushButton::clicked, this, std::bind(&Widget::showExample, this, 9));
23-
connect(ui->microsoft_store, &QPushButton::clicked, this, std::bind(&Widget::showExample, this, 10));
24-
connect(ui->ntqq, &QPushButton::clicked, this, std::bind(&Widget::showExample, this, 11));
25-
connect(ui->adaptive, &QPushButton::clicked, this, std::bind(&Widget::showExample, this, 12));
13+
14+
QMap<QString,int> btn_indexs;
15+
btn_indexs["base"] = 0;
16+
btn_indexs["simple"] = 1;
17+
btn_indexs["standard"] = 2;
18+
btn_indexs["areo"] = 3;
19+
btn_indexs["fully_transparent"] = 4;
20+
btn_indexs["system_color"] = 5;
21+
btn_indexs["mica"] = 6;
22+
btn_indexs["mica_alt"] = 7;
23+
btn_indexs["acrylic"] = 8;
24+
btn_indexs["custom_titlebar"] = 9;
25+
btn_indexs["microsoft_store"] = 10;
26+
btn_indexs["ntqq"] = 11;
27+
btn_indexs["adaptive"] = 12;
28+
29+
#if (QT_VERSION <= QT_VERSION_CHECK(6, 3, 0))
30+
for(auto& btn : findChildren<QPushButton *>(QRegularExpression(QString(R"([\s\S]+)")),Qt::FindChildOption::FindDirectChildrenOnly))
31+
#else
32+
for(auto& btn : findChildren<QPushButton*>())
33+
#endif
34+
connect(btn,&QPushButton::clicked,this,std::bind(&Widget::showExample,this,btn_indexs[btn->objectName()]));
35+
2636
}
2737

2838
Widget::~Widget()
@@ -110,7 +120,7 @@ void Widget::showExample(int index)
110120
break;
111121
case 10:
112122
w = new MSWindow; // 微软商店风格窗口
113-
master_scope = ((MSWindow *)w)->clientArea(); // MSWindow重新实现了qWidgetUseInSetupUi,不能使用父类的此函数
123+
master_scope = ((MSWindow *)w)->clientArea();
114124
((FramelessWindow *)w)->setWindowTitle("微软商店风格窗口");
115125
break;
116126
case 11:
@@ -144,10 +154,10 @@ void Widget::showExample(int index)
144154
{
145155
QPushButton *btn = new QPushButton("设置颜色", w);
146156
ui->expansion->addWidget(btn);
147-
connect(btn, &QPushButton::clicked, w, [w]()
148-
{
157+
connect(btn, &QPushButton::clicked, w, [w](){
149158
QColor color = QColorDialog::getColor(QColor(255,255,255,105),w,"选择颜色",QColorDialog::ShowAlphaChannel);
150-
((AreoWindow*)w)->changeColor(ABGR(color.alpha(),color.blue(),color.green(),color.red())); });
159+
((AreoWindow*)w)->changeColor(ABGR(color.alpha(),color.blue(),color.green(),color.red()));
160+
});
151161
}
152162
if (index == 11)
153163
{

example/Window/FramelessWindow/widget.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,16 @@
44
#include <QObject>
55
#include <QWidget>
66
#include <QVector>
7+
#include <QMap>
78
#include <QSpacerItem>
89
#include <QLineEdit>
910
#include <QCheckBox>
1011
#include <QColorDialog>
1112

13+
#if (QT_VERSION <= QT_VERSION_CHECK(6, 3, 0))
14+
#include <QRegularExpression>
15+
#endif
16+
1217
namespace Ui
1318
{
1419
class form;

0 commit comments

Comments
 (0)