1、新建"QT GUI",基类选择"QDialog',取消“创建界面”
2、dialog.h中
#ifndef DIALOG_H#define DIALOG_H#include#include #include #include class Dialog : public QDialog{ Q_OBJECTpublic: Dialog(QWidget *parent = 0); ~Dialog();private: QPushButton *fontBtn; QLineEdit *fontLineEdit; QGridLayout *mainLayout;private slots: void showFont();};#endif // DIALOG_H
2、dialog.cpp中
#include "dialog.h"#includeDialog::Dialog(QWidget *parent) : QDialog(parent){ //控件初始化 fontBtn = new QPushButton; fontBtn->setText(tr("字体标准对话框")); fontLineEdit = new QLineEdit; fontLineEdit->setText(tr("welcome!")); //添加布局管理 mainLayout = new QGridLayout(this); //注意一定要有一个this mainLayout->addWidget(fontBtn, 2, 0); mainLayout->addWidget(fontLineEdit, 2, 1); //信号与槽 connect(fontBtn, SIGNAL(clicked()), this, SLOT(showFont()));}Dialog::~Dialog(){}void Dialog::showFont(){ bool ok; QFont f = QFontDialog::getFont(&ok); if(ok){ fontLineEdit->setFont(f); }}
3、效果
、
QFont getFont
( bool *ok; //如果"ok",返回用户选择的字体,否则,返回函数默认字体 QWidget *parent = 0; );