Understanding QScopedPointer following a link

I was trying to figure out how to pass this as a reference to const.

I have the following class:

class DBContext : public QObject

In my MainWindow class, I define it as the following:

private:
    QScopedPointer<DBContext> dbContext;

How the storage class is determined:

class StorageData : public QObject
{
    Q_OBJECT

public:
    StorageData(QObject *parent = 0);
    ~StorageData();

    void SetDBContext(const QScopedPointer<DBContext> &db_context);

private:
    QScopedPointer<DBContext> dbContext;

In StorageData SetDBContext (), I try to assign it as follows:

void StorageData::SetDBContext(const QScopedPointer<DBContext> &db_context)
{
    dbContext = db_context;
}

Then, in the MainWindow constructor, initialize it:

dbContext.reset(new DBContext(this));
StorageData storage;
storage.SetDBContext(dbContext);

Using the StorageData function in SetDBContext ():

_DBContext = db_context; 

I get an error message:

'QScopedPointer> :: operator =': cannot access the private member declared in the QScopedPointer class>

Update

Taking the tip of using QSharedPointer and QWeakPointer. Is this the right approach to using these two pointers? Also am I using clear () correctly for classes A and B?

class A
private:
  QWeakPointer<DBContext> dbContext;

void A::~A()
{
     dbContext.clear()
}
void A::SetDBContext(QWeakPointer<DBContext> db_context)
{
    dbContext= db_context;
}

void A::UseCode()
{
    QSharedPointer<DBContext> ptrContext= dbContext.toStrongRef();
    ..
}

class B constructor:
private:
    QSharedPointer<DBContext> dbContext; 

In the constructor:

B::B()
{
    dbContext.reset(new DBContext(this));
    QWeakPointer<DBContext> ptrDBConnect = dbContext;
    storage.SetDBContext(ptrDBConnect);
}

In the destructor:

B::~B()
{
    dbContext.clear();
}
+4
1

QScopedPointer:

QScopedPointer , .

QScopedPointer - , , QScopedPointer. / , .

+2

Source: https://habr.com/ru/post/1616456/


All Articles