font-demo/qt-font/mainwindow.cpp
zty199 77a4b4aace Upload project files
Upload dtk-font and qt-font demo project files and build script.
2020-10-23 01:33:51 +08:00

46 lines
1.3 KiB
C++

#include "mainwindow.h"
#include <QFontDatabase>
#include <QLayout>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent),
// 实例化窗口控件
button_1(new QPushButton),
button_2(new QPushButton)
{
// 初始化主窗口
setCentralWidget(w); // 将 w 作为窗口的用户部分
setFixedSize(400, 300); // 改变窗口大小应当改变 MainWindow 的大小
// 载入字体
int loadedFontID = QFontDatabase::addApplicationFont(":/fonts/华康少女字体.ttf");
QStringList loadedFontFamilies = QFontDatabase::applicationFontFamilies(loadedFontID);
if(!loadedFontFamilies.isEmpty())
font = loadedFontFamilies.at(0);
// 设置按钮样式
button_1->setParent(w);
button_1->resize(300, 100);
button_1->setText("这是一个带有字体样式的 QPushButton");
button_1->setFont(font);
button_2->setParent(w);
button_2->resize(300, 100);
button_2->setText("这是一个没有字体样式的 QPushButton");
// 设置按钮布局
QVBoxLayout *layout = new QVBoxLayout;
layout->setAlignment(Qt::AlignCenter);
layout->addWidget(button_1);
layout->addSpacing(50);
layout->addWidget(button_2);
w->setLayout(layout);
}
MainWindow::~MainWindow()
{
delete w;
}