qt-C++笔记之两个窗口ui的交互

code review!

文章目录

qt-C++笔记之两个窗口ui的交互0.运行1.文件结构2.先创建widget项目,搞一个窗口ui出来3.项目添加第二个widget窗口出来4.补充代码4.1.qt_widget_interaction.pro4.2.main.cpp4.3.widget.h4.4.widget.cpp4.5.second_widget.h4.6.second_widget.cpp4.7.widget.ui4.8.second_widget.ui

0.运行

1.文件结构

2.先创建widget项目,搞一个窗口ui出来

3.项目添加第二个widget窗口出来

4.补充代码

4.1.qt_widget_interaction.pro

代码

QT += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++11

# The following define makes your compiler emit warnings if you use

# any Qt feature that has been marked deprecated (the exact warnings

# depend on your compiler). Please consult the documentation of the

# deprecated API in order to know how to port your code away from it.

DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if it uses deprecated APIs.

# In order to do so, uncomment the following line.

# You can also select to disable deprecated APIs only up to a certain version of Qt.

#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \

main.cpp \

second_widget.cpp \

widget.cpp

HEADERS += \

second_widget.h \

widget.h

FORMS += \

second_widget.ui \

widget.ui

# Default rules for deployment.

qnx: target.path = /tmp/$${TARGET}/bin

else: unix:!android: target.path = /opt/$${TARGET}/bin

!isEmpty(target.path): INSTALLS += target

4.2.main.cpp

代码

#include "widget.h"

#include

int main(int argc, char *argv[])

{

QApplication a(argc, argv);

Widget w;

w.show();

return a.exec();

}

4.3.widget.h

代码

#ifndef WIDGET_H

#define WIDGET_H

#include

#include

QT_BEGIN_NAMESPACE

namespace Ui { class Widget; }

QT_END_NAMESPACE

class Widget : public QWidget

{

Q_OBJECT

public:

Widget(QWidget *parent = nullptr);

~Widget();

private slots:

void on_push_second_widget_clicked();

void show_widget();

private:

Ui::Widget *ui;

};

#endif // WIDGET_H

4.4.widget.cpp

代码

#include "widget.h"

#include "ui_widget.h"

Widget::Widget(QWidget *parent)

: QWidget(parent)

, ui(new Ui::Widget)

{

ui->setupUi(this);

}

Widget::~Widget()

{

delete ui;

}

void Widget::show_widget()

{

this->show();

}

void Widget::on_push_second_widget_clicked()

{

second_widget* f = new second_widget;

f->show();

this->hide();

connect(f,SIGNAL(close_and_open()),this,SLOT(show_widget()));

}

4.5.second_widget.h

代码

#ifndef SECOND_WIDGET_H

#define SECOND_WIDGET_H

#include

namespace Ui {

class second_widget;

}

class second_widget : public QWidget

{

Q_OBJECT

public:

explicit second_widget(QWidget *parent = nullptr);

~second_widget();

private slots:

void on_pushButton_clicked();

signals:

void close_and_open();

private:

Ui::second_widget *ui;

};

#endif // SECOND_WIDGET_H

4.6.second_widget.cpp

代码

#include "second_widget.h"

#include "ui_second_widget.h"

second_widget::second_widget(QWidget *parent) :

QWidget(parent),

ui(new Ui::second_widget)

{

ui->setupUi(this);

}

second_widget::~second_widget()

{

delete ui;

}

void second_widget::on_pushButton_clicked()

{

emit close_and_open();

this->hide();

}

4.7.widget.ui

代码

Widget

0

0

800

600

Widget

350

210

171

41

first_widget

70

340

281

51

open scond_widget

4.8.second_widget.ui

代码

second_widget

0

0

460

312

Form

100

120

211

41

second_widget

20

210

411

41

close_second_and_open_first

相关链接

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: