gpt4 book ai didi

qt - 如何在QDockWidget标题栏中显示图标?

转载 作者:行者123 更新时间:2023-12-01 18:59:01 30 4
gpt4 key购买 nike

我的QDockWidget有窗口标题和关闭按钮。如何在标题栏中放置图标?

当我从资源中为 QDockWidget WindowIcon 属性选择图标时,它也不起作用。

有什么想法吗?

最佳答案

通过自定义代理样式:

class iconned_dock_style: public QProxyStyle{
Q_OBJECT
QIcon icon_;
public:
iconned_dock_style(const QIcon& icon, QStyle* style = 0)
: QProxyStyle(style)
, icon_(icon)
{}

virtual ~iconned_dock_style()
{}

virtual void drawControl(ControlElement element, const QStyleOption* option,
QPainter* painter, const QWidget* widget = 0) const
{
if(element == QStyle::CE_DockWidgetTitle)
{
//width of the icon
int width = pixelMetric(QStyle::PM_ToolBarIconSize);
//margin of title from frame
int margin = baseStyle()->pixelMetric(QStyle::PM_DockWidgetTitleMargin);

QPoint icon_point(margin + option->rect.left(), margin + option->rect.center().y() - width/2);

painter->drawPixmap(icon_point, icon_.pixmap(width, width));

const_cast<QStyleOption*>(option)->rect = option->rect.adjusted(width, 0, 0, 0);
}
baseStyle()->drawControl(element, option, painter, widget);
}
};

示例:

QDockWidget* w("my title", paretn);
w->setStyle(new iconned_dock_style( QIcon(":/icons/icons/utilities-terminal.png"), w->style()));

关于qt - 如何在QDockWidget标题栏中显示图标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2386527/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com