I want to write dialog with "spoiler". When I show/hide some elements, the form must resize to match her content. But I noticed that minimumSizeHint works with some delay. For example I wrote following dialog code:
dialog.h
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
class Dialog : public QDialog {
Q_OBJECT
public:
explicit Dialog(QWidget *parent = 0);
private:
QPushButton *button1, *button2, *button3;
void adjust();
};
#endif // DIALOG_H
dialog.cpp
#include <QVBoxLayout>
#include <QPushButton>
#include <QTimer>
#include "dialog.h"
Dialog::Dialog(QWidget *parent) :
QDialog(parent) {
QVBoxLayout *layout = new QVBoxLayout;
button1 = new QPushButton("bad method", this);
button2 = new QPushButton("working method", this);
button3 = new QPushButton("indent", this);
layout->addWidget(button1);
layout->addWidget(button2);
layout->addWidget(button3);
setLayout(layout);
connect(button1, &QPushButton::pressed, [=]() {
button3->setVisible(!button3->isVisible());
adjust();
});
connect(button2, &QPushButton::pressed, [=]() {
button3->setVisible(!button3->isVisible());
QTimer *timer = new QTimer(this);
connect(timer, &QTimer::timeout, [=](){ adjust(); });
timer->setSingleShot(true);
timer->start(0);
});
}
void Dialog::adjust() {
resize(width(), minimumSizeHint().height());
}
When pressed button1, the form isn't match her content tightly, but when pressed button2, the form tightly match her content. How to achieve results as pressing button2 without timers and other workarounds? Why button1 isn't work?
Aucun commentaire:
Enregistrer un commentaire