Last active
July 16, 2022 05:28
-
-
Save mistic100/d41af2013b33087189df to your computer and use it in GitHub Desktop.
[Qt/C++] Custom QToolBar allowing to add a button/menu with text and icon
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment