forked from GLDsuh-a/qt-1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdialog.cpp
More file actions
165 lines (136 loc) · 4.94 KB
/
dialog.cpp
File metadata and controls
165 lines (136 loc) · 4.94 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include "dialog.h"
#include "ui_dialog.h"
#include <QGridLayout>
#include <QFileDialog>
#include <QPushButton>
#include <QColorDialog>
#include <QFontDialog>
#include <QMessageBox>
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
// set the dialog border round
//this->setMask(roundedRect(this->rect(), 8));
setWindowTitle(QStringLiteral("各种标准对话框的实例"));
fileBtn = new QPushButton; //各个控件对象的初始化
fileBtn->setText(QStringLiteral("文件标准对话框实例"));
fileLineEdit = new QLineEdit; //用来显示选择的文件名
mainLayout = new QGridLayout(this); //布局设计
mainLayout->addWidget(fileBtn,0,0);
mainLayout->addWidget(fileLineEdit,0,1);
connect(fileBtn, SIGNAL(clicked()), this, SLOT(showFile())); //事件关联
colorBtn = new QPushButton; //创建各个控件的对象
colorBtn->setText(QStringLiteral("颜色标准对话框实例"));
colorFrame = new QFrame;
colorFrame->setFrameShape(QFrame::Box);
colorFrame->setAutoFillBackground(true);
mainLayout->addWidget(colorBtn,1,0); //布局设计
mainLayout->addWidget(colorFrame,1,1);
connect(colorBtn, SIGNAL(clicked()), this, SLOT(showColor())); //事件关联
fontBtn = new QPushButton; //创建控件的对象
fontBtn->setText(QStringLiteral("字体标准对话框实例"));
fontLineEdit = new QLineEdit; //显示更改的字符串
fontLineEdit->setText(QStringLiteral("Welcome!"));
mainLayout->addWidget(fontBtn,2,0); //布局设计
mainLayout->addWidget(fontLineEdit,2,1);
connect(fontBtn, SIGNAL(clicked()), this, SLOT(showFont())); //事件关联
inputBtn = new QPushButton; //创建控件的对象
inputBtn->setText(QStringLiteral("标准输入对话框实例"));
mainLayout->addWidget(inputBtn,3,0); //布局设计
connect(inputBtn,SIGNAL(clicked()),this,SLOT(showInputDlg()));
//事件关联
MsgBtn = new QPushButton; //创建控件对象
MsgBtn->setText(QStringLiteral("标准消息对话框实例"));
mainLayout->addWidget(MsgBtn,3,1);
connect(MsgBtn,SIGNAL(clicked()),this,SLOT(showMsgDlg()));
CustomBtn = new QPushButton;
CustomBtn->setText(QStringLiteral("用户自定义消息对话框实例"));
label = new QLabel;
label->setFrameStyle(QFrame::Panel|QFrame::Sunken);
mainLayout->addWidget(CustomBtn,4,0);
mainLayout->addWidget(label,4,1);
connect(CustomBtn,SIGNAL(clicked()),this,SLOT(showCustomDlg()));
}
Dialog::~Dialog()
{
delete ui;
}
void Dialog::showFile()
{
QString s = QFileDialog::getOpenFileName(this,"open file dialog","/","C++ files(*.cpp)::C files(*.c)::Head files(*.h)");
fileLineEdit->setText(s);
}
void Dialog::showColor()
{
QColor c = QColorDialog::getColor(Qt::blue);
if(c.isValid())
{
colorFrame->setPalette(QPalette(c));
}
}
void Dialog::showFont()
{
bool ok;
QFont f = QFontDialog::getFont(&ok);
if (ok)
{
fontLineEdit->setFont(f);
}
}
void Dialog::showInputDlg()
{
inputDlg = new InputDlg(this);
inputDlg->show();
}
void Dialog::showMsgDlg()
{
msgDlg = new MsgBoxDlg();
msgDlg->show();
}
void Dialog::showCustomDlg()
{
label->setText(QStringLiteral("Custom Message Box"));
QMessageBox customMsgBox;
customMsgBox.setWindowTitle(QStringLiteral("用户自定义消息框")); //设置消息框的标题
QPushButton *yesBtn = customMsgBox.addButton(QStringLiteral("Yes"), QMessageBox::ActionRole);
QPushButton *noBtn = customMsgBox.addButton(QStringLiteral("No"), QMessageBox::ActionRole);
QPushButton *cancelBtn = customMsgBox.addButton(QMessageBox::Cancel);
customMsgBox.setText(QStringLiteral("这是一个用户自定义消息框!"));
customMsgBox.setIconPixmap(QPixmap("Qt.png"));
customMsgBox.exec();
if(customMsgBox.clickedButton()== yesBtn)
{
label->setText("Custom Message Box/Yes");
}
if(customMsgBox.clickedButton()== noBtn)
{
label->setText("Custom Message Box/No");
}
if(customMsgBox.clickedButton()== cancelBtn)
{
label->setText("Custom Message Box/Cancel");
}
return;
}
QRegion Dialog::roundedRect(const QRect &rect, int r)
{
QRegion region;
// middle and borders
region += rect.adjusted(r, 0, -r, 0);
region += rect.adjusted(0, r, 0, -r);
// top left
QRect corner(rect.topLeft(), QSize(r*2, r*2));
region += QRegion(corner, QRegion::Ellipse);
// top right
corner.moveTopRight(rect.topRight());
region += QRegion(corner, QRegion::Ellipse);
// bottom left
corner.moveBottomLeft(rect.bottomLeft());
region += QRegion(corner, QRegion::Ellipse);
// bottom right
corner.moveBottomRight(rect.bottomRight());
region += QRegion(corner, QRegion::Ellipse);
return region;
}