1. 程式人生 > >【Qt】標準輸入對話方塊

【Qt】標準輸入對話方塊

標準輸入對話方塊是在標準字型對話方塊的基礎上完成的

參見:http://blog.csdn.net/ldan508/article/details/51388526

【實現效果】

        

【新增程式碼】

//inputdlg.h

<span style="font-family:Microsoft YaHei;font-size:18px;">#ifndef INPUTDLG_H
#define INPUTDLG_H

#include<QLabel>
#include<QPushButton>
#include<QGridLayout>
#include<QDialog>

class InputDlg : public QDialog
{
    Q_OBJECT

public:
    InputDlg(QWidget* parent=0);
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
</span>

//inputdlg.cpp

<span style="font-family:Microsoft YaHei;font-size:18px;">#include "inputdlg.h"

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()
{

}

void InputDlg::ChangeSex()
{

}

void InputDlg::ChangeAge()
{

}

void InputDlg::ChangeScore()
{

}
</span>