This repository has been archived by the owner on Mar 31, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathcustomdialog.cpp
147 lines (122 loc) · 4.8 KB
/
customdialog.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include "customdialog.h"
/// windowName:窗体标题
/// windowIcon:窗体标识图片路径
/// buttonName:按键水平布局---纯按键(垂直布局)
/// textContent:文本垂直布局
void CustomDialog::showCustomDialog(QString windowName, QString windowIcon, QStringList buttonName, QString textContent, QWidget *parent)
{
//传参
_WindowName = windowName;
_WindowIcon = windowIcon;
_ButtonName = buttonName;
_TextContent = textContent;
_Parent = parent;
//背景颜色
this->setWindowOpacity(0.9); //透明度
this->setStyleSheet("background-color: rgba(128, 128, 128, 25%)"); //背景
//设置窗口的标题和标识
this->setWindowTitle(_WindowName);
this->setWindowIcon(QIcon(_WindowIcon));
//适应屏幕大小
this->resize(_Parent->width()*0.9,_Parent->height()*0.9);
this->setFixedSize(_Parent->width()*0.9,_Parent->height()*0.9);
//使弹窗居中显示
this->move(_Parent->pos().x() + (_Parent->width() - this->width()) / 2,_Parent->pos().y() + (_Parent->height() - this->height()) / 2);
//设置文本显示
QTextBrowser textBrowser;
textBrowser.setStyleSheet("border: none; color: rgb(245, 246, 255);background-color: rgba(128, 128, 128, 0%);");
textBrowser.setText(_TextContent);
textBrowser.setFixedSize(_Parent->width()*0.877,_Parent->height()*0.5);
// textBrowser.move();
// textBrowser.setGeometry(50,50,_Parent->width()*800*0.8,_Parent->height()*480*0.5);
textBrowser.setAlignment(Qt::AlignCenter);
//根据QStringList buttonName创建按键
QList<QPushButton*> btn;
for(int i = 0;i < _ButtonName.count() ;i++)
{
btn.append(new QPushButton(_ButtonName[i]));
btn[i] = new QPushButton(_ButtonName[i]);
//指定按键样式
btn[i]->setWhatsThis("BTStyleSheet");
//调整字体大小
QFont _font = btn[i]->font();
_font.setPointSize(_Parent->maximumWidth() > _Parent->maximumHeight() ? (int)_Parent->maximumHeight()/30 : (int)_Parent->maximumWidth()/30);
btn[i]->setFont(_font);
textBrowser.setFont(_font);
}
//调整按键和文本的位置
//文本垂直布局
QVBoxLayout VLayout(this);
VLayout.addSpacing(150);
VLayout.addWidget(&textBrowser);
VLayout.addSpacing(300);
//按键水平布局
QHBoxLayout RestartBT;
foreach(QPushButton *item , btn)
{
RestartBT.addWidget(item);
//按键信号连接槽
QObject::connect(item, &QPushButton::clicked, this,[=]()
{
QPushButton *optBtn = qobject_cast<QPushButton *>(sender());
emit OutputEvent(optBtn->text());
this->close();
});
}
VLayout.addLayout(&RestartBT);
//模态显示
this->exec();
}
void CustomDialog::showCustomDialog(QString windowName, QString windowIcon, QStringList buttonName, QWidget *parent)
{
//传参
_WindowName = windowName;
_WindowIcon = windowIcon;
_ButtonName = buttonName;
_Parent = parent;
//背景颜色
this->setWindowOpacity(0.9); //透明度
this->setStyleSheet("background-color: rgba(128, 128, 128, 25%)"); //背景
//设置窗口的标题和标识
this->setWindowTitle(_WindowName);
this->setWindowIcon(QIcon(_WindowIcon));
//适应屏幕大小
this->resize(_Parent->width()*0.9,_Parent->height()*0.9);
this->setFixedSize(_Parent->width()*0.9,_Parent->height()*0.9);
//使弹窗居中显示
this->move(_Parent->pos().x() + (_Parent->width() - this->width()) / 2,_Parent->pos().y() + (_Parent->height() - this->height()) / 2);
//根据QStringList buttonName创建按键
QList<QPushButton*> btn;
for(int i = 0;i < _ButtonName.count() ;i++)
{
btn.append(new QPushButton(_ButtonName[i]));
btn[i] = new QPushButton(_ButtonName[i]);
//指定按键样式
btn[i]->setWhatsThis("BTStyleSheet");
//调整字体大小
QFont _font = btn[i]->font();
_font.setPointSize(_Parent->maximumWidth() > _Parent->maximumHeight() ? (int)_Parent->maximumHeight()/30 : (int)_Parent->maximumWidth()/30);
btn[i]->setFont(_font);
}
//调整按键的位置
QVBoxLayout VLayout(this);
//按键水平布局
foreach(QPushButton *item , btn)
{
VLayout.addWidget(item);
//按键信号连接槽
QObject::connect(item, &QPushButton::clicked, this,[=]()
{
QPushButton *optBtn = qobject_cast<QPushButton *>(sender());
emit OutputEvent(optBtn->text());
this->close();
});
}
//模态显示
this->exec();
}
void CustomDialog::showCustomDialog(QString windowName, QString windowIcon, QString textContent, QWidget *parent)
{
QStringList btnName = {"Ok"};
showCustomDialog(windowName, windowIcon, btnName, textContent, parent);
}