Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How can I show QToolBar again, after having closed it?



  • Good afternoon, my question is how I can show a QToolBar again after having closed it, I happened to right click on my toolbar and it showed me a contextual menu, which I clicked on, and I close the toolbar, but then I couldn't go back to show it, some suggestion to solve this little problem.

    This is the menu that came out before closing the toolbar. The window is not from my application, what happens is, as I mentioned before the toolbar is no longer shown so I had to create a new app, to show them the option that I got. I hope you can help me, thanks in advance.

    a5473fec-87a9-430d-8b77-87243f1b8b03-image.png


  • Lifetime Qt Champion

    @lincoln said in How can I show QToolBar again, after having closed it?:

    If I already tried that and it does not work, since when I close, and reload the app again the toolbar is shown hidden, since I mentioned earlier that I am using saveState () of the QMainWindow to save the state of my application, here the code:

    Where and when did you try that ?
    If you set that in the constructor before calling readSettings, then it's going to be useless.
    The other issue that might arise is that in the constructor, the widget does not have any notion of visibility since does not yet "physically" exists. To make your life simpler in the current situation just nuke the settings file so can start from clean state. In any case, you should either add the toggle action as I suggested before or at least add an action that allows to reset the state of your widgets to a sensible default.


  • Lifetime Qt Champion

    Hi,

    You can add the QToolBar::toggleViewAction to a menu of your main window.



  • @SGaist
    I just discovered that my problem is that I have a writeSettings () function, which saves the states of my app, and another readSettings (), to load the configurations, so I am using saveState () from the QMainWindow, that is what this preventing my toolbar from showing after reopening my app; Then I rectify my question, how do I restore the toolbar, from my readSettings () function. I mean so that it is shown when loading the app.


  • Lifetime Qt Champion

    Call setVisible(true) on your toolbar.



  • @SGaist
    If I already tried that and it does not work, since when I close, and reload the app again the toolbar is shown hidden, since I mentioned earlier that I am using saveState () of the QMainWindow to save the state of my application, here the code:

    #ifndef FRMPRINCIPAL_H
    #define FRMPRINCIPAL_H
    
    #include <QMainWindow>
    #include <QPaintEngine>
    #include <QSettings>
    
    #include "qeasysettings.hpp"
    namespace Ui {
      class FrmPrincipal;
    }
    
    class FrmPrincipal : public QMainWindow
    {
      Q_OBJECT
    
    public:
      explicit FrmPrincipal(QWidget *parent = nullptr);
      ~FrmPrincipal();
      void readSettings();
      void writeSettings();
     
    private:
      Ui::FrmPrincipal *ui;
    
       // QPaintDevice interface
    public:
      virtual QPaintEngine *paintEngine() const override;
    
      // QWidget interface
    protected:
      virtual void closeEvent(QCloseEvent *event) override;
    
    };
    
    #endif // FRMPRINCIPAL_H
    
    
    #include "frmprincipal.h"
    #include "ui_frmprincipal.h"
    
    FrmPrincipal::FrmPrincipal(QWidget *parent) :
      QMainWindow(parent),  ui(new Ui::FrmPrincipal)
    {
      ui->setupUi(this);
      readSettings();
    }
    void FrmPrincipal::readSettings()
    {
      QEasySettings::init(QEasySettings::Format::regFormat,qApp->organizationName());
      restoreGeometry(QEasySettings::readSettings("MainWindow","geometry").toByteArray());
      restoreState(QEasySettings::readSettings("MainWindow","windowState").toByteArray());
      ui->splitter->restoreState(QEasySettings::readSettings("MainWindow","splitterState").toByteArray());
    
     }
    
    void FrmPrincipal::writeSettings()
    {
      QEasySettings::writeSettings("MainWindow","geometry",saveGeometry());
      QEasySettings::writeSettings("MainWindow","windowState",saveState());
      QEasySettings::writeSettings("MainWindow","splitterState",ui->splitter->saveState());
    
     }
    QPaintEngine *FrmPrincipal::paintEngine() const
    {
      return QWidget::paintEngine();
    }
    
    void FrmPrincipal::closeEvent(QCloseEvent *event)
    {
      writeSettings();
      QMainWindow::closeEvent(event);
    }
    

    I am using QEasySettings to save my app settings.:
    https://github.com/mguludag/QEasySettings


  • Lifetime Qt Champion

    @lincoln said in How can I show QToolBar again, after having closed it?:

    If I already tried that and it does not work, since when I close, and reload the app again the toolbar is shown hidden, since I mentioned earlier that I am using saveState () of the QMainWindow to save the state of my application, here the code:

    Where and when did you try that ?
    If you set that in the constructor before calling readSettings, then it's going to be useless.
    The other issue that might arise is that in the constructor, the widget does not have any notion of visibility since does not yet "physically" exists. To make your life simpler in the current situation just nuke the settings file so can start from clean state. In any case, you should either add the toggle action as I suggested before or at least add an action that allows to reset the state of your widgets to a sensible default.



  • @SGaist
    Thanks for your answer, add an action like suggest, in a context menu, and with that solve my problem.


Log in to reply