一、演示效果如下:
二、工程头文件如下:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QFontComboBox>
#include <QLabel>
#include <QMainWindow>
#include <QPushButton>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
private slots:
void txtButton();
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
QFontComboBox *fontComboBox;
QPushButton *button;
QLabel *label;
QFont f;
};
三、mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QLabel>
#include <QDebug>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->setWindowTitle("测试");
fontComboBox = new QFontComboBox(this);
button = new QPushButton(this);
label = new QLabel(this);
label ->setGeometry(QRect(50,150,400,50));
button->setText("确认字体设置");
button->move(180,50);
connect(button,SIGNAL(released()),this,SLOT(txtButton()));
fontComboBox->setGeometry(QRect(50,50,120,25));
}
MainWindow::~MainWindow()
{
delete ui;
label= nullptr;
button = nullptr;
}
//槽函数
void MainWindow::txtButton()
{
label->setText("选择的字体:"+fontComboBox->currentText());
f = fontComboBox->currentFont();
f.setPointSize(30);//设置字体大小
label->setFont(f);
}
四、main.cpp文件
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
温故知新,备忘一下。