Results 1 to 6 of 6

Thread: Returning QPointer via function from one class to another, using the QPointer's data

  1. #1

    Default Returning QPointer via function from one class to another, using the QPointer's data

    I've declared a qpointer to a QLineEdit pointer called txtNewCareer in the class Populate. I have created a getter function in Populate so other classes can access that pointer. It is a dynamically created pointer that creates a QLineEdit, which I need to keep as dynamic and on the heap.

    So, how do I appropriately call my getter function return_txtNewCareer()? I've had not problems returning regular pointers. But I am just learning how to use QPointers.
    As you can see I'm trying to connect a signal of the QPointer txtNewCareer by calling the return_txtNewCareer function of class Populate, to a slot in mainwindow class.

    Thanks.
    This is a skeleton of my classes with the basic information of what I'm doing.

    Populate.h
    Qt Code:
    1. #include <QPointer>
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. public:
    2. QLineEdit* return_txtNewCareer();
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. private:
    2. QPionter<QLineEdit> txtNewCareer;
    To copy to clipboard, switch view to plain text mode 


    Populate.cpp
    Qt Code:
    1. QPointer<QLineEdit> txtNewCareer = new QLineEdit
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. QLineEdit* Populate::return_txtNewCareer(){
    2. return txtNewCareer;
    3. }
    To copy to clipboard, switch view to plain text mode 

    mainwindow.cpp
    Qt Code:
    1. connect(populate->return_txtNewCareer(), SIGNAL(textEdited(QString)), this, SLOT(txtNewCareer_edited(QString)));
    To copy to clipboard, switch view to plain text mode 

    I am unable to get the program to run. I have narrowed down the problem to the connect statement because I have commented it out and the program runs. When I run the program with the connect statement I get this Segmentation fault:

    Signal name : SIGSEGV
    Signal meaning : Segmentation fault
    Last edited by pditty8811; 19th March 2015 at 02:48.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Returning QPointer via function from one class to another, using the QPointer's d

    Have you checked the result of the getter method?
    Is it the pointer you expect?

    Cheers,
    _

  3. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,310
    Thanks
    314
    Thanked 870 Times in 857 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Returning QPointer via function from one class to another, using the QPointer's d

    QPointer<QLineEdit> txtNewCareer = new QLineEdit
    Although it is impossible to tell from your "one-liner" code posts where this line occurs, if it is indeed as you show it here, it implies you are creating a local QPointer named "txtNewCareer" which is hiding the private member declared in your class definition. As a consequence, your private member is never initialized (i.e. contains a NULL QLineEdit pointer instance) and of course will cause a SEGFAULT when you try to use it.

  4. #4

    Default Re: Returning QPointer via function from one class to another, using the QPointer's d

    Quote Originally Posted by d_stranz View Post
    Although it is impossible to tell from your "one-liner" code posts where this line occurs, if it is indeed as you show it here, it implies you are creating a local QPointer named "txtNewCareer" which is hiding the private member declared in your class definition. As a consequence, your private member is never initialized (i.e. contains a NULL QLineEdit pointer instance) and of course will cause a SEGFAULT when you try to use it.
    Qt Code:
    1. QPointer<QLineEdit> txtNewCareer = new QLineEdit;
    To copy to clipboard, switch view to plain text mode 
    Thanks for the response. How would I initialize it so that it can be access from other classes? Should I make it public? This is often considered bad practice. However, when I change it to public I still get the same problem. So that can't be it.

    I understand what you are saying, but if I initialize a non-smart pointer that way it works just fine. But I'd like to use smart pointers to help with garbage clean-up.

    Quote Originally Posted by anda_skoa View Post
    Have you checked the result of the getter method?
    Is it the pointer you expect?

    Cheers,
    _
    I've tried to use the getter as if it was a non-smart pointer and I can compile but I get segfault during running. And no, the getter does not work as it should.
    Last edited by pditty8811; 19th March 2015 at 20:18.

  5. #5
    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: Returning QPointer via function from one class to another, using the QPointer's d

    Qt Code:
    1. How would I initialize it
    To copy to clipboard, switch view to plain text mode 
    http://www.learncpp.com/cpp-tutorial...ization-lists/

    Should I make it public?
    No, just initialize the class member instead of creating a local object with the same name, as d_stranz already suggested.

    But I'd like to use smart pointers to help with garbage clean-up.
    QPointer is not managing the cleanup. If you want your object to be automatically deleted when a pointer goes out of scope, use QScopedPointer or QSharedPointer.

  6. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,310
    Thanks
    314
    Thanked 870 Times in 857 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Returning QPointer via function from one class to another, using the QPointer's d

    However, when I change it to public I still get the same problem. So that can't be it.
    As I said before, and as stampede reiterated, if you are doing this:

    Qt Code:
    1. void SomeClass::someMethod()
    2. {
    3. QPointer<QLineEdit> txtNewCareer = new QLineEdit;
    4.  
    5. // ...
    6. }
    To copy to clipboard, switch view to plain text mode 

    The variable "txtNewCareer" in someMethod() is not the same variable as the one in your class definition. It is a local variable, defined only within the scope of someMethod(). Even if it has the same name as the one in your class definition, it is a different variable and "hides" the one defined in your class. So you are initializing a variable that goes out of scope as soon as the method exits (and creates a memory leak in the process). The variable named txtNewCareer in your class definition is never initialized (except to set it to NULL by the default QPointer constructor), and so when you go to use it, it segfaults.

    If you still don't understand this and why what you're doing is wrong, go back to your C++ books and read about scoping.

    Should I make it public?
    If you really need to get access to the QLineEdit pointer from outside your class, then don't make it a public variable, add a method to safely retrieve the pointer from your class: QPointer< QLineEdit > getLineEdit() const;

    But that is nearly as bad an idea as making the variable public, because you have no control over how it is used, and if you change your UI design, everywhere that that pointer is used will have to be changed also. It makes your code fragile. Probably what you actually need is access to the contents of the QLineEdit, not to the widget itself. So in that case, provide getter and setter methods to get or set the QLineEdit text:

    Qt Code:
    1. QString getText() const
    2. {
    3. return txtNewCareer->text();
    4. }
    5.  
    6. void setText( const QString & text )
    7. {
    8. txtNewCareer->setText( text );
    9. }
    To copy to clipboard, switch view to plain text mode 

    Even if you change the UI to use something instead of a QLineEdit, the rest of your program won't care. The only two lines you'll need to change are the lines that access the QLineEdit from within your getter and setter methods.

Similar Threads

  1. Replies: 0
    Last Post: 27th September 2013, 06:09
  2. How to mix QScopedPointer with QPointer
    By jezz in forum Qt Programming
    Replies: 5
    Last Post: 14th April 2012, 08:20
  3. How to cast QPointer<T> to QPointer<childT>
    By cafu in forum Qt Programming
    Replies: 4
    Last Post: 19th March 2010, 10:51
  4. Problem using QPointer
    By weaver4 in forum Newbie
    Replies: 8
    Last Post: 20th February 2010, 05:05
  5. QMutableVectorIterator and QPointer?
    By Scorp2us in forum Qt Programming
    Replies: 1
    Last Post: 8th November 2008, 19:39

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
  •  
Qt is a trademark of The Qt Company.