如何获得Qt中的系统默认字体设置? [英] How do you get System default font settings in Qt?

查看:1996
本文介绍了如何获得Qt中的系统默认字体设置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Qt构建一个桌面应用程序,我的开发机器是以日语区域设置赢得7 x64,标准系统字体是 Meiryo

一>。大多数的赢7 UI是在这个字体,虽然经典/较旧的程序,如UI字体定制窗口本身使用不同的字体,这是MS UI哥特式。
这并不妨碍我,直到我发现QtCreator与MS UI哥特式在一个地方建立我的应用程序,而在另一个地方Meiryo。例如,qlabels,qlineedits,qcombobox都使用MS UI哥特式,但是一个自定义完成器,后面我添加一个qtableview使用Meiryo。

我使用了QtCreator的Designer中的大部分UI,并且在代码中添加了完成者。如果我在设计器中将所有小部件的字体更改为Meiryo,那么当然应用程序将使用Meiryo,因此看起来是正确的,但是在这种情况下,我宁愿Qt自动选择任何系统的默认字体,因为win 7不会是唯一的我将使用这个程序。

更令我关注的是QApplication :: font()返回MS UI哥特式,这在我的例子中是错误的。当然,我可以使用QApplication :: setFont()来设置应用程序范围的字体,但是这样做会使本机的外观感觉没有微操作字体的全部目的。

所以我的问题是,


  1. Qt如何确定系统默认字体,如果是这样,
  2. 一个Qt的错误我该如何解决它?

  3. 我应该如何使用.ui文件,并且仍然有我的UI在运行时使用默认的系统字体?

我发现的一些澄清和事实


  1. 我希望我的应用程序为每个文本使用系统默认字体。

  2. 这个讨论说设计师将添加字体信息,无论你是否想要。所以Qt会尊重这个信息,而不是系统默认的字体。至少有人提到手动去除这个信息应该让Qt在运行时选择系统默认的字体。在我的开发机器QApplication :: font()返回错误的默认字体。但是怎么来一个QTableView我以后在代码中添加使用RIGHT字体?所以如果我找到QTableView找到这个信息的地方,我可以得到它的主要信息,并使用QApplication :: setFont()将其设置为应用程序。然后剩下的是手动删除所有的字体信息,然后HOPEFULLY它将工作。但是这就是为什么我们使用Qt在第一个地方不是吗?我可以想到两个可能的解决方案:


    1. 你可以用应用程序打包一个字体作为资源文件,这样所有的平台都可以使用这种字体,默认字体。

    2. QFont类有一个名为 defaultFamily() 。使用此功能,您可以手动设置整个 QApplication


    一个例子(主要方法) p>

      QApplication应用程序(argc,argv); 
    QFont字体;
    font.setFamily(font.defaultFamily());
    application.setFont(font);
    ...其余代码...


    I am building a desktop app using Qt, my dev machine is win 7 x64 with japanese locale, standard system font is Meiryo. Most of win 7 UI is in this font, though classic/older programs such as ui font customization window itself uses different font which is MS UI Gothic. This doesn't bother me until I found that QtCreator builds my app with MS UI Gothic in one place, and Meiryo in the other. For example, qlabels, qlineedits, qcombobox all uses MS UI Gothic, but a custom completer with a qtableview i add later uses Meiryo.

    I made most of the UI in QtCreator's Designer, and the completer I added in code. If I change all widgets' font to Meiryo in The Designer, then of course the app will use Meiryo therefore look right, but in this case I'd rather Qt pick whatever system's default font automatically for me because win 7 will not be the only platform I'll use this program.

    What raised my concern even more is that QApplication::font() returns MS UI Gothic, which is false in my case. Of course I can set app-wide font using QApplication::setFont() but that defeats the whole purpose of having the native look-n-feel without micromanaging fonts.

    So my question is,

    1. how do Qt determine system default font and,
    2. if this is a Qt Bug how do I go about getting around it?
    3. how should I use .ui files and still have my UI use default system font in runtime?

    Some clarifications and facts I found

    1. I want my app to use system default font for EVERY text.

    2. This discussion said that Designer will add font info regardless whether you want it or not. So Qt will honor this info rather than system default font. At least someone mentioned removing this information manually should make Qt pick system default font in runtime.

    3. In my dev machine QApplication::font() returns WRONG default font. BUT how come a QTableView I add later in code uses RIGHT font? Where did it get this info?

    So if I find where QTableView finds this info, I can get it in main, and set it app wide with QApplication::setFont(). Then what's left is manually removing all font info, then HOPEFULLY it will work. But this is like why we use Qt in the first place isn't it?

    解决方案

    I can think of two possible solution:

    1. You could pack with your application a font as a resource file, this way all platforms will use that font regardless the current systems default font.

    2. The QFont class has a method called defaultFamily(). Using this you could manually set the default font for your whole QApplication.

    An example (main method):

    QApplication application(argc, argv);
    QFont font;
    font.setFamily(font.defaultFamily());
    application.setFont(font);
    ...rest of your code...
    

    这篇关于如何获得Qt中的系统默认字体设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆