问题
Initially I made it in the main of the window I had made earlier
回答1:
There is nothing complicated to open a 2nd window (or dialog) with the main window.
To demonstrate this, I made an MCVE.
C++ source testQMainWindowWithQDialog.cc
:
// Qt header:
#include <QtWidgets>
class Dialog: public QDialog {
private:
QVBoxLayout _qVBox;
QLabel _qLbl;
public:
Dialog(QWidget *pQParent = nullptr):
QDialog(pQParent),
_qLbl("the dialog")
{
setWindowTitle("the Dialog Window");
_qVBox.addWidget(&_qLbl, 1, Qt::AlignCenter);
setLayout(&_qVBox);
}
virtual ~Dialog() = default;
Dialog(const Dialog&) = delete;
Dialog& operator=(const Dialog&) = delete;
};
class MainWindow: public QMainWindow {
private:
Dialog _qDlg;
QWidget _qWidgetCentral;
QVBoxLayout _qVBox;
QLabel _qLbl;
public:
MainWindow():
QMainWindow(),
_qDlg(this),
_qLbl("the main window")
{
setWindowTitle("Test QMainWindow with QDialog");
_qVBox.addWidget(&_qLbl, 1, Qt::AlignCenter);
_qWidgetCentral.setLayout(&_qVBox);
setCentralWidget(&_qWidgetCentral);
_qDlg.show();
}
};
// main application
int main(int argc, char **argv)
{
qDebug() << "Qt Version:" << QT_VERSION_STR;
QApplication app(argc, argv);
// setup GUI
MainWindow qWinMain;
qWinMain.show();
// runtime loop
return app.exec();
}
Build script CMakeLists.txt
(to prepare a VS solution for VS2017):
project(QMainWindowWithQDialog)
cmake_minimum_required(VERSION 3.10.0)
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
find_package(Qt5Widgets CONFIG REQUIRED)
include_directories("${CMAKE_SOURCE_DIR}")
add_executable(testQMainWindowWithQDialog testQMainWindowWithQDialog.cc)
target_link_libraries(testQMainWindowWithQDialog Qt5::Widgets)
Built and run on Windows 10, VS2017, Qt 5.13:
a Qt project file testQMainWindowWithQDialog.pro
:
SOURCES = testQMainWindowWithQDialog.cc
QT += widgets
Built and tested on cygwin64 (with X11 server):
$ qmake-qt5 testQMainWindowWithQDialog.pro
$ make && ./testQMainWindowWithQDialog
g++ -c -fno-keep-inline-dllexport -D_GNU_SOURCE -pipe -O2 -Wall -W -D_REENTRANT -DQT_NO_DEBUG -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -I. -isystem /usr/include/qt5 -isystem /usr/include/qt5/QtWidgets -isystem /usr/include/qt5/QtGui -isystem /usr/include/qt5/QtCore -I. -I/usr/lib/qt5/mkspecs/cygwin-g++ -o testQMainWindowWithQDialog.o testQMainWindowWithQDialog.cc
g++ -o testQMainWindowWithQDialog.exe testQMainWindowWithQDialog.o -lQt5Widgets -lQt5Gui -lQt5Core -lGL -lpthread
Qt Version: 5.9.4
I still believe that there must be something else wrong in OP's code.
来源:https://stackoverflow.com/questions/61673728/how-to-make-a-program-with-2-windows-that-open-simultaneously-upon-runtime-in-qt