font-demo/dtk-font/mainwindow.cpp

69 lines
2.1 KiB
C++
Raw Permalink Normal View History

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