Important: Please read the Qt Code of Conduct -

Hiding/removing toolbar from QMainWindow

  • Hello everyone,

    I am making a small app that should work as a vector graphics editor. I want to make it look like e.g. MS Paint - as a central widget of my QMainWindow I set QgraphicsScene and on the left I attached toolbar that contains different tools (they corresponds to various Qgraphicsitem which I add to the scene, which is being displayed by QgraphicsView). I also want to make different toolbars (one for each tool) that would change depending on which tool is currently chosen. I made a whole QToolbar class,(where I store the necessary info) and I can easily add it to the MainWindow (with command addToolBar). My problem is that I cannot remove it. I want to work it like this: I click icon on my left toolbar, I activate the suitable action, set it checked - toolbar appears on the screen - I can choose tool properties and draw with it - that works. Now when I want to change the tool (click different icon, set it checked and uncheck the previous one) I want the "old" toolbar to disappear and load a "new" one. Unfortunetly I can`t get rid of the old one which is still present, next to the "new" one. How can I remove it or replace it with other toolbar? I tried removeToolBar, setDisabled - none of them work the way I want. I made a picture ilustraiting my problem : I am asking for any advice that can help me solve this issue. I use PyQT 4 and Python 3.4.3 . Greetings Konrad

  • To hide toolbar you call close(), like

    to delete
    delete ui.toolBar;

  • but I would consider to add toolbar to your view or use concept of the active view ( best when controls for each view are the same) and share mainwindow toolbar.

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Looks like your implementing an equivalent of toggleViewAction

  • Thank you all for advices. I used toggleViewAction and it works great. I can show and hide toolbar whenever I click on the Icon. Unfortunetly one problem is gone, another appeared. After I click the icon and the toggleAction is checked I want to change a value of a variable that indicates which tool is currently active. To do that I need some indicator which action was triggerd. Las time it was easy, because i changed the value in the function to which the action was connected. How can I achive the same goal now with toggleaction? I tried isChecked(), isEnabled(), isVisible() to get the info which action is active but it didn`t work.

    The solution to this problem was trivial. I just connected the ToggleAction to function(triggered signal). Thanks for advices anyway.

  • Lifetime Qt Champion

    What kind of indicator do you mean ?