欢迎小伙伴的点评✨✨,相互学习、互关必回、全天在线🍳🍳🍳
博主🧑🧑 本着开源的精神交流Qt开发的经验、将持续更新续章,为社区贡献博主自身的开源精神👩🚀
本章节将会给大家带来标准输入对话框的详细使用方法
标准输入对话框提供四种数据类型的输入,包括字符串、下拉列表框的条目、 int 数据类型和 double 数据类型。
标准字符串输入对话框通过 QInputDialog 类的静态函数 getText()完成, getText()函数形式如下:
QString getText
(
QWidget * parent, //标准输入对话框的父窗口
const QString& title, //标准输入对话框的标题名
const QString& label, //标准输入对话框的标签提示
QLineEdit::EchoMode mode = QLineEdit::Normal, //指定标准输入对话框中 QLineEdit 控件的输入模式
const QString& text = QString(), //标准字符串输入对话框弹出时QLineEdit控件中默认出现的文字
bool *ok =0, //注
Qt::WindowFlags flags = 0 //指明标准输入对话框的窗体标识
);
注:指示标准输入对话框的哪个按钮被触发,若为 true, 则表示用户单击了 “OK” (确定)按钮;若为 false,则表示用户单击了 “Cancle” (取消)按钮。
标准条目选择对话框是通过 QinputDialog 类的静态函数 getltem()来完成的, getltem()函数形式如下:
QString getItem
(
QWidget * parent, //标准输入对话框的父窗口
const QString& title, //标准输入对话框的标题名
const QString& label, //标准输入对话框的标签提示
const QStringList& items, //注(1)
int current=0, //注(2)
bool editable = true, //指定QComboBox控件中显示的文字是否可编辑
bool * ok=0, //注(3)
Qt::WindowFlags flags=0 //指明标准输入对话框的窗体标识
);
注:
(1) 指定标准输入对话框中 QComboBox 控件显示的可选条目为一个 QStringList 对象。
(2) 标准条目选择对话框弹出时 QComboBox 控件中默认显示的条目序号。
(3) 指示标准输入对话框的哪个按钮被触发,若 ok 为 true, 则表示用户单击了 “OK”
( 确定)按钮;若 ok为 false, 则表示用户单击了 “Cancle” (取消)按钮。
标准 int 类型输入对话框是通过 QlnputDialog 类的静态函数 getlnt()来完成的, getlnt()函数形式如下:
int getInt
(
QWidget * parent, //标准输入对话框的父窗口
const QString& title, //标准输入对话框的标题名
const QString& label, //标准输入对话框的标签提示
int value=0, //指定标准输入对话框中QSpinBox控件的默认显示值
int min = -2147483647, //指定QSpinBox控件的数值范围
int max = 2147483647,
int step=1, //指定QSpinBox控件的步进值
bool* ok=0, //注
Qt::WindowFlags flags=0 //指明标准输入对话框的窗口标识
)
标准 double 类型输入对话框是通过 QlnputDialog 类的静态函数 getDouble()来完成的,getDouble() 函数形式如下:
double getDouble
(
QWidget* parent, //标准输入对话框的父窗口
const QString& title, //标准输入对话框的标题名
const QString& label, //标准输入对话框的标签提示
double value=0, //指定标准输入对话框中QSpinBox控件默认的显示值
double min = -2147483647, //指定QSpinBox控件的数值范围
double max = 2147483647,
int decimals=1, //指定QSpinBox控件的步进值
bool* ok=0, //注
Qt::WindowFlags flags=0 //指明标准输入对话框的窗口标识
)
注:用千指示标准输入对话框的哪个按钮被触发,若 ok 为 true, 则表示用户单击了 “OK” (确定)按钮;若 ok 为 false, 则表示用户单击了 “Cancel” (取消)按钮。
图一
#ifndef DIALOG_H
#define DIALOG_H
#include
#include
#include
#include
namespace Ui {
class Dialog;
}
class Dialog : public QDialog
{
Q_OBJECT
public:
explicit Dialog(QWidget *parent = nullptr);
~Dialog();
private:
Ui::Dialog *ui;
QPushButton *inputBtn;
inputdlg *inputDlg;
QGridLayout *mainLayout;
private slots:
void showInputDlg();
};
#endif // DIALOG_H
#ifndef INPUTDLG_H
#define INPUTDLG_H
#include
#include
#include
#include
class inputdlg : public QDialog
{
Q_OBJECT
public:
inputdlg(QWidget* parent = nullptr);
private slots:
void ChangeName();
void ChangeSex();
void ChangeAge();
void ChangeScore();
private:
QLabel *nameLabel1;
QLabel *sexLabel1;
QLabel *ageLabel1;
QLabel *scoreLabel1;
QLabel *nameLabel2;
QLabel *sexLabel2;
QLabel *ageLabel2;
QLabel *scoreLabel2;
QPushButton *nameBtn;
QPushButton *sexBtn;
QPushButton *ageBtn;
QPushButton *scoreBtn;
QGridLayout *mainLayout;
};
#endif // INPUTDLG_H
#include "dialog.h"
#include "ui_dialog.h"
#include
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
inputBtn = new QPushButton; //创建控件对象
inputBtn->setText(tr("标准输入对话框实例"));
inputBtn->setStyleSheet("border:2px groove gray;border-radius:10px;padding:2px 4px;"); //设置按钮形状
mainLayout = new QGridLayout(this);
mainLayout->addWidget(inputBtn,1,1); //布局设计
connect (inputBtn, SIGNAL (clicked()), this, SLOT (showInputDlg()));
}
Dialog::~Dialog()
{
delete ui;
}
void Dialog::showInputDlg()
{
inputDlg = new inputdlg(this);
inputDlg->show();
qDebug()<<1;
}
#include "inputdlg.h"
#include
inputdlg::inputdlg(QWidget* parent):QDialog (parent)
{
setWindowTitle(tr(" 标准输入对话框实例")) ;
nameLabel1 =new QLabel;
nameLabel1->setText(tr(" 姓名:"));
nameLabel2 =new QLabel;
nameLabel2->setText(tr(" 周何骏")) ; //姓名的初始值
nameLabel2->setFrameStyle(QFrame::Panel|QFrame::Sunken);
nameBtn =new QPushButton;
nameBtn->setText(tr(" 修改姓名")) ;
sexLabel1 =new QLabel;
sexLabel1->setText(tr(" 性别:"));
sexLabel2 =new QLabel;
sexLabel2->setText(tr(" 男")) ; //性别的初始值
sexLabel2->setFrameStyle(QFrame::Panel|QFrame::Sunken);
sexBtn =new QPushButton;
sexBtn->setText(tr(" 修改性别"));
ageLabel1 =new QLabel;
ageLabel1->setText(tr(" 年龄:"));
ageLabel2 =new QLabel;
ageLabel2->setText(tr("21")); //年龄的初始值
ageLabel2->setFrameStyle(QFrame::Panel|QFrame::Sunken);
ageBtn =new QPushButton;
ageBtn->setText(tr(" 修改年龄")) ;
scoreLabel1 =new QLabel;
scoreLabel1->setText(tr(" 成绩:"));
scoreLabel2 =new QLabel;
scoreLabel2->setText(tr ("80")); //成绩的初始值
scoreLabel2->setFrameStyle(QFrame::Panel|QFrame::Sunken);
scoreBtn =new QPushButton;
scoreBtn->setText(tr("修改成绩")) ;
mainLayout =new QGridLayout(this);
mainLayout->addWidget(nameLabel1,0,0);
mainLayout->addWidget(nameLabel2,0,1);
mainLayout->addWidget(nameBtn,0,2);
mainLayout->addWidget(sexLabel1,1,0);
mainLayout->addWidget(sexLabel2,1,1);
mainLayout->addWidget(sexBtn,1,2);
mainLayout->addWidget(ageLabel1,2,0);
mainLayout->addWidget(ageLabel2,2,1);
mainLayout->addWidget(ageBtn,2,2);
mainLayout->addWidget(scoreLabel1,3,0);
mainLayout->addWidget(scoreLabel2,3,1);
mainLayout->addWidget(scoreBtn,3,2);
mainLayout->setMargin(15);
mainLayout->setSpacing(10);
connect (nameBtn, SIGNAL (clicked()), this, SLOT (ChangeName()));
connect (sexBtn, SIGNAL (clicked()), this, SLOT (ChangeSex ()));
connect (ageBtn, SIGNAL (clicked()), this, SLOT (ChangeAge ()));
connect(scoreBtn,SIGNAL(clicked()) ,this,SLOT(ChangeScore()));
}
void inputdlg::ChangeName() //修改姓名
{
bool ok;
QString text=QInputDialog::getText(this,tr("标准字符串输入对话框"), tr("请输入姓名: "), QLineEdit::Normal, nameLabel2->text (), &ok);
if (ok && !text.isEmpty())
nameLabel2->setText(text);
}
void inputdlg::ChangeSex() //修改性别
{
QStringList Sexitems;
Sexitems << tr("男") << tr(" 女") ;
bool ok;
QString Sexitem = QInputDialog::getItem(this, tr("标准条目选择对话框"),
tr(" 请选择性别: "), Sexitems, 0, false, &ok);
if (ok && !Sexitem.isEmpty())
sexLabel2->setText(Sexitem);
}
void inputdlg::ChangeAge() //修改年龄
{
bool ok;
int age= QInputDialog::getInt(this, tr("标准 int 类型输入对话框n"),tr(" 请输入年龄: "), ageLabel2->text().toInt(&ok), 0, 100, 1, &ok);
if (ok)
ageLabel2->setText(QString(tr("%1")) . arg (age)) ;
}
void inputdlg::ChangeScore() //修改成绩
{
bool ok;
double score= QInputDialog::getDouble(this, tr("标准 double 类型输入对话框"),tr(" 请输入成绩: "),scoreLabel2->text().toDouble(&ok), 0, 100, 1, &ok);
if (ok)
scoreLabel2->setText(QString(tr("%1")) .arg(score));
}
#include "dialog.h"
#include
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Dialog w;
inputdlg w1;
w.show();
return a.exec();
}
标准输入对话框也是在应用程序中经常用到的