//基本的文件对话框
this->setWindowTitle("标准文本对话框");
main_Button = new QPushButton;
main_Button->setText("选择文件");
main_LineEdit = new QLineEdit;
main_layout = new QGridLayout(this);
main_layout->addWidget(main_Button, 0, 0);
main_layout->addWidget(main_LineEdit, 0, 1);
main_layout->setSpacing(10);
main_layout->setMargin(10);
connect(main_Button, &QPushButton::clicked, this, &Dialog::openFile);
void Dialog::openFile()
{
QString sPathe = QFileDialog::getOpenFileName(this,
"标准文件框",
".",
"C++file(*.cpp);;"
"C file(*.c);;"
"Header file(*.h)");
main_LineEdit->setText(sPathe);
}
color_button = new QPushButton("选择颜色");
color_frame = new QFrame;
color_frame->setFrameStyle(QFrame::Box);
color_frame->setAutoFillBackground(true);
main_layout->addWidget(color_button, 1, 0);
main_layout->addWidget(color_frame, 1, 1);
connect(color_button, &QPushButton::clicked, this, &Dialog::openColor);
void Dialog::openColor()
{
QColor color = QColorDialog::getColor(Qt::yellow);
if (color.isValid()) {
color_frame->setPalette(QPalette(color));
}
}
font_Button = new QPushButton("选择字体");
font_LineEdit = new QLineEdit("are you ok ?");
main_layout->addWidget(font_Button);
main_layout->addWidget(font_LineEdit);
connect(font_Button, &QPushButton::clicked, this, &Dialog::openFont);
void Dialog::openFont()
{
bool ok;
QFont font = QFontDialog::getFont(&ok);
if (ok) {
font_LineEdit->setFont(font);
}
}
void InputDialog::nameEdit()
{
bool ok;
QString text = QInputDialog::getText(this,
"标准的文本输入对话框",
"请输入姓名:",
QLineEdit::Normal,
nameLabel->text(),
&ok);
if (ok && !text.isEmpty()) {
nameLabel->setText(text);
}
}
void InputDialog::sexEdit()
{
QStringList sexList;
sexList << "男"
<< "女"
<< "未知";
bool ok;
QString sex = QInputDialog::getItem(this, "标准条目对话框", "请选择性别", sexList, 0, false, &ok);
if (ok && !sex.isEmpty()) {
sexLabel->setText(sex);
}
}
void InputDialog::ageEdit()
{
bool ok;
int age = QInputDialog::getInt(this,
"标准的数字对话框",
"请输入年龄",
ageLabel->text().toInt(),
1,
120,
1,
&ok);
if (ok) {
ageLabel->setText(QString::number(age));
}
}
void InputDialog::scoreEdit()
{
bool ok;
double score = QInputDialog::getDouble(this,
"标准的浮点对话框",
"请输入分数",
scoreLabel->text().toDouble(),
1,
100,
1,
&ok);
if (ok) {
scoreLabel->setText(QString("%1").arg(score));
}
}
setWindowTitle("标准消息框");
m_tipLabel = new QLabel("请选择消息框");
//凹陷属性
m_tipLabel->setFrameStyle(QFrame::Panel | QFrame::Sunken);
m_queseMsg = new QPushButton("问题消息框");
m_infoMsg = new QPushButton("输入消息框");
m_warnMsg = new QPushButton("警告消息框");
m_criticalMsg = new QPushButton("错误消息框");
m_aboutMsg = new QPushButton("关于消息框");
m_aboutQtMsg = new QPushButton("关于Qt消息框");
m_MyMsg = new QPushButton("自定义消息框");
//布局
gridLayout = new QGridLayout(this);
gridLayout->addWidget(m_tipLabel, 0, 0, 1, 2);
gridLayout->addWidget(m_queseMsg, 1, 0);
gridLayout->addWidget(m_infoMsg, 1, 1);
gridLayout->addWidget(m_warnMsg, 2, 0);
gridLayout->addWidget(m_criticalMsg, 2, 1);
gridLayout->addWidget(m_aboutMsg, 3, 0);
gridLayout->addWidget(m_aboutQtMsg, 3, 1);
gridLayout->addWidget(m_MyMsg, 4, 0, 1, 2);
gridLayout->setSpacing(10);
gridLayout->setMargin(10);
connect(m_queseMsg, &QPushButton::clicked, this, &MsgDialog::showQueMsg);
connect(m_infoMsg, &QPushButton::clicked, this, &MsgDialog::showInfoMsg);
connect(m_warnMsg, &QPushButton::clicked, this, &MsgDialog::showWarnMsg);
connect(m_criticalMsg, &QPushButton::clicked, this, &MsgDialog::showCriticalMsg);
connect(m_aboutMsg, &QPushButton::clicked, this, &MsgDialog::showAboutMsg);
connect(m_aboutQtMsg, &QPushButton::clicked, this, &MsgDialog::showAboutQtMsg);
connect(m_MyMsg, &QPushButton::clicked, this, &MsgDialog::showMyMsg);
}MsgDialog::~MsgDialog() {}
void MsgDialog::showQueMsg()
{
m_tipLabel->setText("问题消息框");
int res = QMessageBox::question(this,
"问题消息栏",
"当前发现问题,是否退出?",
QMessageBox::Ok | QMessageBox::Cancel,
QMessageBox::Ok);
switch (res) {
case QMessageBox::Ok:
m_tipLabel->setText("问题消息|确定");
break;
case QMessageBox::Cancel:
m_tipLabel->setText("问题消息|返回");
break;
}
}void MsgDialog::showInfoMsg()
{
m_tipLabel->setText("信息消息框");
QMessageBox::information(this, "信息消息栏", "一二三四五,上三打老虎");
}void MsgDialog::showWarnMsg()
{
m_tipLabel->setText("警告消息框");
int res = QMessageBox::warning(this,
"警告消息栏",
"您有未保存的消息,是否保存",
QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel,
QMessageBox::Save);
switch (res) {
case QMessageBox::Save:
m_tipLabel->setText("警告消息|保存");
break;
case QMessageBox::Discard:
m_tipLabel->setText("警告消息|忽视");
break;
case QMessageBox::Cancel:
m_tipLabel->setText("警告消息|返回");
break;
}
}void MsgDialog::showCriticalMsg()
{
m_tipLabel->setText("错误消息框");
QMessageBox::critical(this, "错误消息栏", "发生重大错误");
}
// QTC_TEMP
void MsgDialog::showAboutMsg()
{
m_tipLabel->setText("关于消息框");
QMessageBox::about(this, "关于消息栏", "一二三四五,上三打老虎");
}
// QTC_TEMP
void MsgDialog::showAboutQtMsg()
{
m_tipLabel->setText("关于Qt消息框");
QMessageBox::aboutQt(this, "Qt消息栏");
}
void MsgDialog::showMyMsg()
{
m_tipLabel->setText("自定义消息框");
QMessageBox messBox;
messBox.setWindowTitle("自定义消息框");
messBox.setText("这是一个自定义的消息框");
QPushButton *yes = messBox.addButton("好的吧", QMessageBox::AcceptRole);
QPushButton *no = messBox.addButton("算了吧", QMessageBox::AcceptRole);
QPushButton *cancel = messBox.addButton(QMessageBox::Cancel);
messBox.setIconPixmap(QPixmap("login.ico"));
messBox.exec(); //执行起来
if (messBox.clickedButton() == yes) {
m_tipLabel->setText("好的吧");
}
if (messBox.clickedButton() == no) {
m_tipLabel->setText("算了吧");
}
if (messBox.clickedButton() == cancel) {
m_tipLabel->setText("返回了");
}
}