Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. Qt 6
  4. QHBoxLayout problem: QSlider keeps increasing the width when window's width increases
Forum Updated to NodeBB v4.3 + New Features

QHBoxLayout problem: QSlider keeps increasing the width when window's width increases

Scheduled Pinned Locked Moved Solved Qt 6
9 Posts 4 Posters 929 Views 2 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.
  • PbaodogeP Offline
    PbaodogeP Offline
    Pbaodoge
    wrote on last edited by
    #1

    I have a problem while dealing with QHBoxLayout in a widget. When I tried to add items to QHBoxLayout, everything seems normal. Except for the slider. I wanted to make the icon lie near the slider while keeping the slider a specific width(I just wanted to make the slider small).
    Here's the code:

    layout2->addWidget(BackwardButton);
        layout2->addSpacing(60);
        layout2->addWidget(button1, 0, Qt::AlignCenter);
        layout2->addSpacing(60);
        layout2->addWidget(button2, 0, Qt::AlignCenter);
        layout2->addSpacing(60);
        layout2->addWidget(ForwardButton);
        layout2->addSpacing(100);
        layout2->addWidget(icon);
        layout2->addWidget(slider);
    

    2da5cdab-dd4d-495e-abe5-19cb8d3bda56-image.png
    Here's when the problem started. Yes, the icon lies near the slider perfectly. However, the slider's width keeps getting bigger when the window's width increases. Thats why I tried with this code to set a specific width for slider:

       slider->setMaximumWidth(300);
    

    60dca9d6-8b3b-4a82-b926-1e7d92e4005b-image.png
    It does keep a specific width. However, that makes the icon lay far away from the slider as it used to be. I have no idea how to fix this. I'm just a newbie...
    I will be very appreciative if anyone here can help me. Thanks a lot!

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by VRonin
      #2

      @Pbaodoge said in QHBoxLayout problem: QSlider keeps increasing the width when window's width increases:

      layout2->addWidget(slider);

          layout2->addWidget(BackwardButton);
          layout2->addSpacing(60);
          layout2->addWidget(button1, 0, Qt::AlignCenter);
          layout2->addSpacing(60);
          layout2->addWidget(button2, 0, Qt::AlignCenter);
          layout2->addSpacing(60);
          layout2->addWidget(ForwardButton);
          layout2->addSpacing(100);
          layout2->addWidget(icon);
      slider->setSizePolicy(QSizePolicy::Maximum,QSizePolicy::Maximum); //Tell the slider not to expand beyond its sizeHint
          layout2->addWidget(slider);
      layout2->addSpacing(60); // (optional) add some blank space that will expand instead of the slider
      

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      2
      • PbaodogeP Offline
        PbaodogeP Offline
        Pbaodoge
        wrote on last edited by
        #3

        398648dd-ac8f-4c86-b17d-8119e20ceb5e-image.png
        addSpacing at the end works, but setSizePolicy has a little bit of problems: No member named "Maximum" in namespace Qt
        maybe Qt::Maximum is wrong or something? 🤔

        1 Reply Last reply
        0
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #4

          Ooops, my bad, should be QSizePolicy::Maximum. Corrected above

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          JonBJ 1 Reply Last reply
          0
          • VRoninV VRonin

            Ooops, my bad, should be QSizePolicy::Maximum. Corrected above

            JonBJ Online
            JonBJ Online
            JonB
            wrote on last edited by
            #5

            @VRonin
            I know nothing to little about layouting, I fiddle in Designer or code till it works! I totally accept what you have proposed. But my first "logical" thought when reading this is I would start by creating an extra QHBoxLayout to group the icon and its slider (or even a widget holding that). They are a pair of widgets which have to go together. Does that seem sensible?

            VRoninV 1 Reply Last reply
            0
            • JonBJ JonB

              @VRonin
              I know nothing to little about layouting, I fiddle in Designer or code till it works! I totally accept what you have proposed. But my first "logical" thought when reading this is I would start by creating an extra QHBoxLayout to group the icon and its slider (or even a widget holding that). They are a pair of widgets which have to go together. Does that seem sensible?

              VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by VRonin
              #6

              @JonB said in QHBoxLayout problem: QSlider keeps increasing the width when window's width increases:

              Does that seem sensible?

              It does but does not prevent the slider expanding and while it works, it's unecessary here

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              1 Reply Last reply
              1
              • PbaodogeP Offline
                PbaodogeP Offline
                Pbaodoge
                wrote on last edited by
                #7

                Thank you so much, @VRonin @JonB!

                1 Reply Last reply
                0
                • PbaodogeP Offline
                  PbaodogeP Offline
                  Pbaodoge
                  wrote on last edited by Pbaodoge
                  #8

                  @VRonin, @JonB
                  Wait, actually when I update the code, the slider and the icon lines perfectly. However, when it is on full screen(it means that the window's width increases, the problem keeps repeating.
                  Here's the small window size:
                  c6047f1e-a3f4-4820-86db-604e411e0ad0-image.png
                  And here's what happens when it is on full-screen size:
                  a34a800f-8e9e-4a89-be86-244eab828f0b-image.png
                  That's weird.

                  M 1 Reply Last reply
                  0
                  • PbaodogeP Pbaodoge

                    @VRonin, @JonB
                    Wait, actually when I update the code, the slider and the icon lines perfectly. However, when it is on full screen(it means that the window's width increases, the problem keeps repeating.
                    Here's the small window size:
                    c6047f1e-a3f4-4820-86db-604e411e0ad0-image.png
                    And here's what happens when it is on full-screen size:
                    a34a800f-8e9e-4a89-be86-244eab828f0b-image.png
                    That's weird.

                    M Offline
                    M Offline
                    mpergand
                    wrote on last edited by
                    #9

                    @Pbaodoge
                    layout2->addSpacing(100);
                    layout2->addWidget(icon);

                    instead of addSpacing use:
                    addStretch();

                    1 Reply Last reply
                    2

                    • Login

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