[英]Open a new Qt window with a slot
In my Qt Program, there is a menu bar where one of the menu options is Settings. 在我的Qt程序中,有一个菜单栏,其中一个菜单选项是“设置”。 When the user click on the Settings window, it should open a Settings window. 当用户单击“设置”窗口时,应该打开“设置”窗口。 The settings window gets opened with a openSettingsWindow()
function. 使用openSettingsWindow()
函数打开设置窗口。 This is how I made the Settings menu in the main window: 这就是我在主窗口中创建“设置”菜单的方式:
QMenu settingsMenu("&Settings");
QAction *settings = toolsMenu.addAction("&Settings");
Window::connect(settings,&QAction::triggered,&mainWindow,[&mainWindow](){
openSettingsWindow();
});
menuBar.addMenu(&toolsMenu);
mainWindow
is the main window and Window
is the class used to create windows which inherits from QWidget
. mainWindow
是主窗口, Window
是用于创建从QWidget
继承的Window
的类。 Its constructor takes two arguments: the title of the window and the icon of the window. 它的构造函数有两个参数:窗口的标题和窗口的图标。 This is the openSettingsWindow()
function: 这是openSettingsWindow()
函数:
void openSettingsWindow(){
Window settingsWindow("Settings","icon.png");
settingsWindow.show();
}
The problem is that when I click ont he Settings option in the Settings menu, the Settings window opens as it should, but it closes directly after less than a second. 问题是,当我单击“设置”菜单中的“设置”选项时,“设置”窗口将按原样打开,但在不到一秒钟后会立即关闭。 What should I do to keep the Settings window opened? 如何保持“设置”窗口打开?
The local variable settingsWindow
gets destructed when your function openSettingsWindow
goes out of scope, you need to keep the object valid as long as you want to show your settingsWindow
. 当你的函数openSettingsWindow
超出范围时,局部变量settingsWindow
会被破坏,只要你想显示你的settingsWindow
,就需要保持对象有效。
one solution would be to allocate the Window
object on the heap, and use the Qt::WA_DeleteOnClose
to make Qt delete the Window
object for you when it is closed, here is how your openSettingsWindow
would look like: 一个解决方案是在堆上分配Window
对象,并使用Qt::WA_DeleteOnClose
让Qt在关闭时为你删除Window
对象,这是你的openSettingsWindow
样子:
void openSettingsWindow(){
Window* settingsWindow = new Window("Settings","icon.png");
settingsWindow->setAttribute(Qt::WA_DeleteOnClose);
settingsWindow->show();
}
You need to return a reference to that Window
and keep it until you're no longer using it. 您需要返回对该Window
的引用并保留它,直到您不再使用它为止。
Window *openSettingsWindow() {
Window *settingsWindow = new Window("Settings, "icon.png");
settingsWindow.show();
return settingsWindow;
}
QMenu settingsMenu("&Settings");
QAction *settings = toolsMenu.addAction("&Settings");
Window *settingsWindow = null;
Window::connect(settings,&QAction::triggered,&mainWindow,[&mainWindow, &settingsWindow](){
settingsWindow = openSettingsWindow();
});
menuBar.addMenu(&toolsMenu);
You might want to find a better way of storing the settingsWindow
pointer in the main function if you're going to have many possible open windows but this will work. 您可能想要找到一种更好的方法来将settingsWindow
指针存储在main函数中,如果您将要有许多可能的打开窗口,但这将起作用。
Remember to call delete()
on that pointer when you're done with the settings window (likely on the window close event) 完成设置窗口后,请记得在该指针上调用delete()
(可能在窗口关闭事件中)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.