QT 创建对话框 Dialog 实例
1.
2. dialog.h 头文件
#ifndef DIALOG_H
#define DIALOG_H #include <QDialog> QT_BEGIN_NAMESPACE
class QLabel;
class QErrorMessage;
QT_END_NAMESPACE class Dialog : public QDialog
{
Q_OBJECT public: Dialog(QWidget *parent = 0); private slots: void getAge();
void getStature();
void getSex();
void getName();
void getColor();
void getFont(); void getCriticalMessage();
void getInformationMessage();
void getQuestionMessage();
void getWarningMessage();
void getErrorMessage(); private: QLabel *nameLabel;
QLabel *sexLabel;
QLabel *ageLabel;
QLabel *statureLabel; QLabel *colorLabel;
QLabel *fontLabel; QLabel *criticalLabel;
QLabel *informationLabel;
QLabel *questionLabel;
QLabel *warningLabel;
QLabel *errorLabel;
QErrorMessage *errorMessageDialog; }; #endif
3. dialog.cpp源文件
#include <QtGui> #include "dialog.h" #define MESSAGE \
Dialog::tr("<p>Message boxes have a caption, a text, " \
"and any number of buttons, each with standard or custom texts." \
"<p>Click a button to close the message box. Pressing the Esc button " \
"will activate the detected escape button (if any).") Dialog::Dialog(QWidget *parent)
: QDialog(parent)
{ setWindowTitle(tr("Standard Dialogs"));
errorMessageDialog = new QErrorMessage(this); int frameStyle = QFrame::Sunken | QFrame::Panel; //Name
nameLabel = new QLabel;
nameLabel->setFrameStyle(QFrame::Sunken | QFrame::Panel);
QPushButton *nameButton = new QPushButton(tr("&Name")); //Sex
sexLabel = new QLabel;
sexLabel->setFrameStyle(frameStyle);
QPushButton *sexButton = new QPushButton(tr("&Sex")); //Age
ageLabel = new QLabel;
ageLabel->setFrameStyle(frameStyle);
QPushButton *ageButton = new QPushButton(tr("&Age")); //Stature
statureLabel = new QLabel;
statureLabel->setFrameStyle(frameStyle);
QPushButton *statureButton = new QPushButton(tr("S&tature")); //Color
colorLabel = new QLabel;
colorLabel->setFrameStyle(frameStyle);
QPushButton *colorButton = new QPushButton(tr("&Color")); //Font
fontLabel = new QLabel;
fontLabel->setFrameStyle(frameStyle);
QPushButton *fontButton = new QPushButton(tr("&Font")); //Critical MessageBox
criticalLabel = new QLabel;
criticalLabel->setFrameStyle(frameStyle);
QPushButton *criticalButton =
new QPushButton(tr("Critica&l")); //Information MessageBox
informationLabel = new QLabel;
informationLabel->setFrameStyle(frameStyle);
QPushButton *informationButton =
new QPushButton(tr("&Information")); //Question MessageBox
questionLabel = new QLabel;
questionLabel->setFrameStyle(frameStyle);
QPushButton *questionButton =
new QPushButton(tr("&Question")); //Warning MessageBox
warningLabel = new QLabel;
warningLabel->setFrameStyle(frameStyle);
QPushButton *warningButton = new QPushButton(tr("&Warning")); //Error MessageBox
errorLabel = new QLabel;
errorLabel->setFrameStyle(frameStyle);
QPushButton *errorButton =
new QPushButton(tr("Show &Message")); connect(ageButton, SIGNAL(clicked()), this, SLOT(getAge()));
connect(statureButton, SIGNAL(clicked()), this, SLOT(getStature()));
connect(sexButton, SIGNAL(clicked()), this, SLOT(getSex()));
connect(nameButton, SIGNAL(clicked()), this, SLOT(getName()));
connect(colorButton, SIGNAL(clicked()), this, SLOT(getColor()));
connect(fontButton, SIGNAL(clicked()), this, SLOT(getFont())); connect(criticalButton, SIGNAL(clicked()), this, SLOT(getCriticalMessage()));
connect(informationButton, SIGNAL(clicked()),
this, SLOT(getInformationMessage()));
connect(questionButton, SIGNAL(clicked()), this, SLOT(getQuestionMessage()));
connect(warningButton, SIGNAL(clicked()), this, SLOT(getWarningMessage()));
connect(errorButton, SIGNAL(clicked()), this, SLOT(getErrorMessage())); //Setup Layout
QGridLayout *layout = new QGridLayout;
layout->setColumnStretch(1, 1);
layout->setColumnMinimumWidth(1, 250); layout->addWidget(nameButton, 0, 0);
layout->addWidget(nameLabel, 0, 1);
layout->addWidget(sexButton, 1, 0);
layout->addWidget(sexLabel, 1, 1);
layout->addWidget(ageButton, 3, 0);
layout->addWidget(ageLabel, 3, 1);
layout->addWidget(statureButton, 4, 0);
layout->addWidget(statureLabel, 4, 1); layout->addWidget(colorButton, 5, 0);
layout->addWidget(colorLabel, 5, 1);
layout->addWidget(fontButton, 6, 0);
layout->addWidget(fontLabel, 6, 1); layout->addWidget(criticalButton, 7, 0);
layout->addWidget(criticalLabel, 7, 1);
layout->addWidget(informationButton, 8, 0);
layout->addWidget(informationLabel, 8, 1);
layout->addWidget(questionButton, 9, 0);
layout->addWidget(questionLabel, 9, 1);
layout->addWidget(warningButton, 10, 0);
layout->addWidget(warningLabel, 10, 1);
layout->addWidget(errorButton, 11, 0);
layout->addWidget(errorLabel, 11, 1); setLayout(layout); } void Dialog::getAge()
{ bool ok;
int age = QInputDialog::getInteger(this,tr("User Age"),
tr("Please input age"),ageLabel->text().toInt(),0,150,1,&ok);
if(ok)
{
ageLabel->setText(QString(tr("%1")).arg(age));
}
} void Dialog::getStature()
{ bool ok;
double d = QInputDialog::getDouble(this,tr("Stature"),
tr("Please input stature"),6782.5,0,65536.00,1,&ok);
if(ok)
{
statureLabel->setText(QString(tr("%1")).arg(d));
}
} void Dialog::getSex()
{ QStringList items;
items << tr("male") << tr("female"); bool ok;
QString sex = QInputDialog::getItem(this,tr("Sex"),
tr("Please select sex"),items,0,false,&ok);
if (ok)
{
sexLabel->setText(sex);
}
} void Dialog::getName()
{ bool ok;
QString name = QInputDialog::getText(this,tr("User Name"),
tr("Please input new name"),QLineEdit::Normal,nameLabel->text(),&ok); if(ok && !name.isEmpty())
{
nameLabel->setText(name);
}
} void Dialog::getColor()
{
QColor color = QColorDialog::getColor(Qt::green, this); if (color.isValid())
{
colorLabel->setText(color.name());
colorLabel->setPalette(QPalette(color));
colorLabel->setAutoFillBackground(true);
}
} void Dialog::getFont()
{
bool ok;
QFont font = QFontDialog::getFont(&ok, QFont(fontLabel->text()), this); if (ok)
{
fontLabel->setText(font.key());
fontLabel->setFont(font);
}
} void Dialog::getCriticalMessage()
{
QMessageBox::StandardButton reply;
reply = QMessageBox::critical(this, tr("QMessageBox::critical()"),
MESSAGE,
QMessageBox::Abort | QMessageBox::Retry | QMessageBox::Ignore); if (reply == QMessageBox::Abort)
{
criticalLabel->setText(tr("Abort"));
}
else if (reply == QMessageBox::Retry)
{
criticalLabel->setText(tr("Retry"));
}
else
{
criticalLabel->setText(tr("Ignore"));
}
} void Dialog::getInformationMessage()
{
QMessageBox::StandardButton reply;
reply = QMessageBox::information(this, tr("QMessageBox::information()"), MESSAGE); if (reply == QMessageBox::Ok)
{
informationLabel->setText(tr("OKOK"));
}
else
{
informationLabel->setText(tr("Escape"));
}
} void Dialog::getQuestionMessage()
{
QMessageBox::StandardButton reply;
reply = QMessageBox::question(this, tr("QMessageBox::question()"),
MESSAGE,
QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel); if (reply == QMessageBox::Yes)
{
questionLabel->setText(tr("Yes"));
}
else if (reply == QMessageBox::No)
{
questionLabel->setText(tr("No"));
}
else
{
questionLabel->setText(tr("Cancel"));
}
} void Dialog::getWarningMessage()
{
QMessageBox msgBox(QMessageBox::Warning, tr("QMessageBox::warning()"),
MESSAGE, 0, this);
msgBox.addButton(tr("Save &Again"), QMessageBox::AcceptRole);
msgBox.addButton(tr("&Continue"), QMessageBox::RejectRole); if (msgBox.exec() == QMessageBox::AcceptRole)
{
warningLabel->setText(tr("Save Again"));
}
else
{
warningLabel->setText(tr("Continue"));
} } void Dialog::getErrorMessage()
{
errorMessageDialog->showMessage(
tr("This dialog shows and remembers error messages. "
"If the checkbox is checked (as it is by default), "
"the shown message will be shown again, "
"but if the user unchecks the box the message "
"will not appear again if QErrorMessage::showMessage() "
"is called with the same message.")); errorLabel->setText(tr("If the box is unchecked, the message "
"won't appear again."));
}
QT 创建对话框 Dialog 实例的更多相关文章
- EasyUI 动态创建对话框Dialog
// 拒绝审批通过 function rejectApproval() { // 创建填写审批意见对话框 $("<div id='reject-comment'> </di ...
- 【Qt】2.3 使用Qt设计师来创建对话框
安装完Qt OpenSource之后,在开始菜单目录下会有这几个东西. 其中[Designer]是用来设计窗口界面的程序.所以现在可以使用它来设计一个对话框.在[Qt Creator]中,[设计]这一 ...
- 详解Linux交互式shell脚本中创建对话框实例教程_linux服务器
本教程我们通过实现来讲讲Linux交互式shell脚本中创建各种各样对话框,对话框在Linux中可以友好的提示操作者,感兴趣的朋友可以参考学习一下. 当你在终端环境下安装新的软件时,你可以经常看到信息 ...
- Android中制作自定义dialog对话框的实例
http://www.jb51.net/article/83319.htm 这篇文章主要介绍了Android中制作自定义dialog对话框的实例分享,安卓自带的Dialog显然不够用,因而我们要继 ...
- QT创建模态对话框阻塞整个应用程序和非模态对话框唯一性约束的简单示例
QT创建模态对话框阻塞整个应用程序和非模态对话框唯一性约束的简单示例 部分代码: // 创建模态对话框阻塞整个应用程序和非模态对话框唯一性约束 QMenu *pDialog = mBar->ad ...
- 【转】QT创建子对话框的方法
原文地址:http://blog.csdn.net/baidu_18991997/article/details/42713159 代码实现功能:单击某个按钮后,弹出对话框,对话框中的内容可自行设计. ...
- Android对话框Dialog深度剖析
对话框 对话框是提示用户作出决定或输入额外信息的小窗口. 对话框不会填充屏幕,通常用于需要用户采取行动才能继续执行的模式事件. 对话框设计 Dialog 类是对话框的基类,但您应该避免直接实例化 Di ...
- QT模态对话框及非模态对话框
QT模态对话框及非模态对话框 模态对话框(Modal Dialog)与非模态对话框(Modeless Dialog)的概念不是Qt所独有的,在各种不同的平台下都存在.又有叫法是称为模式对话框,无模式对 ...
- Android 对话框(Dialog) 及 自己定义Dialog
Activities提供了一种方便管理的创建.保存.回复的对话框机制,比如 onCreateDialog(int), onPrepareDialog(int, Dialog), showDialog( ...
随机推荐
- 回溯法——n后问题
问题描述: 在n*n的棋盘上放置彼此不受攻击的n个皇后.按照国际象棋的规则,皇后可以攻击与之处在同一行或同一列或同一斜线上的棋子.n后问题等价于在n*n格的棋盘上放置n个皇后,任何2个皇后不放在同一行 ...
- python学习【第十篇】单例设计模式
单例设计模式 目的:让类创建对象,在系统中只有唯一的实例,让每一次创建的对象返回的内存地址都是相同的. __new__方法 使用类名创建对象时,python解释器首先会调用__new__方法为对象分配 ...
- SQL Server函数
阅读目录 SQL Server函数---Union与Union All的区别 回到顶部 SQL Server函数---Union与Union All的区别 如果我们需要将两个select语句的结果作为 ...
- 【转】【Spring实战】Spring注解配置工作原理源码解析
一.背景知识 在[Spring实战]Spring容器初始化完成后执行初始化数据方法一文中说要分析其实现原理,于是就从源码中寻找答案,看源码容易跑偏,因此应当有个主线,或者带着问题.目标去看,这样才能最 ...
- setlocale(LC_ALL, ""); 取值为空字符串" "(注意,不是NULL),则locale与本地环境所使用的编码方式相同(在本地化时,应该很有用);
在C运行库提供的多字节字符-宽字符转换函数:mbstowcs()/wcstombs()中,需要用到全局变量locale( locale encoding ),以指定多字节字符的编码类型 1. 功能: ...
- CF85D Sum of Medians
CF85D Sum of Medians 题意翻译 一个集合,初始为空.现有三个操作: 1.add:向集合里加入数x,保证加入前集合中没有数x: 2.del:从集合中删除数x,保证删除前集合中有x: ...
- 在windows和linux之间用SecureCRT来上传和下载文件
SecureCRT可以使用linux下的zmodem协议来快速的传送文件,使用非常方便.具体步骤:一.在使用SecureCRT上传下载之前需要给服务器安装lrzsz:A:CentOS中使用yum安装即 ...
- Apache JServ Protocol (AJP)
The Apache JServ Protocol (AJP) is a binary protocol that can proxy inbound requests from a web serv ...
- 实验一中的OOP思想
子类继承父类 父类中声明了接口变量 接口AB中声明了抽象方法 ab 在子类中 可以用这样通俗的语句写程序: while (!(this.termination.shouldTerminate ...
- Js全局异常捕获
重新window.onerror方法就行了 window.onerror = handleError function handleError(msg,url,l) { var txt="T ...