QImage img(data_image, Nc, Nl, Nc, QImage::Format_Indexed8); QVector<QRgb> color_table; for(int i = 0; i < 256; ++i) color_table.append(qRgb(i,i,i)); img.setColorTable(color_table); QImage optimal_img = img.convertToFormat(QImage::Format_RGB32); QGraphicsScene* scene = new QGraphicsScene(this); QGraphicsPixmapItem item(QPixmap::fromImage(optimal_img)); scene->addItem(&item); ui->graphicsView->setScene(scene); 

I'm trying to display the image in this way, but the QGraphicScene is empty. Tried to display text ( QGraphicsScene::addText ) - it is displayed. The picture is not empty, I checked. What could be the problem?

  • And how was the picture checked? Have you tried, for example, to bring it to QLabel ? - alexis031182
  • @ alexis031182 saved to file. just tried in QLabel - rendered - bronstein87

1 answer 1

Declare item in your code as a pointer:

 QGraphicsPixmapItem *item = new QGraphicsPixmapItem(QPixmap::fromImage(optimal_img)); scene->addItem(item); 

After exiting the function that contains this code, the object is destroyed and the pointer becomes invalid. This can be checked by saving the contents of graphicsView to the file immediately after adding the image:

 ui->graphicsView->grab().save(QApplication::applicationDirPath()+"/tmp.png"); 
  • Thank you, now everything is displayed. - bronstein87
  • @ bronstein87, if the answer helped you, accept it by putting a green check under the arrows with voices. - alexis031182