0

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

3 Answers3

2
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

Community
  • 1
  • 1
Zaiborg
  • 2,492
  • 19
  • 28
  • well thanks a lot for your quick response. I changed my program and also i updated the question. Any idea whats going wrong? – user3461075 Sep 11 '14 at 18:13
0

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();
Roman Schaub
  • 126
  • 1
  • 8
0

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.

e.g:

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 );
    }
  }
}
Werner Erasmus
  • 3,988
  • 17
  • 31
  • Your answer helped a lot here. Thank you. It seems to work now. (I can see the Dialog ui from MyClass).Still the program doesn't run because i have a second constructor in MyClass in which i don't want to pass a dialog ( as there is a nonGUI_version). Compiler gives me:" uninitialized reference member 'MyClass::dialog_'". Can you tell me what to do in this case? – user3461075 Sep 12 '14 at 09:31
  • Sometimes I prefer a second constructor, but in this case a default is sufficient. Make it explicit to prevent unwanted conversions. – Werner Erasmus Sep 12 '14 at 10:19