qtqstandarditemqscopedpointer

cannot convert 'QScopedPointer<T>' to 'QStandardItem *'


I use this code without any error

QStandardItem *newRow;
newRow = new QStandardItem(hostname);
model2->setItem(index, 2, newRow);

I want to change the above code to the below:

QScopedPointer<QStandardItem> newRow(new QStandardItem);
model2->setItem(index, 2, newRow);

But I get this error:

C:\...\mainwindow.cpp:352: error: C2664: 'void QStandardItemModel::setItem(int,int,QStandardItem *)' : cannot convert parameter 3 from 'QScopedPointer<T>' to 'QStandardItem *'
with
[
    T=QStandardItem
]
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

How can I solve the problem?


Solution

  • Try this, use take() method to get the pointer.

    On my computer

    QStandardItem *item2 = new QStandardItem("foo");
           model->setItem(4,0,item2);//works
    
    QScopedPointer<QStandardItem> newRow(new QStandardItem("foo"));
           model->setItem(4,0,newRow.take());//works too