qt点击按钮弹出新窗口
时间: 2023-07-12 08:03:35 浏览: 1274
可以通过创建一个新的窗口类,然后在主窗口中创建一个按钮并连接到一个槽函数来实现弹出新窗口。
首先,创建一个新的窗口类,例如NewWindow:
```cpp
#include <QWidget>
class NewWindow : public QWidget
{
Q_OBJECT
public:
explicit NewWindow(QWidget *parent = nullptr);
};
```
在NewWindow.cpp中实现构造函数:
```cpp
#include "newwindow.h"
NewWindow::NewWindow(QWidget *parent) : QWidget(parent)
{
setFixedSize(200, 100);
setWindowTitle("New Window");
}
```
在主窗口中创建一个按钮,并连接到一个槽函数。在槽函数中创建NewWindow实例并显示:
```cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "newwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
// 创建一个按钮
QPushButton *button = new QPushButton("Open New Window", this);
button->setGeometry(QRect(QPoint(100, 100), QSize(150, 50)));
// 连接到槽函数
connect(button, &QPushButton::clicked, this, &MainWindow::openNewWindow);
}
void MainWindow::openNewWindow()
{
NewWindow *newWindow = new NewWindow(this);
newWindow->show();
}
```
现在运行程序,在主窗口中点击按钮即可弹出一个新的窗口。
阅读全文
相关推荐
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)