Resizing QToolButton and QListView without them overlapping



  • 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 :-)


  • Moderators

    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 :) )?



  • 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


  • Moderators

    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.



  • 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


  • Moderators

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



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



  • 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


  • Moderators

    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();
    @



  • 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 :-)


  • Moderators

    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.


  • Moderators

    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 :) )
    

    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.