widget.h:
#ifndef WIDGET_H #define WIDGET_H #include <QWidget> class Widget : public QWidget { Q_OBJECT public slots: void showFontDialog(); public: Widget(QWidget *parent = 0); ~Widget(); }; #endif // WIDGET_H
widget.cpp:
#include "widget.h" #include<QFont> #include<QDebug> #include<QPushButton> #include<QVBoxLayout> #include<QFontDialog> #include<QFontDatabase> Widget::Widget(QWidget *parent) : QWidget(parent) { this->resize(600,480); QFont qf=this->font(); qf.setFamily("仿宋"); qf.setPointSize(30); this->setFont(qf); //QFontDatabase db; //系统字体数据库 //qDebug()<<db.families()<<endl; QPushButton *qp_one=new QPushButton("用户名"); QPushButton *qp_two=new QPushButton("密码"); QVBoxLayout *qv=new QVBoxLayout(this); qv->addWidget(qp_one); qv->addWidget(qp_two); this->setLayout(qv); connect(qp_one,SIGNAL(clicked()),this,SLOT(showFontDialog())); connect(qp_two,SIGNAL(clicked()),this,SLOT(showFontDialog())); } void Widget::showFontDialog() { bool ok; // QFont qf=QFontDialog::getFont(&ok,this); QFont qf=QFontDialog::getFont(&ok,this->font(),this,"ff"); if(ok) { this->setFont(qf); } } Widget::~Widget() { }
main.cpp:
#include "widget.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); Widget w; w.show(); return a.exec(); }
效果: