Skip to content

Instantly share code, notes, and snippets.

@Mecanik
Forked from mistic100/qtoolbarext.hpp
Created July 16, 2022 05:28
Show Gist options
  • Select an option

  • Save Mecanik/87a6bf907279634bb81ed3a8e71aaab5 to your computer and use it in GitHub Desktop.

Select an option

Save Mecanik/87a6bf907279634bb81ed3a8e71aaab5 to your computer and use it in GitHub Desktop.

Revisions

  1. @mistic100 mistic100 renamed this gist Jan 3, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. @mistic100 mistic100 created this gist Jan 3, 2015.
    69 changes: 69 additions & 0 deletions qtoolbarext.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,69 @@
    #ifndef QTOOLBAREXT_H
    #define QTOOLBAREXT_H

    #include <QToolBar>
    #include <QMenu>
    #include <QToolButton>


    /**
    * @brief Custom QToolBar allowing to add a button/menu with text and icon
    */
    class QToolBarExt : public QToolBar
    {
    Q_OBJECT

    public:
    QToolBarExt(QWidget* _parent) : QToolBar(_parent) {}

    /**
    * @brief Add a button with text only
    */
    QToolButton* addButton(const QString &_text)
    {
    QToolButton* button = new QToolButton(this);
    QAction* action = addWidget(button);
    button->setText(_text);
    action->setText(_text);
    button->setDefaultAction(action);
    return button;
    }

    /**
    * @brief Add a button with text and icon
    */
    QToolButton* addButton(const QIcon &_icon, const QString &_text,
    Qt::ToolButtonStyle _style = Qt::ToolButtonTextBesideIcon)
    {
    QToolButton* button = addButton(_text);
    button->setIcon(_icon);
    button->setToolButtonStyle(_style);
    return button;
    }

    /**
    * @brief Add a menu with text only
    */
    QToolButton* addMenu(const QString &_text, QMenu* _menu,
    QToolButton::ToolButtonPopupMode _mode = QToolButton::InstantPopup)
    {
    QToolButton* button = addButton(_text);
    button->setMenu(_menu);
    button->setPopupMode(_mode);
    return button;
    }

    /**
    * @brief Add a menu with text and icon
    */
    QToolButton* addMenu(const QIcon &_icon, const QString &_text, QMenu* _menu,
    Qt::ToolButtonStyle _style = Qt::ToolButtonTextBesideIcon, QToolButton::ToolButtonPopupMode _mode = QToolButton::InstantPopup)
    {
    QToolButton* button = addButton(_icon, _text, _style);
    button->setMenu(_menu);
    button->setPopupMode(_mode);
    return button;
    }
    };

    #endif // QTOOLBAREXT_H