Qt about the problem that the new sub-window QDialog modification coordinates cannot overlap the main window

Modify the problem that the coordinates cannot overlap on the main window

foreword

This involves an inherited problem. The window created in Qt is centered on the screen by default. Similarly, if the sub-window you set inherits from the main window, it will automatically follow the main window and display in the center. So to create a sub-window, you only need to modify the fixed size of the lower window. If you need to change the size of the sub-window, set the maximum and minimum sizes, so that you can solve the problem of not being able to align the main window.

Reference source code:

First create a GUI class that inherits from QDialog!

Header file: bugtest.h

#pragma once

#include <QtWidgets/QWidget>
#include "ui_bugtest.h"
#include "xdialog.h"

class BugTest : public QWidget
{
    
    
	Q_OBJECT

public:
	BugTest(QWidget *parent = Q_NULLPTR);
public slots:
    //子窗口
	void message();
private:
	Ui::BugTestClass ui;
	XDialog *m;
};

Implementation file: bugtest.cpp

#include "bugtest.h"

BugTest::BugTest(QWidget *parent)
	: QWidget(parent)
{
    
    
	ui.setupUi(this);
	connect(ui.pushButton, SIGNAL(clicked()), this, SLOT(message()));
}
void BugTest::message()
{
    
    
	m = new XDialog(this);//这里是继承于当前主窗口
	m->setWindowTitle("My XDialog");//设置标题
	m->setFixedSize(QSize(this->width(), this->height()));//设置固定大小
	m->exec();
}

Guess you like

Origin blog.csdn.net/weixin_45357007/article/details/125360680