如何在QT中的其他小部件中创建对象小部件

时间:2015-09-22 13:33:23

标签: c++ qt

在我的应用程序中我有三个小部件,名为“Widget”,“one”和“two”。我试图在main函数中创建窗口小部件的对象,将其作为参数传递给另一个窗口小部件。它成功编译但应用程序在运行之前崩溃,请参考下面的代码并指导我,

//main.cpp
#include <QtGui/QApplication>
#include "widget.h"
#include "one.h"
#include "two.h"

int main(int argc, char *argv[])
{
  QApplication a(argc, argv);
  Widget *w;
  One *one;
  Two *two;

   w->GetObject(one,two);
   one->GetObject(w,two);
   two->GetObject(one,w);
   w->show();
   return a.exec();
}

//widget.cpp
#include "widget.h"
Widget::Widget(QWidget *parent) :QWidget(parent)
{
  setupUi(this);
}

void Widget::GetObject(One *onee, Two *twoo)
{
   one=onee;
   two=twoo;
}

//widget.h
#ifndef WIDGET_H
#define WIDGET_H

#include "ui_widget.h"
class One;
class Two;
class Widget : public QWidget, private Ui::Widget
{
   Q_OBJECT
   public:
        explicit Widget(QWidget *parent = 0);
        One *one;
        Two *two; 
        void GetObject(One *onee, Two *twoo);
};
#endif // WIDGET_H

//one.cpp
#include "one.h"
One::One(QWidget *parent) :QWidget(parent)
{
    setupUi(this);
}

void One::GetObject(Widget *widgett, Two *twoo)
{
  widget = widgett;
  two = twoo;
}

void One::on_pushButton_clicked()
{
   widget->show();
}

//one.h
#ifndef ONE_H
#define ONE_H

#include "ui_one.h"
class Widget;
class Two;
class One : public QWidget, private Ui::One
{
   Q_OBJECT
   public:
       explicit One(QWidget *parent = 0);
        Widget *widget;
        Two *two;
        void GetObject(Widget *widgett, Two *twoo);
   private slots:
         void on_pushButton_clicked();
 };
 #endif // ONE_H

0 个答案:

没有答案