Nominate our 2022 Qt Champions!

How to get access to QToolButton?

  • Hi! For example I have a window with tool bar. I have created this windows in Qt Designer. Then I programmaticaly add actions to the tool bar, using ui->toolBar->addAction() function. Calling this function add toolbutton to the toolBar. How can I get access to that toolbutton object? For example I want to change some of toolbutton properties (make it autorise, enable pupupMode, etc.)

  • @Harb
    you can try this

  • @Harb This depends a bit on which overload of addAction you use. When you use the QAction* addAction(const QString&) overload, as the function signature tells you, you get back the new QAction object, which you may use to access the specifics.

    However, when using addAction(), you don't add a QToolButton AFAIK. If you specifically want to have a QToolButton on your toolbar, you have to create one yourself, then add it to the toolbar using QAction* addWidget(QWidget*), in other words, something along the lines of this:

    QToolButton* tool = new QToolButton;

  • @Jakob
    from documentation:
    Tool buttons are normally created when new QAction instances are created with QToolBar::addAction() or existing actions are added to a toolbar with QToolBar::addAction(). It is also possible to construct tool buttons in the same way as any other widget, and arrange them alongside other widgets in layouts.

    Apparently addAction() create QToolButton object. The question is about getting pointer to this tool button.

  • You can use something like:

    mytoolbutton = mytoolbar->widgetForAction (myaction);

Log in to reply