[QT]QT教程之实例分析[一]文件 颜色和字体对话框

重点知识已近在代码里注释...

请仔细看代码

本文原创

转载请保留此链接  http://blog.csdn.net/siren0203

头文件

standardialog .h

#ifndef STANDARDIALOG_H #define STANDARDIALOG_H #include <QObject> #include <QDialog> class QGridLayout; class QPushButton; class QLineEdit; class QFrame; class standardialog:public QDialog{ Q_OBJECT public: //构造函数 standardialog(QWidget *parent=0); //私有成员 private: QGridLayout *gridLayout; QPushButton *fontButton; QPushButton *colorButton; QPushButton *fileButton; QLineEdit *fileLineEdit; QLineEdit *fontLineEdit; QFrame *colorFrame; private slots: void slotOpenFileDlg(); void slotOpenColorDlg(); void slotOpenFontDlg(); }; #endif // STANDARDIALOG_H

 

在看 main.cpp

#include <QDialog> #include <QApplication> #include <QGridLayout> #include <QPushButton> #include <QLineEdit> #include <QFileDialog> #include <QFontDialog> #include <QColorDialog> #include "standardialog.h" standardialog::standardialog(QWidget *parent):QDialog(parent){ //设置窗口标题 setWindowTitle("StanDarDialog"); //实例化布局管理器 gridLayout=new QGridLayout(this); //设置按钮和输入框 fontButton=new QPushButton("font Button"); fileButton =new QPushButton("file Button"); colorButton=new QPushButton("color Button"); fileLineEdit=new QLineEdit(this); fontLineEdit=new QLineEdit(this); fontLineEdit->setText("font line edit"); //实例一个框架 FRAME colorFrame =new QFrame; colorFrame->setFrameShape(QFrame::Box); colorFrame->setAutoFillBackground(true); gridLayout->addWidget(fileButton,0,0); gridLayout->addWidget(fileLineEdit,0,1); gridLayout->addWidget(colorButton,1,0); gridLayout->addWidget(colorFrame,1,1); gridLayout->addWidget(fontButton,2,0); gridLayout->addWidget(fontLineEdit,2,1); //设置间距 gridLayout->setMargin(5); gridLayout->setSpacing(10); //关联信号和槽 connect(fontButton,SIGNAL(clicked()),this,SLOT(slotOpenFontDlg())); connect(fileButton,SIGNAL(clicked()),this,SLOT(slotOpenFileDlg())); connect(colorButton,SIGNAL(clicked()),this,SLOT(slotOpenColorDlg())); //设置布局管理器 setLayout(gridLayout); } //颜色 对话框 void standardialog::slotOpenColorDlg(){ QColor color=QColorDialog::getColor(); if(color.isValid()){ colorFrame->setPalette(QPalette(color)); } } //文件对话框 void standardialog::slotOpenFileDlg(){ QString filename=QFileDialog::getOpenFileName(this,"open this dialog","/"); fileLineEdit->setText(filename.toAscii()); } //字体对话框 void standardialog::slotOpenFontDlg(){ bool ok; QFont font=QFontDialog::getFont(&ok); if(ok){ fontLineEdit->setFont(font); } } int main(int argc,char ** argv){ QApplication app(argc,argv); standardialog dialog; dialog.show(); return app.exec(); }

 

 

效果.

[QT]QT教程之实例分析[一]文件 颜色和字体对话框_第1张图片

你可能感兴趣的:(框架,File,dialog,qt,button,Signal)