C++ 通过指针传递QObject(Qt)

C++ 通过指针传递QObject(Qt),c++,qt,pointers,C++,Qt,Pointers,我的目标是将windowobject指针传递给另一个类。 我会告诉你我到目前为止得到了什么。 其中:“对话框”是要传递的窗口 mainwindow.cpp dialog = new Dialog(this); someClass(dialog); 某个类中的Konstruktor someClass::someClass(Dialog *d) { Dialog *dia = d; } 某个班级 #include "dialog.h" ... public: someClass(Dia

我的目标是将windowobject指针传递给另一个类。 我会告诉你我到目前为止得到了什么。 其中:“对话框”是要传递的窗口

mainwindow.cpp

dialog = new Dialog(this);
someClass(dialog);
某个类中的Konstruktor

 someClass::someClass(Dialog *d)
 {
 Dialog *dia = d;
 }
某个班级

#include "dialog.h"
...
public:
someClass(Dialog *dialog)
//Dialog d;
程序现在运行,但我不确定是否实现了我想要的。 现在可以与我的对话框交互吗? 我想要的是这样的东西

 dia->ui->lineEdit->setText();
任何帮助都将受到感谢

someClass(&dialog);
是不正确的。。。您有一个指针,并在函数中给出指针的地址(指向指针的指针)

你也有

Dialog d;
在标题中,为其指定一个
对话框*


我建议您看看:

我们不知道您的
对话框
类是什么样子的,但是如果它的成员
ui
是公共的(或者
someClass
对话框
的朋友),那么您应该可以这样做

dia->ui->lineEdit->setText();
你有编译器错误吗?还是文本没有如预期的那样出现? 您仍然需要在某个时候使用

dia->show();

我的目标是将windowobject指针传递给另一个类。我将 让你看看我到目前为止得到了什么。其中:“对话框”是要传递的窗口

考虑到您的代码:

 someClass::someClass(Dialog *d)
 {
 Dialog *dia = d;
 }
是构造函数someClass中的本地成员。因此,它只在构造函数本身中具有作用域(在构造函数外部不可见,事实上,在构造函数外部不存在(在构造函数超出作用域时被销毁))

幸运的是,dia是一个指针(对象的地址),而不是实际的对话框(因此只有指针,而不是它指向的对象超出范围)。如果您希望指针保留在作用域中以便以后访问,则必须将其“绑定”到类的作用域(使其成为类成员)

class-MyClass
{
公众:
//正在使用引用,因为它可能不为null。。。
MyClass(对话框和对话框);
void showDialog();
私人:
//我们只想向用户公开对话的一小部分,
//因此,保持它的私密性,并通过
//接口(公共部分)。
对话&Dialog;
};
//MyClass.cpp
MyClass::MyClass(QPointer)
:dialog(dialog)//谷歌“成员初始化”
{
}
void MyClass::showDialog(){dialog_389;.show();}
----修改/补充答案---

如果在上面的示例中,对话框u是可选的,则无需将其设置为引用成员,因为引用成员需要初始化(不能有未初始化的引用)。在这种情况下,将其设置为指针。。。当使用Qt时,我会将其设置为QPointer(假设对话框是QObject),因为使用QPointer比使用原始指针更安全(至少它们总是初始化为零)

我将向您展示目前保持简单的基本原则。阅读有关Qpointer和智能指针的一般信息

e、 g:

class-MyClass
{
公众:
//可能是零,也可能不是零。。。
显式MyClass(Dialog*Dialog=0);
void showDialog();
私人:
//我们只想向用户公开对话的一小部分,
//因此,保持它的私密性,并通过
//接口(公共部分)。
对话*对话;
};
//.cpp
MyClass::MyClass(Dialog*Dialog/*=0*/)
:dialog(dialog)
{
}
void MyClass::showDialog()
{
如果(对话框)
{
对话框->显示();
}
其他的
{
std::coutinputstr;
evalute(inputStr);
}
}
}

非常感谢您的快速回复。我改变了我的程序,也更新了问题。知道出了什么问题吗?对于你的两个问题,你有两个答案…你的答案在这里帮助很大。非常感谢。它现在似乎起作用了。(我可以从MyClass中看到对话框ui)。程序仍然没有运行,因为我在MyClass中有第二个构造函数,我不想在其中传递对话框(因为有一个非ui_版本)。编译器给了我:“未初始化的引用成员‘MyClass::dialog’”。你能告诉我在这种情况下该怎么做吗?有时我更喜欢第二个构造函数,但在这种情况下,默认值就足够了。使其明确以防止不必要的转换。
 someClass::someClass(Dialog *d)
 {
 Dialog *dia = d;
 }
class MyClass 
{
  public:
     //Using reference as it may not be null...
     MyClass( Dialog& dialog );

     void showDialog();

  private:
    //We only want to expose a small part of dialog to users, 
    // hence keep it private, and expose what we want through
    // the interface (the public part).
    Dialog& dialog_;
};

//MyClass.cpp

MyClass::MyClass( QPointer<Dialog> )
: dialog_( dialog ) //Google "member initialisation"
{
}

void MyClass::showDialog(){ dialog_.show(); }
class MyClass 
{
  public:
     // May or may not hold zero...
     explicit MyClass( Dialog* dialog = 0 );

     void showDialog();

  private:
    //We only want to expose a small part of dialog to users, 
    // hence keep it private, and expose what we want through
    // the interface (the public part).
    Dialog* dialog_;
};

//.cpp
MyClass::MyClass( Dialog* dialog /* = 0*/ )
: dialog_( dialog )
{
}

void MyClass::showDialog() 
{
  if( dialog_ ) 
  {
    dialog_->show();
  }
  else
  {
    std::cout << "This is in fact not a dialog"
                 "\nbut be so kind as to enter"
                 " whatever you want here ;-)" 
              << std::endl;

    while( !terminated() )
    {
      std::string inputStr;
      std::cin >> inputStr;
      evalute( inputStr );
    }
  }
}