Qt How does QToolBar> QToolButton overflow button style?
This "button" is QToolBarExtension
, so you can select it in QSS using that class name.
Example:
QToolBarExtension {
background-color: black;
}
This will be the result:
Another user selectable object in QSS is its object name. So it QToolBarExtension#qt_toolbar_ext_button
will work.
Qt doesn't seem to provide an easy way to style an extension button based on its orientation. I will try to provide a workaround to help resolve your problem.
Inherit QToolBar to create a toolbar that updates the name of the extension button when the orientation changes.
mytoolbar.h
#ifndef MYTOOLBAR_H
#define MYTOOLBAR_H
#include <QToolBar>
class MyToolBar : public QToolBar
{
Q_OBJECT
public:
explicit MyToolBar(QWidget *parent = 0);
signals:
private slots:
void updateOrientation(Qt::Orientation orientation);
private:
QObject *extButton;
};
#endif // MYTOOLBAR_H
mytoolbar.cpp
#include "mytoolbar.h"
MyToolBar::MyToolBar(QWidget *parent) :
QToolBar(parent),
extButton(0)
{
// Obtain a pointer to the extension button
QObjectList l = children();
for (int i = 0; i < l.count(); i++) {
if (l.at(i)->objectName() == "qt_toolbar_ext_button") {
extButton = l.at(i);
break;
}
}
// Update extension nutton object name according to current orientation
updateOrientation(orientation());
// Connect orientationChanged signal to get the name updated every time orientation changes
connect (this, SIGNAL(orientationChanged(Qt::Orientation )),
this, SLOT(updateOrientation(Qt::Orientation)));
}
void MyToolBar::updateOrientation(Qt::Orientation orientation) {
if (extButton == 0)
return;
if (orientation == Qt::Horizontal)
extButton->setObjectName("qt_toolbar_ext_button_hor"); // Name of ext button when the toolbar is oriented horizontally.
else
extButton->setObjectName("qt_toolbar_ext_button_ver"); // Name of ext button when the toolbar is oriented vertically.
setStyleSheet(styleSheet()); // Update stylesheet
}
Now you can style the button like this:
QToolBarExtension#qt_toolbar_ext_button_hor {
background-color: black;
}
QToolBarExtension#qt_toolbar_ext_button_ver {
background-color: red;
}
where qt_toolbar_ext_button_hor
represents the button when the toolbar is oriented horizontally and qt_toolbar_ext_button_ver
when vertically.
source to share