求教编程高手教我如何用Qt实现两窗口切换,比如点击窗口A后,窗口B弹出,而窗口A自动关闭...
答案:1 悬赏:0 手机版
解决时间 2021-03-02 23:15
- 提问者网友:伴风望海
- 2021-03-02 05:28
我是用的是Qt3.3.4+vc6.0,我们专业设的课程。能贴出代码最好了,我是个初学者,好多类都不知道什么功能,求高手详细点的回答,谢过了
最佳答案
- 五星知识达人网友:蓝房子
- 2021-03-02 05:47
//窗口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();
}
#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();
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯