qt对话框可以输入姓名性别学号生日
时间: 2024-02-27 14:29:03 浏览: 89
等基本信息。下面是一个基于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`,然后可以通过对话框的公共槽函数获取输入的信息。
阅读全文