第一个qt小界面:新建窗口和按钮

新建窗口和按钮

功能:弹出一个窗口1,窗口1有两个按钮,一个open,一个close;单击open弹出一个新窗口2,新窗口2有 一个close按钮,单击之后关闭新窗口2。在窗口1单击close按钮关闭窗口1。
代码:
//open3.h

#pragma once

#include <QtWidgets/QMainWindow>
#include<QPushButton>
#include "ui_open3.h"
#include<new_window.h>

class open3 : public QMainWindow
{
    
    
	Q_OBJECT

public:
	open3(QWidget *parent = Q_NULLPTR);

private:
	Ui::open3Class ui;
	QPushButton *btn;
	QPushButton *btn2;
private slots:
	void slot_start_window();
};

//open3.cpp:

#include "open3.h"

open3::open3(QWidget *parent)
	: QMainWindow(parent)
{
    
    
	ui.setupUi(this);
	resize(300, 300);
	btn = new QPushButton("open", this);
	btn2 = new QPushButton("close", this);
	btn->move(100, 100);
	btn2->move(100, 200);
	QObject::connect(btn, SIGNAL(clicked()), this, SLOT(slot_start_window()));
	QObject::connect(btn2, SIGNAL(clicked()), this, SLOT(close()));
}

void open3::slot_start_window()
{
    
    
	new_window *w_ = new new_window();
	w_->show();
}

//new_window.h:

#pragma once
#include <qmainwindow.h>
#include<QPushButton>
class new_window :public QMainWindow
{
    
    
	Q_OBJECT

public:
	new_window(QWidget *parent = Q_NULLPTR);
	~new_window();

private:
	QPushButton *close_btn;
	
};

//new_window.cpp:

#include "new_window.h"
#pragma execution_character_set("UTF-8")//这句代码可以让编译器识别中文,不然中文会乱码

new_window::new_window(QWidget *parent)
	: QMainWindow(parent)
{
    
    
	//open3.h是第一个窗口,上面有一个open按钮,按下会弹出一个新窗口2
	//新弹出的窗口2有一个close按钮,按下会关闭窗口2
	resize(200, 100);
	setFixedSize(200,100);//固定窗口大小,不允许拖大拖小
	setWindowTitle("新建的窗口2");
	close_btn = new QPushButton("close", this);
	close_btn->move(50,50);
	connect(close_btn, SIGNAL(clicked()), this, SLOT(close()));
}

new_window::~new_window()
{
    
    

}

//main.cpp:

#include "open3.h"
#include <QtWidgets/QApplication>

int main(int argc, char *argv[])
{
    
    
	QApplication a(argc, argv);
	open3 w;
	w.show();
	return a.exec();
}

猜你喜欢

转载自blog.csdn.net/weixin_44650358/article/details/113513847
今日推荐