Results 1 to 11 of 11

Thread: Newbie problem with passing objects to functions

  1. #1
    Join Date
    May 2011
    Posts
    11
    Thanks
    3
    Qt products
    Qt4

    Default Newbie problem with passing objects to functions

    Hi all,

    I just started learning Qt and was working myself through a few tutorials. I have a basic knowledge of c++, but not too much either.

    I've written a small programm. In this program I have a class with a QTextEdit object. This class has a member function which I want to add some text to the QTextEdit object. But I seem to be unable to get it working. I guess the problem lies where I call the particular function, but to me this looks correct.

    Here is the complete code, the problematic line (line 15) is put as comment ("There is a problem"):

    main.cpp:
    Qt Code:
    1. #include "MyWidget.h"
    2. #include <QApplication>
    3. #include <QVBoxLayout>
    4. #include <QWidget>
    5. #include <QTextEdit>
    6.  
    7.  
    8. // Main function
    9. int main(int argc, char *argv[])
    10. {
    11. QApplication app(argc, argv);
    12. MyWidget widget;
    13. widget.show();
    14. return app.exec();
    15. }
    To copy to clipboard, switch view to plain text mode 

    MyWidget.h
    Qt Code:
    1. #ifndef MYWIDGET_H
    2. #define MYWIDGET_H
    3.  
    4. #include <QApplication>
    5. #include <QVBoxLayout>
    6. #include <QWidget>
    7. #include <QTextEdit>
    8.  
    9.  
    10. class MyWidget : public QWidget
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. MyWidget(QWidget *parent = 0);
    16. ~MyWidget();
    17. void addtexttofield(QTextEdit*);
    18. };
    19.  
    20. #endif
    To copy to clipboard, switch view to plain text mode 

    MyWidget.cpp
    Qt Code:
    1. #include "MyWidget.h"
    2.  
    3. // MyWidget class destructor
    4. MyWidget::~MyWidget()
    5. {}
    6.  
    7. // MyWidget class constructor
    8. MyWidget::MyWidget(QWidget *parent)
    9. : QWidget(parent)
    10. {
    11.  
    12. QTextEdit *textfield = new QTextEdit();
    13.  
    14. // There is a problem
    15. // addtexttofield(textfield);
    16.  
    17. // Layout
    18. QVBoxLayout *layout = new QVBoxLayout;
    19. layout->addWidget(textfield);
    20. setLayout(layout);
    21. }
    22.  
    23. // A function to modify QTextEdit content
    24. void addtexttofield(QTextEdit *zieltextfeld)
    25. {
    26. zieltextfeld -> setText("This is something new");
    27. }
    To copy to clipboard, switch view to plain text mode 

    Thanx in advance.

    Tl;dr: How do I call function addtexttofield(QTextEdit*)?

  2. #2
    Join Date
    Mar 2011
    Posts
    63
    Thanks
    11
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Newbie problem with passing objects to functions

    Qt Code:
    1. void MyWidget::addtexttofield(QTextEdit *zieltextfeld)
    2. {
    3. zieltextfeld -> setText("This is something new");
    4. }
    To copy to clipboard, switch view to plain text mode 

    Try this. Added MyWidget:: before the function name because that is where it is declared

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

    ts66 (9th May 2011)

  4. #3
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Newbie problem with passing objects to functions

    The problem is that you declare void addtexttofield(QTextEdit*) in MyWidget.h:
    Qt Code:
    1. class MyWidget : public QWidget
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. MyWidget(QWidget *parent = 0);
    7. ~MyWidget();
    8. void addtexttofield(QTextEdit*); //as a member function
    9. };
    To copy to clipboard, switch view to plain text mode 
    And define a (separate free function) in MyWidget.cpp:
    Qt Code:
    1. //...
    2. void addtexttofield(QTextEdit *zieltextfeld) //this line should be void MyWidget::addtexttowidget(QTextEdit *zieltextfeld)
    3. {
    4. zieltextfeld -> setText("This is something new");
    5. }
    To copy to clipboard, switch view to plain text mode 

    LE: my answer was little late.... :
    And you can use the QTextEdit pointer as a member to your class, so that you don't even have to pass the pointer as parameter, you can just use the original member pointer in the void MyWidget::addtexttowidget()
    Last edited by Zlatomir; 6th May 2011 at 15:08.

  5. #4
    Join Date
    May 2011
    Posts
    11
    Thanks
    3
    Qt products
    Qt4

    Default Re: Newbie problem with passing objects to functions

    Now it works. Thanks again.

  6. #5
    Join Date
    May 2011
    Posts
    11
    Thanks
    3
    Qt products
    Qt4

    Default Re: Newbie problem with passing objects to functions

    Hey, it's me again.

    Quote Originally Posted by Zlatomir View Post
    And you can use the QTextEdit pointer as a member to your class, so that you don't even have to pass the pointer as parameter, you can just use the original member pointer in the void MyWidget::addtexttowidget()
    I tried to do this, but I am facing new problems. Here is what I have done: I declared textfield as a public member of the MyWidget class (and changed theaddtexttofield declaration):

    MyWidget.h
    Qt Code:
    1. #ifndef MYWIDGET_H
    2. #define MYWIDGET_H
    3.  
    4. #include <QApplication>
    5. #include <QVBoxLayout>
    6. #include <QWidget>
    7. #include <QTextEdit>
    8.  
    9.  
    10. class MyWidget : public QWidget
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. MyWidget(QWidget *parent = 0);
    16. ~MyWidget();
    17. void addtexttofield();
    18. QTextEdit textfield;
    19. };
    20.  
    21. #endif
    To copy to clipboard, switch view to plain text mode 

    MyWidget.cpp now looks like this:


    Qt Code:
    1. #include "MyWidget.h"
    2.  
    3. // MyWidget class destructor
    4. MyWidget::~MyWidget()
    5. {}
    6.  
    7. // MyWidget class constructor
    8. MyWidget::MyWidget(QWidget *parent)
    9. : QWidget(parent)
    10. {
    11.  
    12. QTextEdit *textfield = new QTextEdit();
    13.  
    14. // There is still a problem
    15. // addtexttofield();
    16.  
    17. // Layout
    18. QVBoxLayout *layout = new QVBoxLayout;
    19. layout->addWidget(textfield);
    20. setLayout(layout);
    21. }
    22.  
    23. // A function to modify QTextEdit content
    24. void addtexttofield()
    25. {
    26. textfield.setText("This is something new");
    27. }
    To copy to clipboard, switch view to plain text mode 

    I get no compiler errors, and I know addtexttofield is called, as I tested this with a qDebug message. But it doesn't change the content of the the QTextEdit object textfield.

    What am I doing wrong?
    Thanks in advance again.

  7. #6
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Newbie problem with passing objects to functions

    In the code you show us now, you have two textfield one pointer to QTextEdit (declared in the constructor of MyWidget) and one QTextEdit object (member in the MyWidget class) and void addtexttofield() is again a free function, i assume you just edited the previous posted code (that is not the same as the one you have in your project) so post the actual code - else we can't know what is wrong.

  8. #7
    Join Date
    May 2011
    Posts
    11
    Thanks
    3
    Qt products
    Qt4

    Default Re: Newbie problem with passing objects to functions

    I am sorry.
    I corrected the function definition in my project, it is now
    Qt Code:
    1. void MyWidget::addtexttofield()
    To copy to clipboard, switch view to plain text mode 
    , but forgot to update the code I posted here.

    The rest is as I posted it here.

    The two textfield pointers looked curious to me too, but the code didn't compile when I left out the one from the constructor. I could try this again tomorrow, but I can't access my work machine today.

  9. #8
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Newbie problem with passing objects to functions

    Then that is the problem, you create another object (that you add to the layout) and basically show on screen - the one behind the pointer and a second one - the member gets the new text - but this one is never showed.

    To fix it you need the pointer as a member in your class declaration:
    Qt Code:
    1. class MyWidget : public QWidget
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. MyWidget(QWidget *parent = 0);
    7. ~MyWidget();
    8. void addtexttofield();
    9. QTextEdit *textfield; //the pointer is a member
    10. };
    To copy to clipboard, switch view to plain text mode 
    And then use it as in your previous code that worked:
    Qt Code:
    1. MyWidget::MyWidget(QWidget *parent)
    2. : QWidget(parent)
    3. {
    4.  
    5. QTextEdit *textfield = new QTextEdit(); //initialize the pointer
    6.  
    7. // There is still a problem
    8. addtexttofield(); //call the function
    9.  
    10. // Layout
    11. QVBoxLayout *layout = new QVBoxLayout;
    12. layout->addWidget(textfield); //add it to the layout
    13. setLayout(layout);
    14. }
    15.  
    16. // A function to modify QTextEdit content
    17. void MyWidget::addtexttofield()
    18. {
    19. textfield->setText("This is something new"); //use the arrow operator -> //because you only have the pointer
    20. }
    To copy to clipboard, switch view to plain text mode 

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

    ts66 (9th May 2011)

  11. #9
    Join Date
    May 2011
    Posts
    11
    Thanks
    3
    Qt products
    Qt4

    Default Re: Newbie problem with passing objects to functions

    Thanks again for your help. Although I worked through a number c++ of tutorials and had no problems with the exercises, there is obviously still a lot I don't understand. Well, I guess this will change when I do more programming. :-)

    When I tried the code example you gave, the programm crashed with a segmentation fault when calling the function addtextofield(). So I decided to work a bit more on my c++ basics before coming back to this problem.

    The problem is the initialization of textfield in MyWidget.cpp. It works when the line looks like this:
    Qt Code:
    1. textfield = new QTextEdit();
    To copy to clipboard, switch view to plain text mode 
    instead of
    Qt Code:
    1. QTextEdit *textfield = new QTextEdit();
    To copy to clipboard, switch view to plain text mode 
    (which probably simply creates a new instance, but leaves the one declared in the header file uninitialised.)

  12. #10
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Newbie problem with passing objects to functions

    (which probably simply creates a new instance, but leaves the one declared in the header file uninitialised.)
    Exactly, that's why it's good habit to use this keyword to reference data members:
    Qt Code:
    1. this->textfield = new QTextEdit();
    To copy to clipboard, switch view to plain text mode 
    Some programmers use various naming conventions for data members, like:
    Qt Code:
    1. // some class...
    2. class Class{
    3. ...
    4. protected:
    5. int _value; // or:
    6. QString m_text; // m stands for 'member', or even:
    7. float data_;
    8. ...
    To copy to clipboard, switch view to plain text mode 
    This way you can distinguish between data members and local variables even without the this keyword:
    Qt Code:
    1. void Class::setValues( int value, QString text ){
    2. _value = value;
    3. m_text = text;
    4. }
    To copy to clipboard, switch view to plain text mode 

  13. #11
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Newbie problem with passing objects to functions

    @ts66 this is my mistake (i forgot to delete the type there), so as you figure in out the correct line is supposed to be:
    Qt Code:
    1. textfield = new QTextEdit(); //textfield is declared as a class member in the header file, here we just use it - don't re-declare it
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 2
    Last Post: 7th July 2010, 00:14
  2. QtScript: Passing an array of objects to C++
    By Plow in forum Qt Programming
    Replies: 0
    Last Post: 16th April 2010, 12:53
  3. Passing values to custom 'slot' functions (pyqt)
    By Richie in forum Qt Programming
    Replies: 2
    Last Post: 7th September 2009, 07:05
  4. Replies: 6
    Last Post: 15th July 2008, 10:43
  5. Problems passing an array of pointers to objects to a function
    By Valheru in forum General Programming
    Replies: 16
    Last Post: 30th June 2008, 00:11

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.