Upload project files

Upload dtk-font and qt-font demo project files and build script.
This commit is contained in:
忘记、过去 2020-10-23 01:33:51 +08:00
parent b646cc685d
commit 77a4b4aace
15 changed files with 307 additions and 66 deletions

View File

@ -1,36 +0,0 @@
# FontDemo
#### Description
Simple Qt Demo of loading custom fonts for specific widgets, including qt-font demo and dtk-font demo using DTK.
#### Software Architecture
Software architecture description
#### Installation
1. xxxx
2. xxxx
3. xxxx
#### Instructions
1. xxxx
2. xxxx
3. xxxx
#### Contribution
1. Fork the repository
2. Create Feat_xxx branch
3. Commit your code
4. Create Pull Request
#### Gitee Feature
1. You can use Readme\_XXX.md to support different languages, such as Readme\_en.md, Readme\_zh.md
2. Gitee blog [blog.gitee.com](https://blog.gitee.com)
3. Explore open source project [https://gitee.com/explore](https://gitee.com/explore)
4. The most valuable open source project [GVP](https://gitee.com/gvp)
5. The manual of Gitee [https://gitee.com/help](https://gitee.com/help)
6. The most popular members [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/)

View File

@ -1,37 +1,9 @@
# FontDemo
#### 介绍
Simple Qt Demo of loading custom fonts for specific widgets, including qt-font demo and dtk-font demo using DTK.
#### 软件架构
软件架构说明
#### 安装教程
1. xxxx
2. xxxx
3. xxxx
简单的 Qt 应用程序 Demo实现为指定的 widget 加载自定义字体,包含 qt-font demo 以及使用 DTK 的 dtk-font demo。
#### 使用说明
1. xxxx
2. xxxx
3. xxxx
#### 参与贡献
1. Fork 本仓库
2. 新建 Feat_xxx 分支
3. 提交代码
4. 新建 Pull Request
#### 特技
1. 使用 Readme\_XXX.md 来支持不同的语言,例如 Readme\_en.md, Readme\_zh.md
2. Gitee 官方博客 [blog.gitee.com](https://blog.gitee.com)
3. 你可以 [https://gitee.com/explore](https://gitee.com/explore) 这个地址来了解 Gitee 上的优秀开源项目
4. [GVP](https://gitee.com/gvp) 全称是 Gitee 最有价值开源项目,是综合评定出的优秀开源项目
5. Gitee 官方提供的使用手册 [https://gitee.com/help](https://gitee.com/help)
6. Gitee 封面人物是一档用来展示 Gitee 会员风采的栏目 [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/)
运行 build.sh 编译 dtk-font 和 qt-font编译生成的可执行文件在 build_dtk 和 build_qt 文件夹中。

19
build.sh Executable file
View File

@ -0,0 +1,19 @@
#!/bin/sh
cd $PWD
# 编译 dtk-font
mkdir -p build_dtk && cd build_dtk
/usr/lib/qt5/bin/qmake ../dtk-font/dtk-font.pro -spec linux-g++ CONFIG+=qtquickcompiler && /usr/bin/make qmake_all
make -j4
cd ..
# 编译 qt-font
mkdir -p build_qt && cd build_qt
/usr/lib/qt5/bin/qmake ../qt-font/qt-font.pro -spec linux-g++ CONFIG+=qtquickcompiler && /usr/bin/make qmake_all
make -j4
exit

42
dtk-font/dtk-font.pro Normal file
View File

@ -0,0 +1,42 @@
#-------------------------------------------------
#
# Project created by QtCreator 2020-10-23T00:40:00
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = dtk-font
TEMPLATE = app
# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
CONFIG += c++11 link_pkgconfig
PKGCONFIG += dtkwidget
SOURCES += \
main.cpp \
mainwindow.cpp
HEADERS += \
mainwindow.h
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
RESOURCES += \
fonts.qrc

5
dtk-font/fonts.qrc Normal file
View File

@ -0,0 +1,5 @@
<RCC>
<qresource prefix="/">
<file>fonts/华康少女字体.ttf</file>
</qresource>
</RCC>

Binary file not shown.

15
dtk-font/main.cpp Normal file
View File

@ -0,0 +1,15 @@
#include "mainwindow.h"
#include <DApplication>
int main(int argc, char *argv[])
{
DApplication a(argc, argv);
a.loadTranslator(); // 加载翻译
MainWindow w;
w.show();
return a.exec();
}

68
dtk-font/mainwindow.cpp Normal file
View File

@ -0,0 +1,68 @@
#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;
}

29
dtk-font/mainwindow.h Normal file
View File

@ -0,0 +1,29 @@
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <DMainWindow>
#include <DPushButton>
DWIDGET_USE_NAMESPACE
class MainWindow : public DMainWindow
{
Q_OBJECT
public:
MainWindow(DMainWindow *parent = nullptr);
~MainWindow();
private:
QWidget *w = new QWidget; // w 是窗口的用户区,应当是所有窗口中控件的父(不包含标题栏及其上边的控件)
DPushButton *dbutton_1;
DPushButton *dbutton_2;
QPushButton *qbutton_1;
QPushButton *qbutton_2;
QFont font; // 自定义字体
};
#endif // MAINWINDOW_H

5
qt-font/fonts.qrc Normal file
View File

@ -0,0 +1,5 @@
<RCC>
<qresource prefix="/">
<file>fonts/华康少女字体.ttf</file>
</qresource>
</RCC>

Binary file not shown.

13
qt-font/main.cpp Normal file
View File

@ -0,0 +1,13 @@
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}

45
qt-font/mainwindow.cpp Normal file
View File

@ -0,0 +1,45 @@
#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;
}

24
qt-font/mainwindow.h Normal file
View File

@ -0,0 +1,24 @@
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QPushButton>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
QWidget *w = new QWidget; // w 是窗口的用户区,应当是所有窗口中控件的父
QPushButton *button_1;
QPushButton *button_2;
QFont font; // 自定义字体
};
#endif // MAINWINDOW_H

40
qt-font/qt-font.pro Normal file
View File

@ -0,0 +1,40 @@
#-------------------------------------------------
#
# Project created by QtCreator 2020-10-22T23:50:10
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = qt-font
TEMPLATE = app
# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
CONFIG += c++11
SOURCES += \
main.cpp \
mainwindow.cpp
HEADERS += \
mainwindow.h
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
RESOURCES += \
fonts.qrc