QGraphicsView changes on scene does not reflect
-
I am using QGraphicsView for showing images. First my
MainSlotruns and it does its job, But i have 2 other slots according to 2 button and they re not working.
HEADER:class MyClass{ private: QGraphicsScene *scene = new QGraphicsScene; QImage qimg; QImage scaledQimg; QPixmap pixmap; cv::Mat img; }SOURCE:
void MainSlot(){ img = cv::Mat(...); qimg = QImage(img.data, img.cols, img.rows, img.step, QImage::Format_Grayscale8); scaledQimg = qimg.scaled(ui->graphicsView->width() - 10, ui->graphicsView->height() - 10, Qt::KeepAspectRatio); pixmap = QPixmap::fromImage(scaledQimg); scene->addPixmap(pixmap); ui->graphicsView->setScene(scene); } void originalSize() { pixmap = QPixmap::fromImage(qimg); scene->update(); ui->graphicsView->update(); } void ScaledSize() { pixmap = QPixmap::fromImage(scaledQimg); scene->update(); ui->graphicsView->update(); }Whats wrong with my code?
-
I am using QGraphicsView for showing images. First my
MainSlotruns and it does its job, But i have 2 other slots according to 2 button and they re not working.
HEADER:class MyClass{ private: QGraphicsScene *scene = new QGraphicsScene; QImage qimg; QImage scaledQimg; QPixmap pixmap; cv::Mat img; }SOURCE:
void MainSlot(){ img = cv::Mat(...); qimg = QImage(img.data, img.cols, img.rows, img.step, QImage::Format_Grayscale8); scaledQimg = qimg.scaled(ui->graphicsView->width() - 10, ui->graphicsView->height() - 10, Qt::KeepAspectRatio); pixmap = QPixmap::fromImage(scaledQimg); scene->addPixmap(pixmap); ui->graphicsView->setScene(scene); } void originalSize() { pixmap = QPixmap::fromImage(qimg); scene->update(); ui->graphicsView->update(); } void ScaledSize() { pixmap = QPixmap::fromImage(scaledQimg); scene->update(); ui->graphicsView->update(); }Whats wrong with my code?
@masa4 said in QGraphicsView changes on scene does not reflect:
and they re not working.
It really helps if you say what you mean by this. Are we supposed to guess?
If my "guess" is "when I change
pixmapin the 2 functions shown the pixmap shown on the graphics scene does not change", that would be becauseaddPixmap(pixmap)takes a copy ofpixmap, it does not make the scene have a dynamic reference topixmap. Is that what you are asking about? -
@masa4 said in QGraphicsView changes on scene does not reflect:
and they re not working.
It really helps if you say what you mean by this. Are we supposed to guess?
If my "guess" is "when I change
pixmapin the 2 functions shown the pixmap shown on the graphics scene does not change", that would be becauseaddPixmap(pixmap)takes a copy ofpixmap, it does not make the scene have a dynamic reference topixmap. Is that what you are asking about?@JonB Yeah basically nothing happens when i click these 2 buttons. So you mean I should make pixmap pointer? For example:
void ScaledSize() { pixmap = QPixmap::fromImage(scaledQimg); scene->update(); ui->graphicsView->update(); }here i am creating a pixmap with
QPixmap::fromImage(scaledQimg)and assigning it to mypixmap. Isnt it sufficient? -
@JonB Yeah basically nothing happens when i click these 2 buttons. So you mean I should make pixmap pointer? For example:
void ScaledSize() { pixmap = QPixmap::fromImage(scaledQimg); scene->update(); ui->graphicsView->update(); }here i am creating a pixmap with
QPixmap::fromImage(scaledQimg)and assigning it to mypixmap. Isnt it sufficient?@masa4 said in QGraphicsView changes on scene does not reflect:
assigning it to my pixmap. Isnt it sufficient?
Of course not.
Why should anything change in the scene if you update pixmap variable? -
@JonB Yeah basically nothing happens when i click these 2 buttons. So you mean I should make pixmap pointer? For example:
void ScaledSize() { pixmap = QPixmap::fromImage(scaledQimg); scene->update(); ui->graphicsView->update(); }here i am creating a pixmap with
QPixmap::fromImage(scaledQimg)and assigning it to mypixmap. Isnt it sufficient? -
@masa4 said in QGraphicsView changes on scene does not reflect:
assigning it to my pixmap. Isnt it sufficient?
Of course not.
Why should anything change in the scene if you update pixmap variable? -
@masa4 You need to understand that your pixmap variable has no connection to your scene. If you call scene->addPixmap(pixmap); your pixmap is copied into the scene, this was already pointed out by @JonB . If you change pixmap variable later it has zero influence on the scene. You need to update the pixmap item in the scene...
-
@masa4
You need to alter theQPixmapwhich was created on the scene viascene->addPixmap().
Or, possibly, depending on your desires, remove it and put a new one instead.@JonB yeah now worker with this:
void originalSize() { pixmap = QPixmap::fromImage(qimg); scene->clear(); scene->addPixmap(pixmap); scene->update(); ui->graphicsView->update(); }But original size image not sitting at the center of graphicsview. You know any trick for centerize it?
-
@masa4 You need to understand that your pixmap variable has no connection to your scene. If you call scene->addPixmap(pixmap); your pixmap is copied into the scene, this was already pointed out by @JonB . If you change pixmap variable later it has zero influence on the scene. You need to update the pixmap item in the scene...
-
@JonB yeah now worker with this:
void originalSize() { pixmap = QPixmap::fromImage(qimg); scene->clear(); scene->addPixmap(pixmap); scene->update(); ui->graphicsView->update(); }But original size image not sitting at the center of graphicsview. You know any trick for centerize it?
@masa4 Now you are adding a new pixmap item all the time. I doubt this is what you want. What you should do instead: scene->addPixmap(pixmap); returns a pointer to QGraphicsPixmapItem, store this pointer as member variable and call https://doc.qt.io/qt-6/qgraphicspixmapitem.html#setPixmap on it every time you need to update pixmap.
-
@JonB yeah now worker with this:
void originalSize() { pixmap = QPixmap::fromImage(qimg); scene->clear(); scene->addPixmap(pixmap); scene->update(); ui->graphicsView->update(); }But original size image not sitting at the center of graphicsview. You know any trick for centerize it?
@masa4 said in QGraphicsView changes on scene does not reflect:
But original size image not sitting at the center of graphicsview. You know any trick for centerize it?
-
@masa4 Now you are adding a new pixmap item all the time. I doubt this is what you want. What you should do instead: scene->addPixmap(pixmap); returns a pointer to QGraphicsPixmapItem, store this pointer as member variable and call https://doc.qt.io/qt-6/qgraphicspixmapitem.html#setPixmap on it every time you need to update pixmap.
@jsulm You mean this right:
header:... QGraphicsPixmapItem *pix;source:
void MainSlot(){ ... pix = scene->addPixmap(pixmap); ui->graphicsView->setScene(scene); } void originalSize() { pixmap = QPixmap::fromImage(qimg); pix->setPixmap(pixmap); ui->graphicsView->centerOn(ui->graphicsView->x()/2,ui->graphicsView->y()/2); //doesnt work } void ScaledSize() { pixmap = QPixmap::fromImage(scaledQimg); pix->setPixmap(pixmap); }In this way it still works. centerize doesnt work by the way @JonB
-
@jsulm You mean this right:
header:... QGraphicsPixmapItem *pix;source:
void MainSlot(){ ... pix = scene->addPixmap(pixmap); ui->graphicsView->setScene(scene); } void originalSize() { pixmap = QPixmap::fromImage(qimg); pix->setPixmap(pixmap); ui->graphicsView->centerOn(ui->graphicsView->x()/2,ui->graphicsView->y()/2); //doesnt work } void ScaledSize() { pixmap = QPixmap::fromImage(scaledQimg); pix->setPixmap(pixmap); }In this way it still works. centerize doesnt work by the way @JonB
@masa4 said in QGraphicsView changes on scene does not reflect:
In this way it still works. centerize doesnt work by the way @JonB
I never had any problem with it. Are you claiming you want to report it fails to work??
ui->graphicsView->centerOn(ui->graphicsView->x()/2,ui->graphicsView->y()/2); //doesnt workIn this way it still works
What are we expected to understand from the line saying "doesn't work" and one saying "still works"?
For your actual code (a) it's not what I suggested to use and (b) I don't know what you expect it to do.
-
@masa4 said in QGraphicsView changes on scene does not reflect:
In this way it still works. centerize doesnt work by the way @JonB
I never had any problem with it. Are you claiming you want to report it fails to work??
ui->graphicsView->centerOn(ui->graphicsView->x()/2,ui->graphicsView->y()/2); //doesnt workIn this way it still works
What are we expected to understand from the line saying "doesn't work" and one saying "still works"?
For your actual code (a) it's not what I suggested to use and (b) I don't know what you expect it to do.
@JonB Haha sorry for poor grammar. I mean my code work, functions work after switching to QGraphicsPixmapItem.
And i added
ui->graphicsView->centerOn(ui->graphicsView->x()/2,ui->graphicsView->y()/2); //doesnt workthis line for centering the image in graphics view. But image still not centered.
-
@masa4 said in QGraphicsView changes on scene does not reflect:
In this way it still works. centerize doesnt work by the way @JonB
I never had any problem with it. Are you claiming you want to report it fails to work??
ui->graphicsView->centerOn(ui->graphicsView->x()/2,ui->graphicsView->y()/2); //doesnt workIn this way it still works
What are we expected to understand from the line saying "doesn't work" and one saying "still works"?
For your actual code (a) it's not what I suggested to use and (b) I don't know what you expect it to do.
@masa4 said in QGraphicsView changes on scene does not reflect:
ui->graphicsView->centerOn(ui->graphicsView->x()/2,ui->graphicsView->y()/2);
But image still not centered.@JonB said in QGraphicsView changes on scene does not reflect:
For your actual code (a) it's not what I suggested to use and (b) I don't know what you expect it to do.
I already wrote the above previously.