2

I have a QDialog with a maximize button and a small test runner.

main.cpp

#include <QApplication>
#include <QPushButton>
#include "MyDialog.h"

    int main(int argc, char** args) {
        QApplication app(argc, args);
        auto widget=new MyDialog;
        auto btn = new QPushButton("Show");
        btn->show();
        QObject::connect(btn, &QPushButton::clicked, [&]() {widget->exec(); });
        app.exec();
    }

MyDialog.h

#pragma once

#include <QDialog>
#include <QMainWindow>
#include <QPushButton>
#include <QVBoxLayout>
#include <QLabel>
#include <QFrame>

class MyDialog : public QDialog {
    Q_OBJECT
public:
    MyDialog(QWidget* parent = nullptr) : QDialog(parent) {     
        auto window = new QMainWindow;
        auto frame = new QFrame;
        frame->setLayout(new QHBoxLayout);
        frame->layout()->addWidget(new QLabel("Test"));
        window->setCentralWidget(frame);
        setLayout(new QVBoxLayout);
        layout()->setMargin(0);
        layout()->addWidget(window);
        setWindowFlags(windowFlags() | Qt::CustomizeWindowHint | Qt::WindowMinimizeButtonHint | Qt::WindowMaximizeButtonHint | Qt::WindowCloseButtonHint);
    }
};

In my test runner I press [Show], then I maximize my dialog. Now I close the dialog by pressing the cross. Then I press [Show] again. After that I got the wrong rendering of my dialog.

How can I fix this behavior. The QLabel("Test")should occupy the whole screen.

Wrong rendering of my dialog

Aleph0
  • 5,816
  • 4
  • 29
  • 80

0 Answers0