简体   繁体   English

如何从模型/视图获取QImage POINTER

[英]How to get a QImage POINTER from model/view

How can I get the QImage * that I stored in a model? 如何获取存储在模型中的QImage *?

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. 现代C ++的主要目的是实现编程,而不必担心手动内存管理。 All you have to do is to use a QImage value. 您要做的就是使用QImage值。 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: 要提取QImage而不是指向它的指针-您需要编写:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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