Questions tagged [qpointer]

QPointer is a Qt class which provides guarded pointers to QObject.

QPointer is a template class which provides quarded pointers to QObject.

Guarded pointer QPointer<T> is like normal pointer T* except it is automatically set to 0 when referenced object is destroyed. T must be subclass of QObject.

Official documentation can be found here for Qt 4.8 and here for Qt 5.

24 questions
45
votes
2 answers

What is the difference between QPointer, QSharedPointer and QWeakPointer classes in Qt?

I have read from the Qt documentations about QPointer, QSharedPointer and QWeakPointer classes. It says: QPointer is a template class that provides guarded pointers to Qt objects and behaves like a normal C++ pointer except that it is…
Nejat
  • 31,784
  • 12
  • 106
  • 138
5
votes
3 answers

Can a QPointer be the key to a std::map

According to SGI's doc on associative containers, "Since elements are stored according to their keys, it is essential that the key associated with each element is immutable". I sometimes use a pointer as a key to std::map, since, although the…
Fred
  • 4,894
  • 1
  • 31
  • 48
5
votes
1 answer

Does QPointer::clear() delete its referenced pointer, or does "Clears this QPointer object." mean something else?

QPointer has a method, clear(). Clears this QPointer object. I am not sure what "clear" exactly means. In my mind, it could mean It deletes the pointer you referenced. or It un-attaches the pointer you referenced, leaving that pointer on the…
Anon
  • 2,267
  • 3
  • 34
  • 51
5
votes
1 answer

Convert std::shared_ptr into QPointer

Our application is VERY poorly designed - it uses both std::shared_ptr and QObject parent-child relationship to manage some of our objects. This leads to segfaults when QObject::~QObject deletes its child objects and THEN shared_ptr tries to delete…
Tomáš Zato
  • 50,171
  • 52
  • 268
  • 778
4
votes
2 answers

Is there a smart pointer that is automatically nulled when its target is destroyed in C++

I've found QPointer. Are there any others?
Neil G
  • 32,138
  • 39
  • 156
  • 257
4
votes
2 answers

QPointer in multi-threaded programs

According to http://doc.qt.io/qt-5/qpointer.html, QPointer is very useful. But I found it could be inefficient in the following context: If I want to show label for three times or do something else, I have to use if(label) label->show1(); if(label)…
echo
  • 789
  • 2
  • 12
  • 21
3
votes
1 answer

Qt: Emit signal when QPointer is changed

I want to make a class similar to QPointer (or subclass it) which will emit a signal whenever the internal pointer gets changed. Unfortunately I need to inherit QObject class to be able to emit signals and also I think I need to use templates to be…
Honza Vojtěch
  • 685
  • 1
  • 7
  • 27
3
votes
2 answers

How should I store pointers in Qt?

My pet project has come to the point where I should start tracking pointer lifetime, and I'm trying to develop some system for it. Sadly, the popular advice to use smart pointers everywhere does not apply since Qt API itself uses naked pointers on…
sigil
  • 815
  • 8
  • 16
3
votes
1 answer

QPointer check is NULL?

When I delete the object the QPointer is pointing to, I check the value of the QPointer, and it is not NULL, but when I check its isNull function, it returns true. And more strangely, when I do (!m_qpointer) it also returns true. So how is this…
Nyaruko
  • 4,329
  • 9
  • 54
  • 105
3
votes
1 answer

QPointer and deleting objects

I've got a Qt App that uses QPointers to bring up new UI Dialogs (Widgets). The main app can have numerous of the same widget loaded with different data. The problem I'm having is in deleting and freeing the memory for each widget. If I monitor the…
Rob S
  • 103
  • 1
  • 2
  • 5
2
votes
1 answer

Is there a QPointer specialization for boost::bind

boost::bind handles boost::shared_ptr the same way as raw pointers. QObject * object(new QObject); boost::shared_ptr sharedObject(new QObject); bind(&QObject::setObjectName, object, _1)( "name" ); bind(&QObject::setObjectName,…
TimW
  • 8,351
  • 1
  • 29
  • 33
2
votes
1 answer

Is this the corrected way to use QPointer?

How could I check whether a pointer's content is deleted? If I use QPointer like this: myClass::myClass(myStruct* p){ _p = p;//_p is a QPointer } myClass::function(){ if(_p) {_p->function();} } then I have myStruct* p = new…
Nyaruko
  • 4,329
  • 9
  • 54
  • 105
1
vote
2 answers

QPointer containing derived object wont match one with the baseclass

Sorry about the wierd title, feel free to come up with a better one if you can think of one. Here is my issue. I have a structure that looks something like this: QObject -> MyBase -> MyDerived I then have a function that will take any of the…
chikuba
  • 4,229
  • 6
  • 43
  • 75
1
vote
1 answer

Store QPointer into a QVariant

Can I store a QPointer, for example a QPointer inside a QVariant and later extract it from it? I tried with: QObject *ob = new QObject(); QPointer qp(ob); QVariant qv(qp); But I got an error -…
sashoalm
  • 75,001
  • 122
  • 434
  • 781
1
vote
1 answer

QPointer for abstract base class

I am writing an expression-parsing library. It is written with Qt, and I have a class structure like this: QCExpressionNode-Abstract Base Class for all pieces of an expression QCConstantNode-Constants in an expression (extends…
Nathan Moos
  • 3,563
  • 2
  • 22
  • 27
1
2