Qt How does QToolBar> QToolButton overflow button style?

I would like to know how in a stylish table I can find and create an "overflow button" that appears when a QToolBar is displayed with a bunch of QToolButtons, because not all buttons fit into the window.

Examples:

example 1

example 2

+3


source to share


1 answer


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:

enter image description here

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.

+3


source







All Articles