46 lines
1.3 KiB
C++
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;
|
||
|
}
|