Important: Please read the Qt Code of Conduct -

How to hide a QPushButton in a QToolBar?

  • Hello,

    I am trying to hide a QPushButton but it does not work.
    Here is the part of my code with the very basic for this purpose, pressing button1 should hide button2...


    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    #include <QMainWindow>
    class QToolBar;
    class QPushButton;
    class MainWindow : public QMainWindow
        MainWindow(QWidget *parent = 0);
        QToolBar*       m_toolBar;
        QPushButton*    m_button1;
        QPushButton*    m_button2;
    private slots:
        void            Button1Slot();
    #endif // MAINWINDOW_H


    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        m_toolBar = addToolBar(QString("test"));
        m_button1 = new QPushButton(QString("Button 1"), m_toolBar);
        //m_button1->show(); //Does not change anything
        QObject::connect(m_button1, SIGNAL(clicked(bool)), this, SLOT(Button1Slot()));
        m_button2 = new QPushButton(QString("Button 2"), m_toolBar);
        //m_button2->show(); //Does not change anything
    void MainWindow::Button1Slot()
        m_button2->hide();              // Does not work
        m_button2->setHidden(true);     // Does not work
        m_button2->setVisible(false);   // Does not work


    #include "mainwindow.h"
    #include <QApplication>
    int main(int argc, char *argv[])
        QApplication a(argc, argv);
        MainWindow w;;
        return a.exec();

    Thanks for your help.

  • @Calivernon

    Hi doc says
    You should use QAction::setVisible() to change the visibility of the widget. Using QWidget::setVisible(), QWidget::show() and QWidget::hide() does not work.

    Example here.

  • Thank you Ratzz,

    that is perfect!

Log in to reply