C++ Qt - QPushButton 文本格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2990060/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 11:43:23  来源:igfitidea点击:

Qt - QPushButton text formatting

c++qtformattingbutton

提问by Narek

I have a QPushButton and on that I have a text and and icon. I want to make the text on the button to be bold and red. Looked at other forums, googled and lost my hope. Seems there is no way to do that if the button has an icon (of course if you don't creat a new icon wich is text+former icon). Is that the only way? Anyone has a better idea?

我有一个 QPushButton,上面有一个文本和图标。我想让按钮上的文字变成粗体和红色。查看其他论坛,用谷歌搜索并失去了我的希望。如果按钮有一个图标,似乎没有办法做到这一点(当然,如果你不创建一个新的图标,即文本+前一个图标)。这是唯一的方法吗?有人有更好的主意吗?

回答by Harald Scheirich

You really don't need to subclass to change the formatting of your button, rather use stylesheets e.g.

你真的不需要子类来改变你的按钮的格式,而是使用样式表,例如

QPushButton {
    font-size: 18pt;
    font-weight: bold;
    color: #ff0000;
}

Applying this to the button that you want to change will make the buttons text 18pt, bold and red. You can apply via widget->setStyleSheet()

将此应用于要更改的按钮将使按钮文本变为 18pt、粗体和红色。您可以通过以下方式申请widget->setStyleSheet()

Applying this to a widget in the hierarchy above will style all the buttons underneath, the QT stylesheetmechanism is very flexible and fairly well documented.

将其应用于上面层次结构中的小部件将设置下面的所有按钮的样式QT 样式表机制非常灵活且文档齐全。

You can set stylesheets in the designer too, this will style the widget that you are editing immediately

您也可以在设计器中设置样式表,这将立即为您正在编辑的小部件设置样式

回答by Naruto

you make the subclass of "QPushbutton", then override the paint event, there you modify the text to your wish.

您创建“QPushbutton”的子类,然后覆盖绘制事件,然后根据需要修改文本。

here it is,

这里是,

class button : public QPushButton
    {
    Q_OBJECT

public:
    button(QWidget *parent = 0)
        {

        }
    ~button()
        {

        }

    void paintEvent(QPaintEvent *p2)
        {

        QPushButton::paintEvent(p2);

            QPainter paint(this);
            paint.save();
            QFont sub(QApplication::font());
            sub.setPointSize(sub.pointSize() + 7);
            paint.setFont(sub);
            paint.drawText(QPoint(300,300),"Hi");
            paint.restore();

        }
    };

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);


    button b1;
    b1.showMaximized();
    return a.exec();
}

回答by Петър Петров

You can subclass a QLabel and completely throw away all of its mouse events (so they reach the parent). Then crate a QPushButton, set a layout on it and in this layout insert a QLabel with formatted text. You get a button that contains QLabel and is clickable. (Any Qt Widget can have layout set, including ones that you never ever expected they can.)

您可以将 QLabel 子类化并完全丢弃其所有鼠标事件(以便它们到达父级)。然后创建一个 QPushButton,在它上面设置一个布局,并在这个布局中插入一个带有格式化文本的 QLabel。您将获得一个包含 QLabel 且可点击的按钮。(任何 Qt Widget 都可以设置布局,包括您从未想过的布局。)