首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Qt设计器:无法更改字体大小

Qt设计器:无法更改字体大小
EN

Stack Overflow用户
提问于 2015-02-05 09:08:01
回答 1查看 3.4K关注 0票数 2

我在选项卡上有一些标签(即tabWidget),当我更改fontsize时,它确实会改变,但是当我保存文件时,它们都会切换回其他的东西(默认的或其他的)。这里发生什么事情?!

EN

Stack Overflow用户

发布于 2015-03-27 12:07:39

我认为您需要使用样式表来设置字体。不久前,我正在寻找信息,我找到了一个解决方案,阅读Qt文档并使用一些正式的Qt示例。

这里有一个例子。按照自己的要求更改css。

.pro文件

代码语言:javascript
运行
复制
HEADERS       = mainwindow.h 
FORMS         = mainwindow.ui 
RESOURCES     = stylesheet.qrc
SOURCES       = main.cpp \
                mainwindow.cpp \

stylesheet.qrc

代码语言:javascript
运行
复制
<RCC>
<qresource prefix="/">
    <file>qss/cool.qss</file>
</qresource>
</RCC>

main.cpp

代码语言:javascript
运行
复制
#include <QtWidgets>

#include "mainwindow.h"

int main(int argc, char *argv[])
{
    Q_INIT_RESOURCE(stylesheet);

    QApplication app(argc, argv);
    MainWindow window;
    window.show();
    return app.exec();
}

mainwindow.h

代码语言:javascript
运行
复制
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QtWidgets>

#include "ui_mainwindow.h"

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow();

    Ui::MainWindow ui;

private:
    void loadStyleSheet(const QString &sheetName);
};

#endif

mainwindow.cpp

代码语言:javascript
运行
复制
#include <QtWidgets>

#include "mainwindow.h"

MainWindow::MainWindow()
{
    ui.setupUi(this);
    loadStyleSheet("Cool");
}

void MainWindow::loadStyleSheet(const QString &sheetName)
{
    QFile file(":/qss/" + sheetName.toLower() + ".qss");
    file.open(QFile::ReadOnly);
    QString styleSheet = QString::fromLatin1(file.readAll());

    qApp->setStyleSheet(styleSheet);
}

最后,最重要的文件,cool.qss:

代码语言:javascript
运行
复制
QTabWidget::pane { /* The tab widget frame */
    border: 2px solid #C2C7CB;
}

QTabWidget::tab-bar {
    left: 5px; /* move to the right by 5px */
}

/* Style the tab using the tab sub-control. Note that
    it reads QTabBar _not_ QTabWidget */
QTabBar::tab {
    background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
                                stop: 0 #E1E1E1, stop: 0.4 #DDDDDD,
                                stop: 0.5 #D8D8D8, stop: 1.0 #D3D3D3);
    border: 2px solid #C4C4C3;
    border-bottom-color: #C2C7CB; /* same as the pane color */
    border-top-left-radius: 4px;
    border-top-right-radius: 4px;
    /* You should change min-width according to the
        lenght of your tab text */
    min-width: 14ex;
    padding: 4px;
    font: bold 14px;
}

QTabBar::tab:selected, QTabBar::tab:hover {
    background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
                                stop: 0 #fafafa, stop: 0.4 #f4f4f4,
                                stop: 0.5 #e7e7e7, stop: 1.0 #fafafa);
}

QTabBar::tab:selected {
    border-color: #9B9B9B;
    border-bottom-color: #C2C7CB; /* same as pane color */
}

QTabBar::tab:!selected {
    margin-top: 2px; /* make non-selected tabs look smaller */
}

在这个文件中,您有更多不必要的代码,但是使用所有属性可能是很重要的。

无论如何,您需要根据您的要求更改以下行。

代码语言:javascript
运行
复制
font: bold 14px;

检查最小宽度对于字体大小也是很重要的.

票数 3
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28339948

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档