windows 如何在 QT 中从主窗口显示另一个窗口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1518317/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 06:48:12  来源:igfitidea点击:

How to show another window from mainwindow in QT

windowsqtshowdialog

提问by Samir

Platform: QT, Windows XP

平台:QT、Windows XP

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 MyWindowfrom mainwindow?

我是 Qt 的新手。我想从mainwindow. 我做了“ add New Item ->Qt Designer Form Class”,命名它说MyWindow。但如何证明这MyWindowmainwindow

回答by Patrice Bernassola

  1. Implement a slot in your QMainWindow where you will open your new Window,
  2. Place a widget on your QMainWindow,
  3. 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).
  1. 在 QMainWindow 中实现一个插槽,您将在其中打开新窗口,
  2. 在 QMainWindow 上放置一个小部件,
  3. 将此小部件的信号连接到 QMainWindow 的插槽(例如:如果小部件是 QPushButton,则将信号连接click()到您创建的 QMainWindow 自定义插槽)。

Code example:

代码示例:

MainWindow.h

主窗口.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.

这是关于如何显示自定义新窗口的示例。有很多方法可以做到这一点。