我有个问题:

我有一个具有类MoveSeries的类用户界面。从MoveSeries,我想访问类用户界面的方法。在此示例中,我想访问用户界面的方法get_MoveCurve_Delta()。我如何从已创建的类(MoveSeries)访问创建类(用户界面)?我尝试了Signal-Slot-Approach,但是由于我不得不多次使用几种用户界面方法,因此会产生很多信号插槽...

这是我的代码:

Userinterface.h:

 class UserInterface : public QMainWindow
 {
     Q_OBJECT

 public:
     UserInterface(QWidget *parent = 0, Qt::WFlags flags = 0);
     ~UserInterface();
     ...
 private:
     double MoveCurve_Delta;
     MoveSeries *MOVE_SERIES  ;
 public:
     void set_MoveCurve_Delta( double val)     { MoveCurve_Delta  = val;}
     double get_MoveCurve_Delta()      { return MoveCurve_Delta  ;}
 }


Userinterface.cpp:

 UserInterface::UserInterface(QWidget *parent, Qt::WFlags flags) :
 QMainWindow(parent, flags)
 {
     ui.setupUi(this);
     ...

     MOVE_SERIES = new MoveSeries( this);
 }


MoveSeries.h:

  class MoveSeries : public QDialog
  {
     Q_OBJECT
 public:
     explicit MoveSeries(QWidget *parent = 0);
     ~MoveSeries();
     ...


MoveSeries.cpp:

 MoveSeries::MoveSeries(QWidget *parent) :
 QDialog(parent),ui(new Ui::MoveSeries)
 {
     ui->setupUi(this);
     this->parent = parent;
     parent->set-MoveSeries_Delta_Val();
 }

最佳答案

您也可以不要求将MoveSeries中的父QWidget假定为UserInterface而不是。

MoveSeries.h:

  class UserInterface; // only need a forward declaration

  class MoveSeries : public QDialog
  {
     Q_OBJECT
 public:
     explicit MoveSeries(UserInterface *parent = 0);
     ~MoveSeries();
     ...
     UserInterface * uiparent;
 }


MoveSeries.cpp:

 #include "Userinterface.h" // include the header where it is required

 MoveSeries::MoveSeries(UserInterface *parent) :
 QDialog(parent), ui(new Ui::MoveSeries), uiparent(parent)
 {
     ui->setupUi(this);
     uiparent->set-MoveSeries_Delta_Val();
 }

10-08 09:35