Questions tagged [qsharedpointer]

QSharedPointer is a Qt class which holds a strong reference to a shared pointer.

Introduced in Qt 4.5 QSharedPointer class represents a strong reference to a shared pointer. It is similar to boost::shared_ptr or std::shared_ptr.

The QSharedPointer is an automatic, shared pointer in C++. It is thread-safe and operates atomically on the pointer value.

QSharedPointer holds a shared pointer by means of an external reference count.

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

74 questions
4
votes
1 answer

Understanding QScopedPointer passing by reference

I've been trying to understand how to pass this as a const reference. I have the following class: class DBContext : public QObject In my class MainWindow I define it as folows: private: QScopedPointer dbContext; How Storage class…
adviner
  • 2,355
  • 6
  • 28
  • 54
4
votes
2 answers

Pool of QObject instances

I got a problem I'm not sure how to solve.. We have generic objects pool. When object is requested the pool returns QSharedPointer to the first available instance, with custom Deleter specified. The deleter just returns object to the pool when…
Vlad
  • 191
  • 3
  • 10
3
votes
1 answer

QSharedPointer without the thread-safety overhead?

I need to use a QSharedPointer for the reference counting, but I'll use it in a single-thread application so I don't want the thread-safety overhead (which I assume is fairly expensive). Is there a way to turn off the thread-safety, or is there an…
sashoalm
  • 63,456
  • 96
  • 348
  • 677
3
votes
1 answer

Which memory management method should we use in Qt?

I'm quite new to programming as I'm still learning how to code efficiently so my question may seem a little bit strange. As I'm learning how to code with Qt, I know that Qt has a memory management method that the parent deletes all its children when…
Ryan Wang
  • 343
  • 4
  • 12
3
votes
1 answer

QCache and QSharedPointer

The problem is, that I have a QVector of QSharedPointer and I want to put part of them to my QCache. How can I insert a shared pointer to QCache? What happens if I set the pointer to my cache but destroy the QVector and the shared pointer in it?…
adapto
  • 85
  • 11
3
votes
2 answers

Only plain pointers used in Qt API

I've been working with Qt for somedays and I wonder why all their API uses plain pointers instead of their own smart pointers like QSharedPointer. Wouldn't it be more consistent to use them?
Basti Funck
  • 1,244
  • 15
  • 26
3
votes
2 answers

QSharedPointer from a raw pointer

I currently have something like this: QSharedPointer Qsharedfoo; Now I have a raw pointer to foo as well called rawfoo. How can I make foo point own the raw pointer and start pointing to it. I know in boost with shared pointers we could use…
MistyD
  • 13,289
  • 28
  • 108
  • 198
3
votes
2 answers

Difference between QSharedPointer::isNull() and operator!()

in Qt docs we read: bool QSharedPointer::operator! () const Returns true if this object is null. This function is suitable for use in if-constructs, like: if (!sharedptr) { ... } and bool QSharedPointer::isNull () const Returns true if this…
4pie0
  • 27,469
  • 7
  • 70
  • 110
2
votes
2 answers

why segmentation fault occurs when using a window in a QSharedPointer?

I am developing a gui proram using Qt 4.7.4 (64 bit). I have tried to isolate the problem as follows: I have a window: class PreferencesWindow : public QMainWindow and in another class I initialize and show it as QSharedPointer
destan
  • 4,042
  • 2
  • 32
  • 55
2
votes
2 answers

QTcpSocket and deletion

I'm writing a tcp based server using Qt. I plan this server to be multithreaded, so my tcpserver class inherits from QTcpServer and overrides incomingConnection(). Everything is fine, except when it comes to deleting a user. The TcpServer class…
Xaqq
  • 4,218
  • 2
  • 21
  • 38
2
votes
0 answers

reference count number for QSharedPointer

The way std::shared_ptr has a use_count function which returns the number of shared_ptr objects referring to the same managed object; in the same way how can we get the reference count for a QSharedPointer. I did not find any API for this.. How can…
Nishant Sharma
  • 261
  • 5
  • 14
2
votes
1 answer

this pointer and QSharedPointer

I have a tree-node like class called Message which looks like this: class Message { public: using Ptr = QSharedPointer; public: explicit Message(); explicit Message(Message::Ptr parentPtr); explicit Message(const Data…
Jacob Krieg
  • 2,430
  • 10
  • 60
  • 114
2
votes
1 answer

Managing QAbstractItemModel data in QSharedPointers

In c++ Qt, I like managing my heap memory with QSharedPointers, but can/should one also use them for managing data in a QAbstractItemModel? For example, one can have a list of QStrings QList > queue. The problem is, that when…
Halfgaar
  • 531
  • 2
  • 6
  • 27
2
votes
1 answer

Why there is no overloaded operator T* () in class QSharedPointer

However, class QSharedDataPointer has it. I always have to define a Class::pointer typedef for shortance and create a pointer with Class::pointer(new Class). Does anyone know the reasoning for this?
Juriy
  • 535
  • 1
  • 5
  • 15
2
votes
1 answer

QSharedPointer dynamicCast and objectCast fail on ubuntu

I am using Qt 5.0.1 under Ubuntu 10.04 and in my application I need to use QSharedPointer together with the appropriate dynamic_cast (object_cast) conversions at runtime. These conversions are called in a shared object which is properly loaded at…
arms
  • 153
  • 3
  • 9