How can I set maximum width to which QScrollArea (within QDialog) will expand without adding scrollbar?

  • I have QDialog and QScrollArea inside. When content in QScrollArea is small, dialog window is also small. When content width is increasing, dialog window's width is also encreasing but only to some fixed value.

    When QPushButton's width in my example is about 450 or more, vertical scrollbar appears. How can I avoid this and let dialog window expand more?

    class Dialog : public QDialog {
      Dialog(QWidget *parent = nullptr) : QDialog(parent) {
        auto dialogLayout = new QVBoxLayout(this);
        auto scrollArea = new QScrollArea(this);
        auto scrollWidget = new QWidget(scrollArea);
        auto scrollLayout = new QVBoxLayout(scrollWidget);
        auto button = new QPushButton("Button", this);
        button->setFixedSize(500, 30);
    class MainWindow : public QMainWindow
      MainWindow(QWidget *parent = nullptr) : QMainWindow(parent)
        auto centralWidget = new QWidget(this);
        auto mainLayout = new QVBoxLayout(centralWidget);
        auto button = new QPushButton("Dialog", this);
        connect(button, &QPushButton::clicked, this, []() {Dialog().exec();});

    I tried QDialog::setMaximumWidth, and setting Expanding size policy for both QDialog and QScrollArea but nothing helps

  • Lifetime Qt Champion


    Might be a silly question but why use a QScrollArea at all if you only have one button inside it and you finally don't want the scrollbar of the QScrollArea to show ?

  • @SGaist
    It's just a simple example. In my application I have a QDialog with QScrollArea, that contains groups (HBoxLayouts with some ComboBoxes and PushButtons), with ability to add new such groups with buttons under the QScrollArea. So I want QScrollArea that would mainly scroll vertically and expand horisontaly (width of ComboBoxes and their count are generated when QDialog is shown, so the scrollArea's width is variable) and showing horizontal scrolllbar only when width is really big. With my example QDialog is not expanding over ~490px. Also to clarify, I can manually expand QDialog by dragging its side so horizontal scrollbar would dissapear, but I wanted QDialog initially shown expanded horizontaly.

  • Lifetime Qt Champion

    If you want to resize your QDialog on startup, use a singleshot QTimer and check the size hint of the QScrollArea. With that you should be able to resize the dialog to your liking.

  • Thanks but I asked same question on StackOverflow
    The reason of such behaviour is cause QScrollArea::sizeHint() has boundedTo(QSize(36 * h, 24 * h)) , where h is fontMetrics().height(), at the end of implementation. So reimplementing sizeHint() is doing the job just fine

Log in to reply

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