【问题标题】:How to set an icon to a new button in QDialogButtonBox?如何将图标设置为 QDialogBu​​ttonBox 中的新按钮?
【发布时间】:2018-02-28 11:53:27
【问题描述】:

我正在尝试在 Kubuntu 14.04 上的 Qt5 程序 (Qt 5.5.1) 中创建一个带有 QDialogButtonBox 图标的自定义按钮。阅读this answer,我知道不建议简单地编辑股票按钮,因此我将QDialogButtonBox::addButton 与我的自定义按钮一起使用。但是由于某种原因,虽然我确实成功更改了股票按钮的图标,但我的自定义按钮在使用QPushButton::setIcon 后仍然没有任何图标。

这是代码

#include <QApplication>
#include <QDialogButtonBox>
#include <QPushButton>

int main(int argc, char** argv)
{
    QApplication app(argc,argv);

    QDialogButtonBox bbox(QDialogButtonBox::Close);

    const auto button=new QPushButton(QObject::tr("Convert..."));
    bbox.addButton(QObject::tr("Convert..."), QDialogButtonBox::ApplyRole);
    const auto icon=QIcon::fromTheme("system-run");
    // This doesn't work - the button remains without icon
    button->setIcon(icon);
    // But this does
    bbox.button(QDialogButtonBox::Close)->setIcon(icon);

    bbox.show();

    return app.exec();
}

我做错了什么?如何在我的自定义按钮上添加一个图标而不需要编辑股票按钮?

【问题讨论】:

    标签: c++ qt button icons


    【解决方案1】:

    您正在创建两个按钮。一个您不会在任何地方显示的按钮,以及您在bbox 中添加的一个按钮。而且您正在为未在任何地方显示的按钮设置图标。

    修复您的代码,这样您就不会创建两个单独的按钮,并为您在bbox 中放置的按钮设置一个图标:

    QPushButton *button = bbox.addButton(QObject::tr("Convert..."), QDialogButtonBox::ApplyRole);
    ...
    button->setIcon(icon);
    

    【讨论】:

      猜你喜欢
      • 2015-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多