Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to dynamically hide/show widgets in a custom QWidget-based toolbar on window resize?
Forum Updated to NodeBB v4.3 + New Features

How to dynamically hide/show widgets in a custom QWidget-based toolbar on window resize?

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 167 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    harsh_ag
    wrote on last edited by
    #1

    Hi everyone,

    I'm building a custom toolbar using a plain QWidget instead of QToolBar, and I need to handle widget visibility based on the window size dynamically.

    What I want to achieve:

    • When the window is wide enough, all child widgets (buttons, labels, etc.) in the custom toolbar are visible.
    • When the window becomes narrow, certain widgets should automatically hide, like a responsive or adaptive toolbar.
    • This is similar to how toolbars work in modern apps — they adapt their content visibility depending on the available width.

    Current setup:

    • My custom toolbar is a QWidget added to a layout in QMainWindow.
    • I'm adding buttons and other widgets manually to it using a horizontal layout (QHBoxLayout).
    • I tried listening to the parent’s resize event, but managing space and knowing when to hide/show specific widgets is tricky.

    Questions:

    • What's the recommended way to measure the available width inside the toolbar and compare it with the total width of child widgets?
    • Should I override resizeEvent() in the custom toolbar and manually calculate visibility per widget?
    • Are there any best practices or existing layouts (or custom ones) that help implement this kind of adaptive behavior?

    Any tips, examples, or direction would be really appreciated!

    Thanks in advance!

    Pl45m4P 1 Reply Last reply
    0
    • H harsh_ag

      Hi everyone,

      I'm building a custom toolbar using a plain QWidget instead of QToolBar, and I need to handle widget visibility based on the window size dynamically.

      What I want to achieve:

      • When the window is wide enough, all child widgets (buttons, labels, etc.) in the custom toolbar are visible.
      • When the window becomes narrow, certain widgets should automatically hide, like a responsive or adaptive toolbar.
      • This is similar to how toolbars work in modern apps — they adapt their content visibility depending on the available width.

      Current setup:

      • My custom toolbar is a QWidget added to a layout in QMainWindow.
      • I'm adding buttons and other widgets manually to it using a horizontal layout (QHBoxLayout).
      • I tried listening to the parent’s resize event, but managing space and knowing when to hide/show specific widgets is tricky.

      Questions:

      • What's the recommended way to measure the available width inside the toolbar and compare it with the total width of child widgets?
      • Should I override resizeEvent() in the custom toolbar and manually calculate visibility per widget?
      • Are there any best practices or existing layouts (or custom ones) that help implement this kind of adaptive behavior?

      Any tips, examples, or direction would be really appreciated!

      Thanks in advance!

      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by Pl45m4
      #2

      Hi and welcome to the forum,

      @harsh_ag said in How to dynamically hide/show widgets in a custom QWidget-based toolbar on window resize?:

      What's the recommended way to measure the available width inside the toolbar and compare it with the total width of child widgets?

      There is no other way than to do it manually. If you want some custom behavior you need to calculate and sum the width of your involved widgets and compare it with the available geometry / size of your widget.

      Should I override resizeEvent() in the custom toolbar and manually calculate visibility per widget?

      Probably. Unless you find something existing that already implements your custom behavior.

      Are there any best practices or existing layouts (or custom ones) that help implement this kind of adaptive behavior?

      Maybe look at how QWidgetAction works in a QToolBar.
      (If a regular toolbar is to small to display all toolbuttons/widgets, it adds this extra popup menu via "extension button".
      There was this topic where I (and OP) tried to figure out how this works exactly, but unfortunately we couldn't manage to achieve what OP was trying to do.

      Also maybe FlowLayout is something worth to look at.
      (custom implementation of a layout, see: FlowLayout Example)
      Instead of the new line "jumps", you could add your own implementation of what should happen when your toolbar is getting smaller than the space your items/widgets actually need.


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      1 Reply Last reply
      0
      • K Offline
        K Offline
        Kevin Hoang
        wrote on last edited by
        #3

        You should implement a method to show or hide the toolbar based on the window size. From MainWindow::resizeEvent(), you can either calculate when to show or hide the toolbar yourself, or simply pass event->size() to your custom toolbar and let it decide whether it should be visible or not.

        Pl45m4P 1 Reply Last reply
        0
        • K Kevin Hoang

          You should implement a method to show or hide the toolbar based on the window size. From MainWindow::resizeEvent(), you can either calculate when to show or hide the toolbar yourself, or simply pass event->size() to your custom toolbar and let it decide whether it should be visible or not.

          Pl45m4P Offline
          Pl45m4P Offline
          Pl45m4
          wrote on last edited by
          #4

          @Kevin-Hoang said in How to dynamically hide/show widgets in a custom QWidget-based toolbar on window resize?:

          calculate when to show or hide the toolbar yourself

          OP asked how caculate the visibility of toolbar items, not the toolbar itself. The toolbar should always be visible, if I understood correctly.


          If debugging is the process of removing software bugs, then programming must be the process of putting them in.

          ~E. W. Dijkstra

          K 1 Reply Last reply
          0
          • Pl45m4P Pl45m4

            @Kevin-Hoang said in How to dynamically hide/show widgets in a custom QWidget-based toolbar on window resize?:

            calculate when to show or hide the toolbar yourself

            OP asked how caculate the visibility of toolbar items, not the toolbar itself. The toolbar should always be visible, if I understood correctly.

            K Offline
            K Offline
            Kevin Hoang
            wrote on last edited by Kevin Hoang
            #5

            @Pl45m4 said in How to dynamically hide/show widgets in a custom QWidget-based toolbar on window resize?:

            OP asked how caculate the visibility of toolbar items, not the toolbar itself. The toolbar should always be visible, if I understood correctly.

            I misunderstood earlier — I thought he was showing or hiding the whole toolbar.

            If the toolbar is a toplevel widget or its parent is the MainWindow, then yes, it still makes sense to handle it the way I mentioned. From MainWindow::resizeEvent(), he should pass the size information to the toolbar so it can decide which items to show or hide based on the available space.

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved