How can I set maximum width to which QScrollArea (within QDialog) will expand without adding scrollbar?
-
I have
QDialog
andQScrollArea
inside. When content inQScrollArea
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 bothQDialog
andQScrollArea
but nothing helps -
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 ?
-
@SGaist
It's just a simple example. In my application I have aQDialog
withQScrollArea
, that contains groups (HBoxLayouts with some ComboBoxes and PushButtons), with ability to add new such groups with buttons under theQScrollArea
. So I wantQScrollArea
that would mainly scroll vertically and expand horisontaly (width of ComboBoxes and their count are generated whenQDialog
is shown, so the scrollArea's width is variable) and showing horizontal scrolllbar only when width is really big. With my exampleQDialog
is not expanding over ~490px. Also to clarify, I can manually expandQDialog
by dragging its side so horizontal scrollbar would dissapear, but I wantedQDialog
initially shown expanded horizontaly. -
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
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 causeQScrollArea::sizeHint()
hasboundedTo(QSize(36 * h, 24 * h))
, where h isfontMetrics().height()
, at the end of implementation. So reimplementingsizeHint()
is doing the job just fine