简体   繁体   中英

Passing QObject with Pointer (Qt)

My goal is to pass the windowobject pointer to another class. I'll show you what i got so far. where: "dialog" is the window to pass.

mainwindow.cpp

dialog = new Dialog(this);
someClass(dialog);

Konstruktor in someClass

 someClass::someClass(Dialog *d)
 {
 Dialog *dia = d;
 }

someClass.h

#include "dialog.h"
...
public:
someClass(Dialog *dialog)
//Dialog d;

The program runs now, but i'm not sure if i achieved what i wanted. Is it possible to interact with my dialog now? What i want is something like this.

 dia->ui->lineEdit->setText();

Any help would be appriciated

someClass(&dialog);

is incorrect ... you have a pointer and give the address of the pointer (a pointer to the pointer) in the function

also you have

Dialog d;

in your header and assigning a Dialog* to it.

I recommend you to have a look at: The Definitive C++ Book Guide and List

We don't know what your Dialog class looks like, but if its member ui is public (or someClass is a friend of Dialog ), then you should be able to do

dia->ui->lineEdit->setText();

Do you get any compiler errors? Or does the text simply not show up as expected? You would still need to show the dialog at some point using

dia->show();

or

dia->exec();

My goal is to pass the windowobject pointer to another class. I'll show you what i got so far. where: "dialog" is the window to pass.

considering your code:

 someClass::someClass(Dialog *d)
 {
 Dialog *dia = d;
 }

is a local member in the constructor someClass. Therefore it only has scope in the constructor itself (is not visible outside of the constructor, and in fact, does not live outside of the constructor (is destroyed when the constructor goes out of scope)).

Fortunately dia is a pointer (address of object), and not the actual dialog (therefore only the pointer, and not the object that it points to goes out of scope). If you want the pointer to remain in scope for the purpose of access later on, you have to "tie" it to the scope of the class (make it a class member).

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(); }

----- Modified/Additional answer -----

If in the above example dialog_ is optional, then you needn't make it a reference member, as reference members require to be initialised (one cannot have an uninitialised reference). In that case, make it a pointer... When using Qt, I would make it a QPointer (Assuming Dialog is a QObject), as QPointers are safer to work with than raw pointers (They are always initialised to zero, at least).

I'll show you the basic principle to keep it simple for now. Read up about QPointers and smart pointers in general.

eg:

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 );
    }
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM