当前位置: 代码迷 >> QT开发 >> qt一个窗体怎样调用另一个窗体的控件解决方法
  详细解决方案

qt一个窗体怎样调用另一个窗体的控件解决方法

热度:480   发布时间:2016-04-25 05:13:16.0
qt一个窗体怎样调用另一个窗体的控件
form1里面就一个按钮(pushButton);
form2里面有 label,
当点击pushButton后在label现实图像

dialog1.h
#define DIALOG1_H

#include <QDialog>
#include "dialog2.h"


namespace Ui {
  class Dialog1;
}

class Dialog1 : public QDialog{
  Q_OBJECT
public:
  Dialog1(QWidget *parent = 0);
  ~Dialog1();

protected:
  void changeEvent(QEvent *e);

private:
  Ui::Dialog1 *uii;


};

#endif // DIALOG1_H

dialog2.h
#define DIALOG2_H

#include <QDialog>
#include "dialog1.h"
#include "ui_dialog1.h"

namespace Ui {
  class Dialog2;
 
}

class Dialog2 : public QDialog {
  Q_OBJECT
public:
  Dialog2(QWidget *parent = 0);
  ~Dialog2();

protected:
  void changeEvent(QEvent *e);

private:
  Ui::Dialog2 *ui;
  Ui_Dialog1 *w2;

private slots:
  void on_pushButton_clicked();
};

#endif // DIALOG2_H

dialog1.cpp
#include "dialog1.h"
#include "ui_dialog1.h"


Dialog1::Dialog1(QWidget *parent) :
  QDialog(parent),
  uii(new Ui::Dialog1)
{
  uii->setupUi(this);
}

Dialog1::~Dialog1()
{
  delete uii;
}

void Dialog1::changeEvent(QEvent *e)
{
  QDialog::changeEvent(e);
  switch (e->type()) {
  case QEvent::LanguageChange:
  uii->retranslateUi(this);
  break;
  default:
  break;
  }
}

dialog2.cpp
#include "dialog2.h"
#include "ui_dialog2.h"
#include "ui_dialog1.h"

Dialog2::Dialog2(QWidget *parent) :
  QDialog(parent),
  ui(new Ui::Dialog2)
{
  ui->setupUi(this);
   
}

Dialog2::~Dialog2()
{
  delete ui;
}

void Dialog2::changeEvent(QEvent *e)
{
  QDialog::changeEvent(e);
  switch (e->type()) {
  case QEvent::LanguageChange:
  ui->retranslateUi(this);
  break;
  default:
  break;
  }
}

void Dialog2::on_pushButton_clicked()
{

  w2->label_2->setText("ss");
}
当点击按钮后,会出现The program has unexpectedly finished.(程序异常结束)
问下应该怎样解决

------解决方案--------------------
当点击按钮后,会出现The program has unexpectedly finished.(程序异常结束)
问下应该怎样解决

answer:
在Dialog2类中:
Ui_Dialog1 *w2; //此变量并没有new 对象

所以在
w2->label_2->setText("ss");//出现异常
  相关解决方案