Qt4对话框之输入对话框

下面这个例子说明一个简单的对话框的使用。跟之前跟讲的C++类实例相应,也是有三个文件:一个是头文件,一个是源文件,一个是Main文件。

好了,不用多讲,代码如下:

inputDialog.h:

#ifndef INPUTDLG_H
#define INPUTDLG_H

#include <QtGui>

class InputDlg : public QDialog	
{
    Q_OBJECT
public:
    InputDlg();

public:
    QPushButton *nameButton;
    QPushButton *sexButton;
    QPushButton *ageButton;
    QPushButton *statureButton;
    
    QLabel *label1;
    QLabel *label2;
    QLabel *label3;
    QLabel *label4;
    QLabel *nameLabel;
    QLabel *sexLabel;
    QLabel *ageLabel;
    QLabel *statureLabel;

private slots:
    void slotName();
    void slotSex();
    void slotAge();
    void slotStature();
 
};


#endif

inputDialog.cpp:

#include "inputdialog.h"

InputDlg::InputDlg()
{

    setWindowTitle(tr("Input Dialog"));
	//设置对话框标题

    label1 = new QLabel(tr("Name : "));
    label2 = new QLabel(tr("Sex : "));
    label3 = new QLabel(tr("Age : "));
    label4 = new QLabel(tr("Stature : "));
    
    nameLabel = new QLabel(tr("LiMing"));
    nameLabel->setFrameStyle(QFrame::Panel|QFrame::Sunken);
    sexLabel = new QLabel(tr("male"));
    sexLabel->setFrameStyle(QFrame::Panel|QFrame::Sunken);
    ageLabel = new QLabel(tr("25"));
    ageLabel->setFrameStyle(QFrame::Panel|QFrame::Sunken);
    statureLabel = new QLabel("175.5");
    statureLabel->setFrameStyle(QFrame::Panel|QFrame::Sunken);
    
    nameButton = new QPushButton;
    nameButton->setIcon(QIcon(":/images/btn.png"));
    sexButton = new QPushButton;
    sexButton->setIcon(QIcon(":/images/btn.png"));
    ageButton = new QPushButton;
    ageButton->setIcon(QIcon(":/images/btn.png"));
    statureButton = new QPushButton;
    statureButton->setIcon(QIcon(":/images/btn.png"));
    

    QGridLayout *layout = new QGridLayout( this );
    int name = 0;
    int sex = 1;
    int age = 2;
    int stature = 3;
    layout->addWidget( label1, name, 0 );	
    layout->addWidget( nameLabel, name, 1 );
    layout->addWidget( nameButton, name, 2 );
    layout->addWidget( label2, sex, 0 );
    layout->addWidget( sexLabel, sex, 1 );
    layout->addWidget( sexButton, sex, 2 );
    layout->addWidget( label3, age, 0 );
    layout->addWidget( ageLabel, age, 1 );
    layout->addWidget( ageButton, age, 2 );
    layout->addWidget( label4, stature, 0 );
    layout->addWidget( statureLabel, stature, 1 );
    layout->addWidget( statureButton, stature, 2 );
    layout->setMargin(15);
    layout->setSpacing(10);
    layout->setColumnMinimumWidth(1,120);
    
    connect(nameButton,SIGNAL(clicked()),this,SLOT(slotName()));
    connect(sexButton,SIGNAL(clicked()),this,SLOT(slotSex()));
    connect(ageButton,SIGNAL(clicked()),this,SLOT(slotAge()));
    connect(statureButton,SIGNAL(clicked()),this,SLOT(slotStature()));
}

void InputDlg::slotName()
{                              
    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 InputDlg::slotSex()                    
{             
    QStringList list;
    list << tr("male") << tr("female");
    bool ok;
    QString sex = QInputDialog::getItem(this,tr("Sex"),
    			tr("Please select sex:"),list,0,false,&ok);
    if (ok)
    	sexLabel->setText(sex);
}                                                           
                                                            
void InputDlg::slotAge()                     
{                             
    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 InputDlg::slotStature()                     
{                             
    bool ok;
    double d = QInputDialog::getDouble(this,tr("Stature"),
    			tr("Please input stature:"),175.00,0,230.00,1,&ok);  
    if(ok)
    	statureLabel->setText(QString(tr("%1")).arg(d));
}

main.cpp

#include "inputdialog.h"
#include <QApplication>

int main( int argc, char **argv )
{
    QFont font("ZYSong18030",12);
    QApplication::setFont(font);
    	
    QApplication app( argc, argv );
    QTranslator translator;
    translator.load("inputdialog_zh");
    app.installTranslator(&translator);
    InputDlg *input = new InputDlg();
    input->show();
    return app.exec();
}

实现效果如下:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
等基本信息。下面是一个基于Qt对话框示例,可以输入姓名、性别、学号和生日: ```c++ #include <QDialog> #include <QLabel> #include <QLineEdit> #include <QComboBox> #include <QDateEdit> #include <QDialogButtonBox> #include <QHBoxLayout> #include <QVBoxLayout> class InfoDialog : public QDialog { Q_OBJECT public: InfoDialog(QWidget *parent = nullptr); QString getName() const; QString getGender() const; QString getStudentID() const; QDate getBirthday() const; private slots: void accept(); private: QLabel *nameLabel; QLineEdit *nameEdit; QLabel *genderLabel; QComboBox *genderComboBox; QLabel *studentIDLabel; QLineEdit *studentIDEdit; QLabel *birthdayLabel; QDateEdit *birthdayEdit; QDialogButtonBox *buttonBox; }; InfoDialog::InfoDialog(QWidget *parent) : QDialog(parent) { nameLabel = new QLabel(tr("Name:")); nameEdit = new QLineEdit; genderLabel = new QLabel(tr("Gender:")); genderComboBox = new QComboBox; genderComboBox->addItem(tr("Male")); genderComboBox->addItem(tr("Female")); studentIDLabel = new QLabel(tr("Student ID:")); studentIDEdit = new QLineEdit; birthdayLabel = new QLabel(tr("Birthday:")); birthdayEdit = new QDateEdit; birthdayEdit->setDisplayFormat("yyyy-MM-dd"); birthdayEdit->setCalendarPopup(true); birthdayEdit->setMaximumDate(QDate::currentDate()); buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel); connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept())); connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject())); QHBoxLayout *nameLayout = new QHBoxLayout; nameLayout->addWidget(nameLabel); nameLayout->addWidget(nameEdit); QHBoxLayout *genderLayout = new QHBoxLayout; genderLayout->addWidget(genderLabel); genderLayout->addWidget(genderComboBox); QHBoxLayout *studentIDLayout = new QHBoxLayout; studentIDLayout->addWidget(studentIDLabel); studentIDLayout->addWidget(studentIDEdit); QHBoxLayout *birthdayLayout = new QHBoxLayout; birthdayLayout->addWidget(birthdayLabel); birthdayLayout->addWidget(birthdayEdit); QVBoxLayout *mainLayout = new QVBoxLayout; mainLayout->addLayout(nameLayout); mainLayout->addLayout(genderLayout); mainLayout->addLayout(studentIDLayout); mainLayout->addLayout(birthdayLayout); mainLayout->addWidget(buttonBox); setLayout(mainLayout); setWindowTitle(tr("Information")); } QString InfoDialog::getName() const { return nameEdit->text(); } QString InfoDialog::getGender() const { return genderComboBox->currentText(); } QString InfoDialog::getStudentID() const { return studentIDEdit->text(); } QDate InfoDialog::getBirthday() const { return birthdayEdit->date(); } void InfoDialog::accept() { if (nameEdit->text().isEmpty() || studentIDEdit->text().isEmpty()) { QMessageBox::warning(this, tr("Warning"), tr("Name and Student ID are required.")); } else { QDialog::accept(); } } ``` 在主窗口中使用该对话框的示例代码: ```c++ #include <QApplication> #include <QPushButton> #include <QMessageBox> #include "infodialog.h" int main(int argc, char *argv[]) { QApplication a(argc, argv); QPushButton button("Show Info Dialog"); QObject::connect(&button, &QPushButton::clicked, [&](){ InfoDialog dialog; if (dialog.exec() == QDialog::Accepted) { QString name = dialog.getName(); QString gender = dialog.getGender(); QString studentID = dialog.getStudentID(); QDate birthday = dialog.getBirthday(); QMessageBox::information(nullptr, "Information", QString("Name: %1\nGender: %2\nStudent ID: %3\nBirthday: %4") .arg(name).arg(gender).arg(studentID).arg(birthday.toString("yyyy-MM-dd"))); } }); button.show(); return a.exec(); } ``` 运行后点击“Show Info Dialog”按钮,就可以弹出一个对话框输入基本信息。如果输入的姓名和学号为空,点击“OK”按钮时会弹出一个警告框提示用户。如果输入的信息都是正确的,点击“OK”按钮后会返回`QDialog::Accepted`,然后可以通过对话框的公共槽函数获取输入的信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值