简体   繁体   中英

How to get a QImage POINTER from model/view

How can I get the QImage * that I stored in a model?

This is where my image manager stores the image:

QImage * tmpImage = new QImage(sFileNames.at(i));

//Add image to the model view
QStandardItem *item = new QStandardItem();            
item->setData(*tmpImage, Qt::DecorationRole);
ImageModel->appendRow(item);   

Now in another class, I want to access the selection in the view and get the pointer. Here's what I have so far:

void NodeEditor::on_set_input_image_clicked()
{
    QModelIndex index = ui->image_list_view->currentIndex();
    QVariant data = ui->image_list_view->model()->data(index);

    //QImage * tmpImg = data.value<QImage*>(); //Returns compilation error
    //pImageMap->SetInputImage(pTmpImg);
}

How? By not using a pointer to start with :)

Modern C++'s primary raison d'être is to enable programming without having to worry about manual memory management. All you have to do is to use a QImage value. They are cheap to copy, and the memory is then managed for you by the compiler and the image implementation. That's all.

To extract the QImage - and not a pointer to it - you'd then write:

auto const role = Qt::DecorationRole;
auto model = ui->image_list_view->model();
auto const image = model->data(index, role).value<QImage>();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM