入门学习qt第一天,跟着教程做了一个简单的例子先,要求如下:
1.两个独立的窗口
2.程序一开始出现主窗口,点击主窗口上的一个按钮,子窗口出现,主窗口隐藏
3.点击子窗口上的一个按钮,主窗口出现,子窗口隐藏
下面我先通过画了一张流程图帮大家理解一下:
下面粘贴各个部分代码:
.pro
#-------------------------------------------------
#
# Project created by QtCreator 2019-03-13T21:01:09
#
#-------------------------------------------------
#模块
QT += core gui
#
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
#可执行文件名
TARGET = day1
TEMPLATE = app
# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as 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 you use 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
CONFIG += c++11
SOURCES += \
main.cpp \
widget.cpp \
subwidget.cpp
HEADERS += \
widget.h \
subwidget.h
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
subwidget.h
#ifndef SUBWIDGET_H
#define SUBWIDGET_H
#include<QPushButton>
#include <QWidget>
class subwidget : public QWidget
{
Q_OBJECT
public:
explicit subwidget(QWidget *parent = nullptr);
void sendslot();
signals:
//信号必须由signals关键字来说明,没有返回值,但可以有参数,信号就是函数的申明,只需申明,无需定义,使用:emit mysignals
void mysignal();
public slots:
private:
QPushButton b;
};
#endif // SUBWIDGET_H
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include<QPushButton>
#include "subwidget.h"
class Widget : public QWidget
{
Q_OBJECT //信号与槽的时候需要
public:
Widget(QWidget *parent = 0);
~Widget();
void myslot();
void changewindow();
void dealsub();
private:
QPushButton b1;
QPushButton *b2;
QPushButton b3;
subwidget w; //定义子窗口
};
#endif // WIDGET_H
main.cpp
#include "widget.h" //窗口控件基类
#include <QApplication>
#include<QPushButton>
int main(int argc, char *argv[])
{
//有且只有一个应用程序类的对象
QApplication a(argc, argv);
Widget w;
w.show();
//让程序一直执行,等待用户操作,即等待事件的发生
return a.exec();
}
subwidget.cpp
#include "subwidget.h"
subwidget::subwidget(QWidget *parent) : QWidget(parent)
{
this->setWindowTitle("br0ther");
b.setParent(this);
b.setText("changtoboss");
b.move(100,100);
connect(&b,&QPushButton::clicked,this,&subwidget::sendslot);
resize(400,300);
}
void subwidget::sendslot()
{
emit mysignal();
}
widget.cpp
#include "widget.h"
#include<QPushButton>
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
b1.setParent(this);
b1.setText("CLOSE");
b1.move(100,100);
b2=new QPushButton(this);
b2->setText("ABC");
//&b1 信号发出者,指针类型
//&QPushButton::pressed 处理的信号,&发送者的类名::信号名字
// this 信号接受者
// &Widget::close 槽函数,信号处理的函数 &接受的类名::槽函数名字
connect(&b1,&QPushButton::pressed,this,&Widget::close);
connect(b2,&QPushButton::released,this,&Widget::myslot);
connect(b2,&QPushButton::released,&b1,&QPushButton::hide);
setWindowTitle("BOSS");
b3.setParent(this);
b3.setText("changetobrother");
b3.move(50,50);
connect(&b3,&QPushButton::pressed,this,&Widget::changewindow);
//w.show();
//处理子窗口信号
connect(&w,&subwidget::mysignal,this,&Widget::dealsub);
resize(400,300);
}
void Widget::dealsub()
{
//子窗口
w.hide();
//本窗口
this->show();
}
void Widget::myslot()
{
b2->setText("123");//wedewewesdawda
}
void Widget::changewindow()
{
//子窗口显示
w.show();
//本窗口
this->hide();
}
Widget::~Widget()
{
}