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 can I set maximum width to which QScrollArea (within QDialog) will expand without adding scrollbar?

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

Scheduled Pinned Locked Moved Solved General and Desktop
qscrollbarqdialog
5 Posts 2 Posters 2.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.
  • K Offline
    K Offline
    Kot Shrodingera
    wrote on last edited by
    #1

    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 {
      Q_OBJECT
    public:
      Dialog(QWidget *parent = nullptr) : QDialog(parent) {
        auto dialogLayout = new QVBoxLayout(this);
    
        auto scrollArea = new QScrollArea(this);
        scrollArea->setWidgetResizable(true);
        auto scrollWidget = new QWidget(scrollArea);
        auto scrollLayout = new QVBoxLayout(scrollWidget);
        scrollLayout->setAlignment(Qt::AlignTop);
        scrollArea->setWidget(scrollWidget);
    
        dialogLayout->addWidget(scrollArea);
    
        auto button = new QPushButton("Button", this);
        button->setFixedSize(500, 30);
    
        scrollLayout->addWidget(button);
      }
    };
    
    class MainWindow : public QMainWindow
    {
      Q_OBJECT
    
    public:
      MainWindow(QWidget *parent = nullptr) : QMainWindow(parent)
      {
        auto centralWidget = new QWidget(this);
        setCentralWidget(centralWidget);
        auto mainLayout = new QVBoxLayout(centralWidget);
    
        auto button = new QPushButton("Dialog", this);
        mainLayout->addWidget(button);
    
        connect(button, &QPushButton::clicked, this, []() {Dialog().exec();});
      }
    };
    

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

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      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 ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      K 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        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 ?

        K Offline
        K Offline
        Kot Shrodingera
        wrote on last edited by
        #3

        @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.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • K Offline
            K Offline
            Kot Shrodingera
            wrote on last edited by
            #5

            Thanks but I asked same question on StackOverflow
            https://stackoverflow.com/questions/55471486/how-can-i-set-maximum-width-to-which-qscrollarea-within-qdialog-will-expand-wi/
            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

            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