【发布时间】: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();
}
我做错了什么?如何在我的自定义按钮上添加一个图标而不需要编辑股票按钮?
【问题讨论】: