注册 登录
编程论坛 QT论坛

qt+vs2008小例子 窗口切换一种办法

lateraware 发布于 2012-03-06 13:07, 1172 次点击
/***************   window.h   ***********/
#ifndef _WINDOW_H
#define _WINDOW_H

#include <qwidget.h>

class QPushButton;

class MyWidget1:public QWidget
{
    Q_OBJECT
public:
    MyWidget1(QWidget *parent = 0, const char *name = 0);
private:
    QPushButton *close;
signals:
    void send1();
};

class MyWidget2:public QWidget
{
    Q_OBJECT
public:
    MyWidget2(QWidget *parent = 0, const char *name = 0);
private:
    QPushButton *back;
signals:
    void send2();
};

class MyWidget:public QWidget
{
public:
    MyWidget(QWidget *parent = 0, const char *name = 0);
private:
    MyWidget1 *w1;
    MyWidget2 *w2;
};

#endif
/*******************************************************/

/********************   window.cpp  ********************/
#include "window.h"
#include "window_moc.cpp"
#include <qpushbutton.h>

MyWidget1::MyWidget1(QWidget *parent, const char *name):QWidget(parent, name)
{
    close = new QPushButton(tr("close"), this);

    connect(close, SIGNAL(clicked()), this, SIGNAL(send1()));
    connect(close, SIGNAL(clicked()), this, SLOT(close()));
    show();
}

MyWidget2::MyWidget2(QWidget *parent, const char *name):QWidget(parent, name)
{
    back = new QPushButton(tr("back"), this);

    connect(back, SIGNAL(clicked()), this, SIGNAL(send2()));
    connect(back, SIGNAL(clicked()), this, SLOT(close()));
}

MyWidget::MyWidget(QWidget *parent, const char *name):QWidget(parent, name)
{
    w1 = new MyWidget1;
    w2 = new MyWidget2;
//emit(send1())
    connect(w1, SIGNAL(send1()), w2, SLOT(show()));
    connect(w2, SIGNAL(send2()), w1, SLOT(show()));
}
/****************************************************************/

/**************************   main.cpp   ************************/
#include "window.h"
#include <qapplication.h>

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


/**********************  配置  **********************************/
附加包含目录:C:\Qt\4.7.4\include\Qt;C:\Qt\4.7.4\include\QtGui;C:\Qt\4.7.4\src\gui\kernel
附加依赖项:qtcore4.lib qtgui4.lib
右键window.h=》属性=》命令行:moc $(InputPath) -o $(InputDir)$(InputName)_moc.cpp 输出:$(InputName)_moc.cpp
配置环境变量:
选择工具->选项->项目和解决方案->VC++目录。
               (1)  选择“可执行文件”,在里面添加:C:\Qt4.7.4\bin;
               (2)  选择“包含文件”,在里面添加:C:\Qt4.7.4\include;   C:\Qt4.7.4\include\Qt;   
                      C:\Qt4.7.0\include\QtCore;   C:\Qt4.7.4\include\QtGui
               (3)  选择“库文件”,在里面添加:C:\Qt4.7.4\lib;
/******************************************************************/

0 回复
1