Results 1 to 12 of 12

Thread: Saving Values from a Dialog to MainWindow - "cannot call member function w/o an objec

  1. #1
    Join Date
    Oct 2012
    Posts
    5
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60

    Default Saving Values from a Dialog to MainWindow - "cannot call member function w/o an objec

    Hi there! This is my first post to qt central. I'm stuck with my trials to save the contents of a Dialog in the calling MainWindow (MainWindow and Dialog created in QTcreator). The scenario: I have a MainWindow with a line edit, an integer spinbox and two buttons (one calls up the Dialog, the other one should call a public function that reads some private variables of class MainWindow and puts them into the line edit and the integer spinbox) and a Dialog with the same input Gadgets.
    Plan was to write some public methods in class MainWindow that should have been used by class Dialog in order to store the values of the Dialog's input wigets in class MainWindow's private vars myTextString and myIntegerZahl.

    Showing up the Dialog works, unless I call void setMyTextString(QString textstring); - in that case the program refuses to compile, throwing an error: "cannot call member function 'void MainWindow::setMyTextString(QString)' without object".

    I did some reading on the Qt examples, but failed to find one that shows what I'm aiming at. Could somebody please show me the correct way of transfering the contents of my Dialog? Here comes the code:

    mainwindow.h
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include "dialog.h"
    6.  
    7. namespace Ui {
    8. class MainWindow;
    9. }
    10.  
    11. class MainWindow : public QMainWindow
    12. {
    13. Q_OBJECT
    14.  
    15. public:
    16. explicit MainWindow(QWidget *parent = 0);
    17. ~MainWindow();
    18. void getMyTextString();
    19. void setMyTextString(QString textstring);
    20. void getMyIntegerZahl();
    21. void setMyIntegerZahl();
    22.  
    23. private slots:
    24. void on_btnDialog_clicked();
    25. void on_btnUebertrag_clicked();
    26.  
    27. private:
    28. Ui::MainWindow *ui;
    29. Dialog *myDialog;
    30. QString myTextString;
    31. int myIntegerZahl;
    32. };
    33.  
    34. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    mainwindow.cpp
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. //#include "dialog.h"
    5.  
    6. MainWindow::MainWindow(QWidget *parent) :
    7. QMainWindow(parent),
    8. ui(new Ui::MainWindow)
    9. {
    10. ui->setupUi(this);
    11. }
    12.  
    13. MainWindow::~MainWindow()
    14. {
    15. delete ui;
    16. }
    17.  
    18. void MainWindow::on_btnDialog_clicked()
    19. {
    20. myDialog = new Dialog;
    21. myDialog->exec();
    22. ui->le_myTextString->setEnabled(true);
    23. }
    24.  
    25. void MainWindow::on_btnUebertrag_clicked()
    26. {
    27.  
    28. }
    29.  
    30. // Austausch-Methoden
    31. void MainWindow::setMyTextString(QString textstring)
    32. {
    33. myTextString = textstring;
    34. }
    To copy to clipboard, switch view to plain text mode 


    dialog.h
    Qt Code:
    1. #ifndef DIALOG_H
    2. #define DIALOG_H
    3.  
    4. #include "ui_dialog.h"
    5.  
    6. //#include <QDialog>
    7.  
    8. namespace Ui {
    9. class Dialog;
    10. }
    11.  
    12. class Dialog : public QDialog, public Ui::Dialog
    13. {
    14. Q_OBJECT
    15.  
    16. public:
    17. Dialog();
    18.  
    19. private slots:
    20. void on_btnSend_clicked();
    21.  
    22. private:
    23. Ui::Dialog *ui;
    24. };
    25.  
    26. #endif // DIALOG_H
    To copy to clipboard, switch view to plain text mode 

    dialog.cpp
    Qt Code:
    1. #include "dialog.h"
    2. #include "ui_dialog.h"
    3. #include "mainwindow.h"
    4.  
    5. Dialog::Dialog()
    6. {
    7. setupUi(this);
    8. }
    9.  
    10. void Dialog::on_btnSend_clicked()
    11. {
    12. QString textstring = this->le_myTextString->text();
    13. MainWindow::setMyTextString(textstring);
    14. }
    To copy to clipboard, switch view to plain text mode 

    Any hints appreciated!

  2. #2
    Join Date
    May 2008
    Location
    Tripoli Libya
    Posts
    70
    Thanks
    10
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Saving Values from a Dialog to MainWindow - "cannot call member function w/o an o

    use Qt emit signals to pass values from Dialog =>MainWindows
    http://doc.qt.io/qt-4.8/signalsandslots
    http://www.digitalfanatics.org/proje...chapter02.html
    Last edited by alrawab; 20th February 2013 at 07:57.

  3. The following user says thank you to alrawab for this useful post:

    mbergmann-sh (20th February 2013)

  4. #3
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Saving Values from a Dialog to MainWindow - "cannot call member function w/o an o

    Qt's Signal&Slot mechanism is great, but not for that task. Use simple getter and setter functions. As an simply -non compilable- example:
    Qt Code:
    1. class Dialog {
    2. QString getString() {return le_myTextString->text();}
    3. };
    4.  
    5. myDialog = new Dialog;
    6. myDialog->exec();
    7. setMyTextString(myDialog->getString());
    To copy to clipboard, switch view to plain text mode 

    And you surely want to check what exec() returns.

  5. The following user says thank you to Lykurg for this useful post:

    mbergmann-sh (20th February 2013)

  6. #4
    Join Date
    May 2008
    Location
    Tripoli Libya
    Posts
    70
    Thanks
    10
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Saving Values from a Dialog to MainWindow - "cannot call member function w/o an o

    is implement of Q_PROPERTY valid in such case ?
    http://www.developer.nokia.com/Commu...use_Q_PROPERTY
    Last edited by alrawab; 20th February 2013 at 12:37.

  7. #5
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Saving Values from a Dialog to MainWindow - "cannot call member function w/o an o

    If the class inherit QObject you can also define Q_PROPERTY to inform the meta object about it, but no need to do so.

  8. #6
    Join Date
    Oct 2012
    Posts
    5
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: Saving Values from a Dialog to MainWindow - "cannot call member function w/o an o

    Quote Originally Posted by alrawab View Post
    use Qt emit signals to pass values from Dialog =>MainWindows
    http://doc.qt.io/qt-4.8/signalsandslots
    http://www.digitalfanatics.org/proje...chapter02.html
    Thanks for your answer and the links! Signal and slot was not what I indendet to do, since I've already s&s'ed the button of the Dialog - this calls the routine to write the values into the private MainWindow class variables. but I solved the Problem: I simply forgott to instantiate MainWindow! everything works fine after changing the slot to this:
    Qt Code:
    1. #include "dialog.h"
    2. #include "ui_dialog.h"
    3. #include "mainwindow.h"
    4.  
    5. Dialog::Dialog()
    6. {
    7. setupUi(this);
    8. }
    9.  
    10. void Dialog::on_btnSend_clicked()
    11. {
    12. MainWindow myMainWindow; // Wichtig: MainWindow instanzieren, sonst Compilerfehler!!!
    13.  
    14. QString textstring = this->le_myTextString->text();
    15. myMainWindow.setMyTextString(textstring);
    16. this->close();
    17. }
    To copy to clipboard, switch view to plain text mode 

  9. #7
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Saving Values from a Dialog to MainWindow - "cannot call member function w/o an o

    Huh, no it is not important to initiate MainWindow. It is totally crap because myMainWindow is destroyed right after the function is left and has nothing in common with the instance of MainWindow you want to deal with. If you want access "MainWindow" from the dialog, you have to pass a pointer or emit a signal. There are no other possibilities if you want to change anything in MainWindow.

    Further use QDialog::accept() instead of close.

  10. The following user says thank you to Lykurg for this useful post:

    mbergmann-sh (20th February 2013)

  11. #8
    Join Date
    Oct 2012
    Posts
    5
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: Saving Values from a Dialog to MainWindow - "cannot call member function w/o an o

    Ha! Triumphed too early...
    now it compiles fine, but since I instantiated MainWindow within void Dialog:n_btnSend_clicked(), it seems that it dies after leaving the Function without writing to MainWindow's private variables.
    Debugging the Function in Dialog returns exactly the values entered, debugging the MainWindow's variables show them empty. I must be totally blind, but I don't see what's going wrong here.
    QString myTextString within Dialog: "Blabberdiblub"
    QString myTextString within MainWindow: ""
    int myIntegerzahl in Dialog: 11
    int myIntegerzahl in Mainwindow: 2 (seems to be a value-by-chance).

    I'm confused...

    These are my Methods in mainwindow.cpp:
    Qt Code:
    1. // Austausch-Methoden
    2. void MainWindow::setMyTextString(QString textstring)
    3. {
    4. myTextString = textstring;
    5. qDebug() << myTextString;
    6. }
    7.  
    8. QString MainWindow::getMyTextString()
    9. {
    10. return myTextString;
    11. }
    12.  
    13. void MainWindow::setMyIntegerZahl(int integerzahl)
    14. {
    15. myIntegerZahl = integerzahl;
    16. qDebug() << "myInt: " << myIntegerZahl << ", Int: " << integerzahl;
    17. }
    18.  
    19. int MainWindow::getMyIntegerZahl()
    20. {
    21. return myIntegerZahl;
    22. }
    To copy to clipboard, switch view to plain text mode 

    ...and this is how I call them in my Dialog:
    Qt Code:
    1. void Dialog::on_btnSend_clicked()
    2. {
    3. MainWindow myMainWindow; // Wichtig: MainWindow instanzieren, sonst Compilerfehler!!!
    4.  
    5. QString textstring = this->le_myTextString->text();
    6. myMainWindow.setMyTextString(textstring);
    7. myMainWindow.setMyIntegerZahl(this->spb_myIntegerZahl->value());
    8.  
    9. this->close();
    10. }
    To copy to clipboard, switch view to plain text mode 

    ...at last, this is my class definitions for MainWindow:
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include "dialog.h"
    6.  
    7. namespace Ui {
    8. class MainWindow;
    9. }
    10.  
    11. class MainWindow : public QMainWindow
    12. {
    13. Q_OBJECT
    14.  
    15. public:
    16. explicit MainWindow(QWidget *parent = 0);
    17. ~MainWindow();
    18. QString getMyTextString();
    19. void setMyTextString(QString textstring);
    20. int getMyIntegerZahl();
    21. void setMyIntegerZahl(int integerzahl);
    22.  
    23. private slots:
    24. void on_btnDialog_clicked();
    25. void on_btnUebertrag_clicked();
    26.  
    27. private:
    28. Ui::MainWindow *ui;
    29. Dialog *myDialog;
    30. QString myTextString; // Ablage für Text aus dem Dialog
    31. int myIntegerZahl; // Ablage für Integer aus dem Dialog
    32. };
    33.  
    34. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 


    aahh - sorry Lykurg, I didn't see you have already pointed to that failure. I'll give it a thought and try to rewrite that crap!
    Last edited by mbergmann-sh; 20th February 2013 at 14:50.

  12. #9
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Saving Values from a Dialog to MainWindow - "cannot call member function w/o an o

    Hi that is basic OOP!
    In Dialog:n_btnSend_clicked() you create a *new* instance of MainWindow which has no connect with the instance of MainWindow from which you execute the dialog. Again define getter functions in the dialog, and after the dialog is finished use them to alter the MainWindow. Have a second look at my example.

  13. #10
    Join Date
    Oct 2012
    Posts
    5
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: Saving Values from a Dialog to MainWindow - "cannot call member function w/o an o

    didn't see you had already pointed to that failure. I'll give it a thought and try to rewrite that crap after overlooking your example. Thanks a lot!

  14. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Saving Values from a Dialog to MainWindow - "cannot call member function w/o an o

    I don't know what your dialog is supposed to be used and what its relation to MainWindow is but if what Lykurg suggested is somehow not an option for you (although it is the best solution possible), another solution would be to pass a pointer to MainWindow to the dialog. However, again, from OOP perspective, it is better to query a value from the dialog and set it on the main window from outside of the dialog.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  15. The following user says thank you to wysota for this useful post:

    mbergmann-sh (20th February 2013)

  16. #12
    Join Date
    Oct 2012
    Posts
    5
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: Saving Values from a Dialog to MainWindow - "cannot call member function w/o an o

    I'm writing on a small alarm watch app consisting of a main window showing times and allowing to start a predefined alarm time, and some user dialogs fpr setting up different alarm tunes (as mp3) and entering events to be shown on wake time. This so far is what I'm working on. Since I need to store the values of those Dialogs in the main window, I thought it as a good idea soing this in the MainWindow class private vars. I wanted to have them central, because of the need of writing them into a presets file and because they are njeeded by the main window in order to do its job. The example code above, however, is a simplified rewrite mirroring my problem - not the app I'm working on. Since I'm relatively new to C++ I expect more quirks and quarks to come there...

    I worked myself through a German book titled "C++ in 21 Tagen", also through "The independant Qt tutorial" and the Blanchette Book on Qt, and I have some knowledge in C from my Amiga times.
    Nevertheless, i see there's more needed to fully understand C++'ing with Qt. Some concepts still confuse me, since I'm still thinking in more procedural terms. Once after a while I hope it'll change to more OOP thinking, but I have to get used to it. Guess there's more practice to be done...
    I'll start giving it a try with both possibilities in some test projects: handing a pointer to MainWindow and using getter functions. The last was what I thought I did, but obviously they are needed in the Dialog - and I implemented them in MainWindow.

    Thanks a lot for all your hints! This seems to be a very friendly forum.

Similar Threads

  1. Cannot call member function without object
    By ehntun in forum Qt Programming
    Replies: 0
    Last Post: 24th October 2012, 07:09
  2. How to call member function from int main()
    By GussieBartlett in forum Qt Programming
    Replies: 3
    Last Post: 5th January 2012, 20:06
  3. to call member function
    By vinayaka in forum General Programming
    Replies: 5
    Last Post: 1st July 2011, 13:48
  4. cannot call member function without object
    By been_1990 in forum Qt Programming
    Replies: 11
    Last Post: 23rd October 2010, 17:12
  5. How to call the C++ member function from the JScript
    By parusri in forum Qt Programming
    Replies: 1
    Last Post: 18th October 2008, 10:13

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.