QT中將外部字型載入到程式中

2021-09-25 21:15:09 字數 1116 閱讀 6203

在qt開發中,會遇到有些電腦沒安裝某種或者某一種字型,又不能每台電腦去安裝。這時候,我們就要考慮在程式中將外部字型檔案當做資源一樣載入,保證顯示效果一致。

main.cpp

#include

"widget.h"

#include

#include

#include

#include

#include

qstring loadfontfamilyfromfiles

(const qstring &fontfilename)

int loadedfontid = qfontdatabase:

:(fontfile.

readall()

);qstringlist loadedfontfamilies = qfontdatabase:

:(loadedfontid);if

(!loadedfontfamilies.

empty()

) fontfile.

close()

;return font;

}int

main

(int argc,

char

*ar**)

// else

//

return a.

exec()

;}

widget.cpp檔案:

#include

"widget.h"

#include

"ui_widget.h"

#include

widget:

:widget

(qwidget *parent)

:qwidget

(parent),ui

(new ui:

:widget)

widget::~

widget()

工程檔案:

該demo的github位址是:

參考:

QT中呼叫外部程式的方法

上個月有人寫信問我這個問題,當時做了比較詳細的回答,把回答內容放上來,也許對其它 人會有些用處的吧。有些地方我的理解可能也不正確,歡迎指正 問題 回答 1 完全可以使用fork,execv函式,其實qprocess類只是對這些底層函式的封裝而 已,但是考慮到使用qprocess的話,不需要自己處理程...

Qt中呼叫外部程式開啟檔案

qdesktopservices openurl qurl fromlocalfile filename filename 檔名,如c 新建 microsoft word 文件.doc qurl fromlocalfile 一定要用這個方法,因為檔名或路徑中中文或空格,不然打不開檔案.直接用qurl...

Qt中執行外部程式介紹 QProcess

在qt中如何呼叫外部程式呢?最近遇到這個問題,解決方法如下 用qprocess類解決這個問題 如執行a.exe,帶兩個引數為 z20,q85 第一種方法 使用 qprocess類靜態函式 qprocess startdetached const qstring program const qstri...