当将 QDialog 移动到另一个监视器并关闭它时,对话框在重新打开后将为空白

问题描述

环境:Win10+Qt5.12.3(msvc)

代码:

亲:

QT       += core gui

greaterThan(QT_MAJOR_VERSION,4): QT += widgets

TARGET = test
TEMPLATE = app

SOURCES += \
        dialog.cpp \
        main.cpp \
        mainwindow.cpp

HEADERS += \
        dialog.h \
        mainwindow.h

FORMS += \
        dialog.ui \
        mainwindow.ui

主窗口.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QDialog>
#include "dialog.h"

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private slots:
    void slot_btn_clicked();

private:
    Ui::MainWindow *ui;
    Dialog *dialog;
};

#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "dialog.h"

#include <QDebug>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent),ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    dialog = new Dialog(this);

    connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(slot_btn_clicked()));
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::slot_btn_clicked()
{
    dialog->exec();
}

对话框.h

#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>
#include <QCloseEvent>
#include <QShowEvent>

namespace Ui {
    class Dialog;
}

class Dialog : public QDialog
{
    Q_OBJECT
protected:
   void closeEvent(QCloseEvent *e);

public:
    explicit Dialog(QWidget *parent = nullptr);
    ~Dialog();

private:
    Ui::Dialog *ui;
};

#endif // DIALOG_H

对话框.cpp

#include "dialog.h"
#include "ui_dialog.h"

#include <QDebug>

#include <QVBoxLayout>
#include <QLabel>

#include <QPushButton>

Dialog::Dialog(QWidget *parent)
    : QDialog(parent),ui(new Ui::Dialog)
{
    ui->setupUi(this);

    QVBoxLayout *layout = new QVBoxLayout(this);
    QLabel *label = new QLabel(this);
    label->setText("this is a label");
    layout->addWidget(label);

    QPushButton *btnaccept= new QPushButton(this);
    btnaccept->setText("accept");
    connect(btnaccept,&QPushButton::clicked,[=](){accept();});
    layout->addWidget(btnaccept);

    QPushButton *btnclose= new QPushButton(this);
    btnclose->setText("close");
    connect(btnclose,[=](){close();});
    layout->addWidget(btnclose);
}

Dialog::~Dialog()
{
    delete ui;
}

void Dialog::closeEvent(QCloseEvent *e)
{
    qDebug() << __FUNCTION__;

    QDialog::closeEvent(e);

    //e->ignore();  // if ignored close event,working well.
    //accept();
}

demo code pack

Video presentation

我发现一种关闭QDialog的方法会导致这种情况:关闭标题栏中的按钮(对话框的右上角),如果通过reject()、accept()close() 一切正常。

another question 与我的相似,但我认为答案(手动调整对话框大小)并不完美,还有其他解决方案吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...