1. 程式人生 > >Qt5學習——實現標準輸入對話方塊

Qt5學習——實現標準輸入對話方塊

Qt5標準輸入對話方塊的例項


標頭檔案

1、inputdlg.h

#ifndef INPUTDLG_H
#define INPUTDLG_H

#include <QDialog>

class QWidget;
class QLabel;
class QPushButton;
class QGridLayout;

class InputDlg : public QDialog
{
    Q_OBJECT

public:
    InputDlg(QWidget* parent = 0);
    ~InputDlg();
    void showSelf();

private slots:
    void ChangeName();
    void ChangeSex();
    void ChangeAge();
    void ChangeScore();

private:
    void CreateName();
    void CreateSex();
    void CreateAge();
    void CreateScore();

    void AddItemInLayout();
    void connectSlot();

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

2、dialog.h

#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>

class QPushButton;
class QLineEdit;
class QGridLayout;
class QFrame;
class InputDlg;

class Dialog : public QDialog
{
    Q_OBJECT

public:
    Dialog(QWidget *parent = 0);
    ~Dialog();

private:
    void createFileDialog();
    void createColorDialog();
    void createFontDialog();
    void createInputBtn();
    void addGridLayout();
    void registerConnect();

private slots:
    void showFile();
    void showColor();
    void showFont();
    void showInputDlg();

private:
    //file dialog
    QPushButton *fileBtn;
    QLineEdit   *fileLineEdit;
    //color dialog
    QPushButton *colorBtn;
    QFrame *colorFrame;
    //font dialog
    QPushButton *fontBtn;
    QLineEdit *fontLineEdit;
    //input dialog
    QPushButton *inputBtn;
    InputDlg *inputDlg;

    // layout
    QGridLayout *mainLayout;
};

#endif // DIALOG_H

原始檔

1、inputdlg.cpp

#include "inputdlg.h"
#include <QWidget>
#include <QLabel>
#include <QPushButton>
#include <QGridLayout>
#include <QFrame>

InputDlg::InputDlg(QWidget* parent) : QDialog(parent)
{
    setWindowTitle(tr("標準輸入對話方塊的例項"));
    AddItemInLayout();
    connectSlot();
}

void InputDlg::showSelf()
{
    this->setVisible(true);
}

void InputDlg::CreateName()
{
    nameLabel1 = new QLabel;
    nameLabel1->setText(tr("姓名:"));
    nameLabel2 = new QLabel;
    nameLabel2->setText(tr("令狐沖"));
    nameLabel2->setFrameStyle(QFrame::Panel | QFrame::Sunken);
    nameBtn = new QPushButton;
    nameBtn->setText(tr("修改姓名"));
}

void InputDlg::CreateSex()
{
    sexLabel1 = new QLabel;
    sexLabel1->setText(tr("性別:"));
    sexLabel2 = new QLabel;
    sexLabel2->setText(tr("男"));
    sexLabel2->setFrameStyle(QFrame::Panel | QFrame::Sunken);
    sexBtn = new QPushButton;
    sexBtn->setText(tr("修改性別"));
}

void InputDlg::CreateAge()
{
    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("修改年齡"));
}

void InputDlg::CreateScore()
{
    scoreLabel1 = new QLabel;
    scoreLabel1->setText(tr("成績:"));
    scoreLabel2 = new QLabel;
    scoreLabel2->setText(tr("99"));
    scoreLabel2->setFrameStyle(QFrame::Panel | QFrame::Sunken);
    scoreBtn = new QPushButton;
    scoreBtn->setText(tr("修改成績"));
}

void InputDlg::AddItemInLayout()
{
    CreateName();
    CreateSex();
    CreateAge();
    CreateScore();

    mainLayout = new QGridLayout;
    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);
}

void InputDlg::connectSlot()
{
    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()
{

}

InputDlg::~InputDlg()
{
    if (nameLabel1) delete nameLabel1;
    if (sexLabel1) delete sexLabel1;
    if (ageLabel1) delete ageLabel1;
    if (scoreLabel1) delete scoreLabel1;
    if (nameLabel2) delete nameLabel2;
    if (sexLabel2) delete sexLabel2;
    if (ageLabel2) delete ageLabel2;
    if (scoreLabel2) delete scoreLabel2;

    if (nameBtn) delete nameBtn;
    if (sexBtn) delete sexBtn;
    if (ageBtn) delete ageBtn;
    if (scoreBtn) delete scoreBtn;
    if (mainLayout) delete mainLayout;
}

2、dialog.cpp

#include "dialog.h"
#include "inputdlg.h"
#include <QPushButton>
#include <QLineEdit>
#include <QGridLayout>
#include <QFileDialog>
#include <QFrame>
#include <QColorDialog>
#include <QPalette>
#include <QFontDialog>

Dialog::Dialog(QWidget *parent) : QDialog(parent)
{
    setWindowTitle(tr("各種標準對話方塊的例項"));
    createFileDialog();
    createColorDialog();
    createFontDialog();
    createInputBtn();
    addGridLayout();
    registerConnect();
}

void Dialog::createFileDialog()
{
    fileBtn = new QPushButton;
    fileBtn->setText(tr("文字標準對話方塊例項"));
    fileLineEdit = new QLineEdit;
}

void Dialog::createColorDialog()
{
    colorBtn = new QPushButton;
    colorBtn->setText(tr("顏色標準對話方塊例項"));
    colorFrame = new QFrame;
    colorFrame->setFrameShape(QFrame::Box);
    colorFrame->setAutoFillBackground(true);
}

void Dialog::createFontDialog()
{
    fontBtn = new QPushButton;
    fontBtn->setText(tr("字型標準對話方塊例項"));
    fontLineEdit = new QLineEdit;
    fontLineEdit->setText(tr("Welcome!"));
}

void Dialog::createInputBtn()
{
    inputBtn = new QPushButton;
    inputBtn->setText(tr("標準輸入對話方塊例項"));
}

void Dialog::addGridLayout()
{
    mainLayout = new QGridLayout(this);
    mainLayout->addWidget(fileBtn, 0, 0);
    mainLayout->addWidget(fileLineEdit, 0, 1);
    mainLayout->addWidget(colorBtn, 1, 0);
    mainLayout->addWidget(colorFrame, 1, 1);
    mainLayout->addWidget(fontBtn, 2, 0);
    mainLayout->addWidget(fontLineEdit, 2, 1);
    mainLayout->addWidget(inputBtn, 3, 0);
}

void Dialog::showFile()
{
    QString s = QFileDialog::getOpenFileName(this, "open file dialog", "/",
                    "C++ files(*.cpp)::C files(*.c)::Head files(*.h)");
    fileLineEdit->setText(s);
}

void Dialog::showColor()
{
    QColor c = QColorDialog::getColor(Qt::blue);
    if (c.isValid())
    {
        colorFrame->setPalette(QPalette(c));
    }
}

void Dialog::showFont()
{
    bool ok = true;
    QFont f = QFontDialog::getFont(&ok);
    if (ok)
    {
        fontLineEdit->setFont(f);
    }
}

void Dialog::showInputDlg()
{
    inputDlg = new InputDlg(this);
    inputDlg->showSelf();
}

void Dialog::registerConnect()
{
    connect(fileBtn, SIGNAL(clicked()), this, SLOT(showFile()));
    connect(colorBtn, SIGNAL(clicked()), this, SLOT(showColor()));
    connect(fontBtn, SIGNAL(clicked()), this, SLOT(showFont()));
    connect(inputBtn, SIGNAL(clicked()), this, SLOT(showInputDlg()));
}

Dialog::~Dialog()
{
    if (fileBtn) delete fileBtn;
    if (fileLineEdit) delete fileLineEdit;
    if (colorBtn) delete colorBtn;
    if (colorFrame) delete colorFrame;
    if (fontBtn) delete fontBtn;
    if (fontLineEdit) delete fontLineEdit;
    if (inputBtn) delete inputBtn;
    if (inputDlg) delete inputDlg;
    if (mainLayout) delete mainLayout;
}

3、main.cpp

#include "dialog.h"
#include <QApplication>
#include <QTextCodec>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QTextCodec::setCodecForTr(QTextCodec::codecForLocale());   //顯示中文設定

    Dialog w;
    w.show();

    return a.exec();
}

顯示結果

上圖中新增‘標準輸入對話方塊例項’
在這裡插入圖片描述

問題

在上面展示結果中,輸入對話方塊所需要展示的元素未能顯現,目前還沒有找到具體是什麼原因導致的。如果看官看到問題所在,望留言指正,再此先感謝。