目录
效果图:
QDialog+QFileDialog文件导入+栅格布局
dialog.h
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
#include <QString>
#include <QPushButton>
#include <QLineEdit>
#include <QGridLayout>
#include <QFileDialog>
#include "inputdlg.h"
class Dialog : public QDialog
{
Q_OBJECT
public:
Dialog(QWidget *parent = nullptr);
~Dialog();
private:
QPushButton *fileBtn;
QLineEdit *fileLineEdit;
QGridLayout *mainLayout;
QPushButton *inputBtn;
InputDlg *inputDlg;
private slots:
void showFiles();
void showInputDlg();
};
#endif // DIALOG_H
dialog.cpp
#include "dialog.h"
Dialog::Dialog(QWidget *parent)
: QDialog(parent)
{
setWindowTitle(QStringLiteral("登记管理"));
fileBtn=new QPushButton;//各个控件对象的初始化
fileBtn->setText(QStringLiteral("文件导入"));
fileLineEdit=new QLineEdit;//用来显示选择的文件名
inputBtn=new QPushButton;
inputBtn->setText(QStringLiteral("成绩登记系统"));
//添加布局管理
mainLayout = new QGridLayout(this);
mainLayout->addWidget(fileBtn,0,0);
mainLayout->addWidget(fileLineEdit,0,1);
mainLayout->addWidget(inputBtn,3,0);
//事件关联
connect(fileBtn,SIGNAL(clicked()),this,SLOT(showFiles()));
connect(inputBtn,SIGNAL(clicked()),this,SLOT(showInputDlg()));
}
void Dialog::showFiles(){
QString s=QFileDialog::getOpenFileName(this,"Open file dialog","/",
"C++ files(*.cpp);;C files(*.c);;Head files(*.h)");
fileLineEdit->setText(s);
}
void Dialog::showInputDlg(){
inputDlg = new InputDlg(this);
inputDlg->show();
}
Dialog::~Dialog()
{
}
imputdlg.h
#ifndef INPUTDLG_H
#define INPUTDLG_H
#include <QDialog>
#include <QLabel>
#include <QPushButton>
#include <QGridLayout>
#include <QString>
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
inputdlg.cpp
#include "inputdlg.h"
InputDlg::InputDlg(QWidget *parent):QDialog(parent)
{
setWindowTitle(QStringLiteral("成绩等级系统"));
//姓名
nameLabel1=new QLabel;
nameLabel1->setText(QStringLiteral("姓名:"));
nameLabel2=new QLabel;
nameLabel2->setText(QStringLiteral("邓超"));
nameLabel2->setFrameStyle(QFrame::Panel|QFrame::Sunken);
nameBtn=new QPushButton;
nameBtn->setText(QStringLiteral("修改姓名"));
//性别
sexLabel1=new QLabel;
sexLabel1->setText(QStringLiteral("性别:"));
sexLabel2=new QLabel;
sexLabel2->setText(QStringLiteral("男"));
sexLabel2->setFrameStyle(QFrame::Panel|QFrame::Sunken);
sexBtn=new QPushButton;
sexBtn->setText(QStringLiteral("修改性别"));
//年龄
ageLabel1 = new QLabel;
ageLabel1->setText(QStringLiteral("年龄:"));
ageLabel2=new QLabel;
ageLabel2->setText(QStringLiteral("22"));
ageLabel2->setFrameStyle(QFrame::Panel|QFrame::Sunken);
ageBtn=new QPushButton;
ageBtn->setText(QStringLiteral("修改年龄"));
//成绩
scoreLabel1 = new QLabel;
scoreLabel1->setText(QStringLiteral("成绩:"));
scoreLabel2 = new QLabel;
scoreLabel2->setText(QStringLiteral("98"));
scoreLabel2->setFrameStyle(QFrame::Panel|QFrame::Sunken);
scoreBtn=new QPushButton;
scoreBtn->setText(QStringLiteral("修改成绩"));
//布局设计
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(50);
//事件关联
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,QStringLiteral("修改姓名"),QStringLiteral("输入姓名:"),
QLineEdit::Normal,nameLabel2->text(),&ok);
if(ok&&!text.isEmpty()){
nameLabel2->setText(text);
}
}
void InputDlg::ChangeAge(){
bool ok;
int age=QInputDialog::getInt(this,QStringLiteral("修改年龄"),QStringLiteral("输入年龄:"),
ageLabel2->text().toInt(&ok),0,100,1,&ok);
if(ok){
ageLabel2->setText(QString(QStringLiteral("%1")).arg(age));//注意将qstring转换成整型
}
}
void InputDlg::ChangeSex(){
QStringList SexItems;
SexItems<<QStringLiteral("男")<<QStringLiteral("女");
bool ok;
QString SexItem=QInputDialog::getItem(this,QStringLiteral("修改性别"),
QStringLiteral("请选择性别:"),SexItems,0,false,&ok);
if(ok&&!SexItem.isEmpty()){
sexLabel2->setText(SexItem);
}
}
void InputDlg::ChangeScore(){
bool ok;
int score=QInputDialog::getInt(this,QStringLiteral("修改分数"),QStringLiteral("输入分数:"),
ageLabel2->text().toInt(&ok),0,100,1,&ok);
if(ok){
scoreLabel2->setText(QString(QStringLiteral("%1")).arg(score));//注意将qstring转换成整型
}
}
main.cpp
#include "dialog.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Dialog w;
w.show();
return a.exec();
}