[SOLVED] Submenu from QToolBar button
-
Hi,
i have QToolBar like this:
!http://www.stud.fit.vutbr.cz/~xadami06/bar.png(QToolBar)!
and i need submenu from QToolBar button, for example:
If I click on save button, the save button submenu will show
!http://www.stud.fit.vutbr.cz/~xadami06/bar2.png(QToolBar submenu)!Please, how can I do this effect?
Thanks for reply! -
you can work with states.
see: "qt-doc":http://qt-project.org/doc/qt-5.0/qtquick/qmlexampletoggleswitch.html
-
Get a reference to the "QToolButton":http://qt-project.org/doc/qt-4.8/qtoolbutton.html for the action, set the "popupMode":http://qt-project.org/doc/qt-4.8/qtoolbutton.html#popupMode-prop and add actions to it thus:
@
#include <QtGui>class MainWindow : public QMainWindow
{
public:
MainWindow(QWidget *parent=0)
: QMainWindow(parent)
{
toolBar=addToolBar("Main Tool Bar");
toolBar->addAction(new QAction("&New", this));
toolBar->addAction(new QAction("&Open", this));QAction *saveAction=new QAction("&Save", this); toolBar->addAction(saveAction); QToolButton *saveButton= dynamic_cast<QToolButton*>(toolBar->widgetForAction(saveAction)); saveButton->setPopupMode(QToolButton::InstantPopup); saveButton->addAction(new QAction("Save &As", this)); saveButton->addAction(new QAction("Save All", this)); }
private:
QToolBar *toolBar;
};int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
@Hope this helps ;o)
-
Thanks jazzycamel, this is very helpful.
But i have a little problem with it. Because my example was wrong :)How do I reach effect, that the button in menu has not been seen in the submenu? Example of the save buttons is stupid, but it doesnt matter. The point is, that the button in menu is NOT button, its only label for buttons in the submenu.
For better understanding look at this picture:
!http://www.stud.fit.vutbr.cz/~xadami06/bar4.png(bar)!Thank you for your advice.
-
If I understand you correctly, you want the QToolBar to act more like a menu where the only result of pressing the 'Save' button/label is to open a sub menu with the 'Save As' and 'Save All' actions in it? If that's the case, then the following slightly modified example might be the solution:
#include <QtGui> class MainWindow : public QMainWindow { public: MainWindow(QWidget *parent=0) : QMainWindow(parent) { toolBar=addToolBar("Main Tool Bar"); toolBar->addAction(new QAction("&New", this)); toolBar->addAction(new QAction("&Open", this)); QToolButton *saveButton=new QToolButton(this); saveButton->setText("Save"); saveButton->setPopupMode(QToolButton::InstantPopup); QMenu *saveMenu=new QMenu(saveButton); saveMenu->addAction(new QAction("Save &As", this)); saveMenu->addAction(new QAction("Save All", this)); saveButton->setMenu(saveMenu); toolBar->addWidget(saveButton); } private: QToolBar *toolBar; }; int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); }
I hope I've understood you correctly and this is the answer you were looking for, if not post back ;o)
-
Yes, you undestood me correctly. It works great!
Thank you very much and have a nice day.