求教编程高手教我如何用Qt实现两窗口切换,比如点击窗口A后,窗口B弹出,而窗口A自动关闭...
发布网友
发布时间:2022-04-23 02:05
我来回答
共1个回答
热心网友
时间:2023-10-11 02:17
//窗口A
#ifndef A_H
#define A_H
#include <QWidget>
#include <QMouseEvent>
class A : public QWidget
{
Q_OBJECT
public:
explicit A(QWidget *parent = 0);
signals:
void showB(bool isShow);
protected:
void mousePressEvent(QMouseEvent *event);
public slots:
};
#endif // A_H
//A.cpp
#include "a.h"
A::A(QWidget *parent) :
QWidget(parent)
{
this->setWindowTitle("我是A");
}
void A::mousePressEvent(QMouseEvent *event){
if(event->button()==Qt::LeftButton){
emit showB(true);
this->deleteLater();
}
}
//窗口B
#ifndef B_H
#define B_H
#include <QWidget>
class B : public QWidget
{
Q_OBJECT
public:
explicit B(QWidget *parent = 0);
signals:
public slots:
void showB(bool isShow);
};
//B.cpp
#include "b.h"
B::B(QWidget *parent) :
QWidget(parent)
{
this->setWindowTitle("我是B");
}
void B::showB(bool isShow){
if(isShow)
this->show();
else
this->deleteLater();
}
//main.cpp
#include <QtGui/QApplication>
#include <QTextCodec>
#include "a.h"
#include "b.h"
int main(int argc, char *argv[])
{
QApplication x(argc, argv);
QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));
B *b=new B();
A *a=new A();
QObject::connect(a,SIGNAL(showB(bool)),b,SLOT(showB(bool)));
a->show();
return x.exec();
}