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. Resizing QToolButton and QListView without them overlapping
QtWS25 Last Chance

Resizing QToolButton and QListView without them overlapping

Scheduled Pinned Locked Moved General and Desktop
12 Posts 2 Posters 4.4k Views
  • 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.
  • D Offline
    D Offline
    Dolphin
    wrote on last edited by
    #1

    I have a button and a listview in a vertical layout and need to increase their sizes but not allow them to overlap. I have tried different layout types, played about with setColumnMinimumWidth/setColumnMaximumWidth/setColumnStretch/setSpacing. Is there a secret to getting the layout to layout and never overlap? These items need to get quite big - not a problem if things go off the screen just a problem if they overlap.

    Thanks :-)

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      Layouts usually work correctly, without overlapping. Are you modifying size hints, perhaps? That might fool layouting system.

      Or maybe I misunderstand. Can you provide a screenshot of what is actually happening (if you suspect some part of the code to be the culprit, paste it here, too :) )?

      (Z(:^

      1 Reply Last reply
      0
      • D Offline
        D Offline
        Dolphin
        wrote on last edited by
        #3

        That is what I thought which is why I was using a layout, to save me calculations!

        Code is below (from a test app so I can prove what is happening and that I am not messing something up in my main app). After pressing the button both widgets resize and half the button is hidden behind the listView - same thing happens in my main app except the resize calculation is more complex. (Tried different layouts too);
        @
        MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
        {
        ui->setupUi(this);

        QGroupBox *screen = new QGroupBox();
        QGridLayout *layout = new QGridLayout();
        
        layout->addWidget(ui->pushButton, 0, 0);
        layout->addWidget(ui->listView, 0, 1);
        screen->setLayout(layout);
        screen->show();
        

        }

        MainWindow::~MainWindow()
        {
        delete ui;
        }

        void MainWindow::on_pushButton_clicked()
        {
        ui->pushButton->resize(ui->pushButton->size()*2);
        ui->listView->resize(ui->listView->size()*2);
        }
        @

        edit: please use @ tags around your code sections; Andre

        1 Reply Last reply
        0
        • sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          Hm, that's quite unconventional, I usually set UIs up in a different way. But, that - probably - does not matter here.

          What you probably need is to instruct the UI to refit itself. Try inserting this after you resize your widgets (so, as the last line in your script):
          @
          screen->resize(screen->sizeHint());
          @

          Not sure if this will work though, layouts are designed to produce a compact view.

          (Z(:^

          1 Reply Last reply
          0
          • D Offline
            D Offline
            Dolphin
            wrote on last edited by
            #5

            No joy.

            This was just the latest layout attempt, I have tried all sorts of ways of laying things out from different examples and not found anything that works :-S

            1 Reply Last reply
            0
            • sierdzioS Offline
              sierdzioS Offline
              sierdzio
              Moderators
              wrote on last edited by
              #6

              Have you tried using ::setGeometry() instead of ::resize()?

              (Z(:^

              1 Reply Last reply
              0
              • D Offline
                D Offline
                Dolphin
                wrote on last edited by
                #7

                Yes and setColumnStretch(), and SetHorizontalSpacing(increase the spacing value as items get bigger - items still overlap) and layout->update() and setColumnMinimumWidth().

                1 Reply Last reply
                0
                • D Offline
                  D Offline
                  Dolphin
                  wrote on last edited by
                  #8

                  Given that this is a bust - got any ideas for an alternative direction? Not a lot on the screen so could just calculate it all myself :-S

                  1 Reply Last reply
                  0
                  • sierdzioS Offline
                    sierdzioS Offline
                    sierdzio
                    Moderators
                    wrote on last edited by
                    #9

                    I would personally set everything up in the UI file itself. Try creating an example QMainWindow app in QtCreator to get some good template code for that.

                    As for this, I have few more ideas, if you don't want to refactor too much. Try setting the central widget for QMainWindow. In your constructor, remove line 11 (screen->show()), and instead, try using this:
                    @
                    this->setCentralWidget(screen); // Syntax might be a bit different, but you should be able to make it work after some tweaking.
                    this->show();
                    @

                    (Z(:^

                    1 Reply Last reply
                    0
                    • D Offline
                      D Offline
                      Dolphin
                      wrote on last edited by
                      #10

                      Thank you very much for your help sweetie - if I do everything in the QtDesigner I get pretty much what I want but I was looking for a way to have layout options for the menu. Need to have something working so will go for that just now and come back to your idea after I have nailed down exactly what layouts they want in case I am driving myself crazy for nothing :-)

                      1 Reply Last reply
                      0
                      • sierdzioS Offline
                        sierdzioS Offline
                        sierdzio
                        Moderators
                        wrote on last edited by
                        #11

                        There is more. I have managed to get it to work on my machine. Will send you a zip soon, and post code here. It requires a bit of a hack, but works.

                        (Z(:^

                        1 Reply Last reply
                        0
                        • sierdzioS Offline
                          sierdzioS Offline
                          sierdzio
                          Moderators
                          wrote on last edited by
                          #12

                          Based on my code, just to show what is needed to make it work:
                          @
                          QRect temp = ui->verticalLayout->geometry(); // That is our main layout
                          temp.setWidth(temp.width() * 2); // We make it bigger
                          temp.setHeight(temp.height() * 2);

                          ui->verticalLayout->setGeometry(temp); // Reset layout's size
                          this->resize(ui->verticalLayout->geometry().size()); // update MainWindow ("this" in this case points to a MainWindow :) )
                          

                          @

                          (Z(:^

                          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