tags:

views:

398

answers:

3

My QDockWidget has window title and close button. How do I put icon in title bar?

When I select icon from my recources for QDockWidget WindowIcon property, it's not working either.

Any ideas?

A: 

Hey, I don't think it's possible to display it without overriding the QDockWidget's class...

When you're using the WindowIcon property, it's used to display the icon when you perform a right click on the main title bar of the application... It lets you choose if you want to display the QDockWidget or not...

You can see an example with that screenshot...

Example

Andy M
A: 

I think you can use QDockWidget::setTitleBarWidget(QWidget *widget).

Mathias
A: 

Through custom proxy-style:

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 );
        //spacing between icon and title
        int spacing = baseStyle()->pixelMetric( QStyle::PM_LayoutHorizontalSpacing );

        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);
}

};

example:

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