版权声明:本文为博主原创文章,如有需要,可以转载,但请注明出处。 https://blog.csdn.net/xunye_dream/article/details/84202640
接续上节: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();
}
显示结果
问题
在上面展示结果中,输入对话框所需要展示的元素未能显现,目前还没有找到具体是什么原因导致的。如果看官看到问题所在,望留言指正,再此先感谢。