如何在 QT 中从主窗口显示另一个窗口 [英] How to show another window from mainwindow in QT
本文介绍了如何在 QT 中从主窗口显示另一个窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
平台:QT、Windows XP
Platform: QT, Windows XP
我是 Qt 的新手.我想从 mainwindow
显示另一个窗口(如何将其打开为对话框).我做了add New Item ->Qt Designer Form Class
",命名为 MyWindow
.但是如何从 mainwindow
显示这个 MyWindow
?
I am new to Qt. I want to show another window(what to do to open it as dialog) from mainwindow
. I did "add New Item ->Qt Designer Form Class
", named it say MyWindow
. But how to show this MyWindow
from mainwindow
?
推荐答案
- 在 QMainWindow 中实现一个插槽,您将在其中打开新窗口,
- 在 QMainWindow 上放置一个小部件,
- 将此小部件的信号连接到 QMainWindow 的插槽(例如:如果小部件是 QPushButton,则将信号
click()
连接到您创建的 QMainWindow 自定义插槽).立>
- Implement a slot in your QMainWindow where you will open your new Window,
- Place a widget on your QMainWindow,
- Connect a signal from this widget to a slot from the QMainWindow (for example: if the widget is a QPushButton connect the signal
click()
to the QMainWindow custom slot you have created).
代码示例:
MainWindow.h
// ...
include "newwindow.h"
// ...
public slots:
void openNewWindow();
// ...
private:
NewWindow *mMyNewWindow;
// ...
}
MainWindow.cpp
// ...
MainWindow::MainWindow()
{
// ...
connect(mMyButton, SIGNAL(click()), this, SLOT(openNewWindow()));
// ...
}
// ...
void MainWindow::openNewWindow()
{
mMyNewWindow = new NewWindow(); // Be sure to destroy your window somewhere
mMyNewWindow->show();
// ...
}
这是一个关于如何显示自定义新窗口的示例.有很多方法可以做到这一点.
This is an example on how display a custom new window. There are a lot of ways to do this.
这篇关于如何在 QT 中从主窗口显示另一个窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文