基本对话框--输入对话框
Dialog
exdialog.h
运行图 文件对话框 颜色对话框 字体对话框 输入对话框-字符串 输入对话框-选择 Item 输入对话框-数字
Dialog
Dialog.h
# ifndef DIALOG_H
# define DIALOG_H
# include "exdialog.h"
# include <QDialog>
# include <QPushButton>
# include <QLineEdit>
# include <QGridLayout>
# include <QFileDialog>
# include <QColorDialog>
# include <QFontDialog>
# include <QLabel>
class Dialog : public QDialog
{
Q_OBJECT
public :
Dialog ( QWidget * parent = nullptr ) ;
~ Dialog ( ) ;
private :
QPushButton* FileBtn;
QLineEdit* FileLineEdit;
QGridLayout* MainLayout;
QPushButton* ColorBtn;
QFrame* ColorFrame;
QPushButton* FontBtn;
QLineEdit* FontLineEdit;
QPushButton* ExPutBtn;
ExDialog* Exdialog;
private slots:
void ShowFile ( ) ;
void ShowColor ( ) ;
void ShowFont ( ) ;
void ShowExPutDlg ( ) ;
} ;
# endif
Dialog.cpp
# include "dialog.h"
Dialog :: Dialog ( QWidget * parent)
: QDialog ( parent)
{
FileBtn = new QPushButton ( tr ( "文件标准对话框" ) ) ;
FileLineEdit = new QLineEdit;
ColorBtn = new QPushButton ( tr ( "颜色标准对话框" ) ) ;
ColorFrame = new QFrame;
ColorFrame-> setFrameShape ( QFrame:: Box) ;
ColorFrame-> setAutoFillBackground ( true ) ;
FontBtn = new QPushButton ( tr ( "字体标准对话框实例" ) ) ;
FontLineEdit = new QLineEdit ( tr ( "welcome!" ) ) ;
ExPutBtn = new QPushButton ( tr ( "标准输入对话框实例" ) ) ;
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 ( ExPutBtn, 3 , 0 , 1 , 2 ) ;
connect ( FileBtn, SIGNAL ( clicked ( ) ) , this , SLOT ( ShowFile ( ) ) ) ;
connect ( ColorBtn, SIGNAL ( clicked ( ) ) , this , SLOT ( ShowColor ( ) ) ) ;
connect ( FontBtn, SIGNAL ( clicked ( ) ) , this , SLOT ( ShowFont ( ) ) ) ;
connect ( ExPutBtn, SIGNAL ( clicked ( ) ) , this , SLOT ( ShowExPutDlg ( ) ) ) ;
}
Dialog :: ~ Dialog ( ) { }
void Dialog :: ShowFile ( )
{
QString s = QFileDialog :: getOpenFileName ( this , QObject :: tr ( "打开文件" ) , "/" ) ;
FileLineEdit-> setText ( s) ;
}
void Dialog :: ShowColor ( )
{
QColor c = QColorDialog :: getColor ( Qt:: blue) ;
if ( c. isValid ( ) )
{
ColorFrame-> setPalette ( QPalette ( c) ) ;
}
}
void Dialog :: ShowFont ( )
{
bool Ok;
QFont f = QFontDialog :: getFont ( & Ok) ;
if ( Ok)
{
FontLineEdit-> setFont ( f) ;
}
}
void Dialog :: ShowExPutDlg ( )
{
Exdialog = new ExDialog ( this ) ;
Exdialog-> show ( ) ;
}
exdialog.h
exdialog.h
# ifndef EXDIALOG_H
# define EXDIALOG_H
# include <QDialog>
# include <QLabel>
# include <QPushButton>
# include <QGridLayout>
# include <QInputDialog>
# include <QLineEdit>
class ExDialog : public QDialog
{
Q_OBJECT
public :
explicit ExDialog ( QWidget * parent = nullptr ) ;
private slots:
void ChangName ( ) ;
void ChangSex ( ) ;
void ChangAge ( ) ;
void ChangScore ( ) ;
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* ExLayout;
signals:
} ;
# endif
exdialog.cpp
# include "exdialog.h"
ExDialog :: ExDialog ( QWidget * parent)
: QDialog{ parent}
{
setWindowTitle ( tr ( "标准输入对话框" ) ) ;
NameLabel1 = new QLabel ( tr ( "姓名:" ) ) ;
NameLabel2 = new QLabel ( tr ( "小明" ) ) ;
NameLabel2-> setFrameStyle ( QFrame:: Panel| QFrame:: Sunken) ;
NameBtn = new QPushButton ( tr ( "修改姓名" ) ) ;
SexLabel1 = new QLabel ( tr ( "性别:" ) ) ;
SexLabel2 = new QLabel ( tr ( "男" ) ) ;
SexLabel2-> setFrameStyle ( QFrame:: Panel| QFrame:: Sunken) ;
SexBtn = new QPushButton ( tr ( "修改性别" ) ) ;
AgeLabel1 = new QLabel ( tr ( "年龄:" ) ) ;
AgeLabel2 = new QLabel ( tr ( "21" ) ) ;
AgeLabel2-> setFrameStyle ( QFrame:: Panel| QFrame:: Sunken) ;
AgeBtn = new QPushButton ( tr ( "修改年龄" ) ) ;
ScoreLabel1 = new QLabel ( tr ( "成绩:" ) ) ;
ScoreLabel2 = new QLabel ( tr ( "80" ) ) ;
ScoreLabel2-> setFrameStyle ( QFrame:: Panel| QFrame:: Sunken) ;
ScoreBtn = new QPushButton ( tr ( "修改成绩" ) ) ;
ExLayout = new QGridLayout ( this ) ;
ExLayout-> setMargin ( 15 ) ;
ExLayout-> setSpacing ( 10 ) ;
ExLayout-> addWidget ( NameLabel1, 0 , 0 ) ;
ExLayout-> addWidget ( NameLabel2, 0 , 1 ) ;
ExLayout-> addWidget ( NameBtn, 0 , 2 ) ;
ExLayout-> addWidget ( SexLabel1, 1 , 0 ) ;
ExLayout-> addWidget ( SexLabel2, 1 , 1 ) ;
ExLayout-> addWidget ( SexBtn, 1 , 2 ) ;
ExLayout-> addWidget ( AgeLabel1, 2 , 0 ) ;
ExLayout-> addWidget ( AgeLabel2, 2 , 1 ) ;
ExLayout-> addWidget ( AgeBtn, 2 , 2 ) ;
ExLayout-> addWidget ( ScoreLabel1, 3 , 0 ) ;
ExLayout-> addWidget ( ScoreLabel2, 3 , 1 ) ;
ExLayout-> addWidget ( ScoreBtn, 3 , 2 ) ;
connect ( NameBtn, SIGNAL ( clicked ( ) ) , this , SLOT ( ChangName ( ) ) ) ;
connect ( SexBtn, SIGNAL ( clicked ( ) ) , this , SLOT ( ChangSex ( ) ) ) ;
connect ( AgeBtn, SIGNAL ( clicked ( ) ) , this , SLOT ( ChangAge ( ) ) ) ;
connect ( ScoreBtn, SIGNAL ( clicked ( ) ) , this , SLOT ( ChangScore ( ) ) ) ;
}
void ExDialog :: ChangName ( )
{
bool Ok;
QString BmpText = QInputDialog :: getText ( this , tr ( "标准字符串输入对话框" ) , tr ( "请输入姓名:" ) , QLineEdit:: Normal, NameLabel2-> text ( ) , & Ok) ;
if ( Ok && ! BmpText. isEmpty ( ) )
{
NameLabel2-> setText ( BmpText) ;
}
}
void ExDialog :: ChangSex ( )
{
QStringList SexItems;
SexItems << tr ( "男" ) << tr ( "女" ) ;
bool Ok;
QString SexItem= QInputDialog :: getItem ( this , tr ( "标准条目选择对话框" ) , tr ( "请选择性别:" ) , SexItems, 0 , false , & Ok) ;
if ( Ok && ! SexItem. isEmpty ( ) )
{
SexLabel2-> setText ( SexItem) ;
}
}
void ExDialog :: ChangAge ( )
{
bool Ok;
int nAge = QInputDialog :: getInt ( this , tr ( "标准int输入对话框" ) , tr ( "请输入年龄:" ) , AgeLabel2-> text ( ) . toInt ( & Ok) , 0 , 100 , 1 , & Ok) ;
if ( Ok)
{
AgeLabel2-> setText ( QString ( tr ( "%1" ) . arg ( nAge) ) ) ;
}
}
void ExDialog :: ChangScore ( )
{
bool Ok;
double nScore = QInputDialog :: getDouble ( this , tr ( "标准int输入对话框" ) , tr ( "请输入年龄:" ) , ScoreLabel2-> text ( ) . toDouble ( & Ok) , 0 , 200 , 1 , & Ok) ;
if ( Ok)
{
ScoreLabel2-> setText ( QString ( tr ( "%1" ) . arg ( nScore) ) ) ;
}
}
运行图
文件对话框
颜色对话框
字体对话框
输入对话框-字符串
输入对话框-选择 Item
输入对话框-数字