No update of my QGraphicsPixmapItem
-
I am making an application to display several picture with the ability to zoom in them
I am using a QMainWindow in which I can open several picture so using QMdiSubWindow
Each MdiSubWindow is made of an object ImageView which inherits QGraphicsView
Each ImageView contains a scene ImageScene which inherits QGraphicsScene
And in each scene there is only one object GraphicsPixmapItem which inherits QGraphicsPixmapItem to display the image in which I want to zoom inI make the zoom thanks to a wheelEvent on the mouse which is intercepted in the ImageView object. With that I change the scale of the GraphicsPixmapItem which is centered on the coordinate (0, 0). I don't forget to adapt the boundaries of my ImageScene.
If I open a "little" image (let's said 1024x768 in 24 bits) everything is OK, I can zoom as much as I want in my picture.
But if I open a bigger image (6000x4000 in 24 bits) when I try to zoom in it, I don't have any more update of my picture.
When I zoom, I see the call to ImageView::paintEvent() but there is no call to GraphicsPixmapItem::paint() like if something said that there was no used to redraw the pixmap.
I have checked to the rect() and region() of the ImageView and they seem to be correct. The boundary change of the ImageScene are also correct. I tried to call an update on a wheel event on the pixmap item but still without any success.
I also tried a prepareGeometryChange() on my GraphicsPixmapItem but still nothing. I have a call to ImageView::paintEvent() but it doesn't call for GraphicsPixmapItem::paint().Does anyone have any idea what could prevent my object pixmap to be updated ? Apparently it is linked to the size of the picture but then what is the problem ? memory size ? time consumption ?
I am using Qt 5.14.0 under Linux 4.19.0
-
I have a similar problem like wotan. I need always to write QApplication::process events();
after i changed / set / loaded a pixmap. without nothing happens.I have this on both platforms Windows 10 and on MacOSX 10.14.6
Could this be a bug? I am using Qt 5.15.0
-
Hi to all experts,
i have googeln a whole day and tried out different solutions to get a graphics scene updated.
-
Suggestion: removeold pixmap and add new one .
scene->removeItem(&pixmap);
scene->addItem(&pixmap); -
Suggestion: Do an update on the whole scene.
scene->update(); -
Suggestion: Do an update on the element which has changed.
pixmap.update(ui->graphicsView->rect());
None of this solution has been working.
Pls can we get help on this problem - it is mandatory for us to actualize an element in graphics view?
-
-
Re: No update of my QGraphicsPixmapItem
ademmler, is your problem also linked to the size of the pixmap ?
Because I am thinking of an extraction of the part of the pixmap which is supposed to be seen in the view. That way I will have a littler pixmap (in size) and put it in the scene. That might solve my problem ... -
Hi SGaist,
I am still trying to solve the issue with my measurement loop and painting to graphics scene.
But I get a "signal/slot" connect error. I can't see why it should not work.QObject::connect: No such signal MainWindow::&MainWindow::signalPaintColor(int i) in mainwindow.cpp:52
QObject::connect: (sender name: 'MainWindow')
QObject::connect: (receiver name: 'MainWindow')This is my modified minimal code example:
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QGraphicsScene> #include <QGraphicsPixmapItem> #include <QThread> #include <QDebug> //QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } //QT_END_NAMESPACE class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); signals: void signalPaintColor(int i); private slots: void on_startButton_released(); void slotPaintColor(int i); private: Ui::MainWindow *ui; QGraphicsScene *scene; QGraphicsPixmapItem pixmap; }; #endif // MAINWINDOW_H
#include "mainwindow.h" #include "ui_mainwindow.h" using namespace std; struct rgb { int R; int G; int B; char cn [10]; } colors[5]; MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); scene = new QGraphicsScene(this); ui->graphicsView->setScene(scene); scene->addItem(&pixmap); colors[0] = { 255, 255, 255, "white" }; colors[1] = { 255, 0, 0, "red" }; colors[2] = { 0, 255, 0, "green" }; colors[3] = { 0, 0, 255, "blue" }; colors[4] = { 0, 0, 0, "black" }; } MainWindow::~MainWindow() { delete ui; } void MainWindow::slotPaintColor(int i){ QPixmap pix(ui->graphicsView->size()); pix.fill(QColor(colors[i].R, colors[i].G, colors[i].B)); pixmap.setPixmap(pix); ui->statusbar->showMessage("Color: " + QString(colors[i].cn), 0); QApplication::processEvents(); } void MainWindow::on_startButton_released() { ui->statusbar->showMessage("Start loop ...", 0); connect(this, SIGNAL(signalPaintColor(int i)), this, SLOT(slotPaintColor(int i))); for( int i = 0; i < 5; i++ ) { emit signalPaintColor(i); } }
-
@ademmler said in No update of my QGraphicsPixmapItem:
QObject::connect: No such signal MainWindow::&MainWindow::signalPaintColor(int i) in mainwindow.cpp:52
If you insist on using old-style signal/slot syntax, this is telling you you have got it wrong. Where did you get/copy
connect(this, SIGNAL(signalPaintColor(int i)), this, SLOT(slotPaintColor(int i)));
from? Please copy examples, should be:connect(this, SIGNAL(signalPaintColor(int)), this, SLOT(slotPaintColor(int)));
Do yourself a favor and switch over now to new-style for everything:
connect(this, &MainWindow::signalPaintColor, this, &MainWindow::slotPaintColor);
Isn't that neater? :)
Separately: why are you doing
connect()
signals/slots insideon_startButton_released()
? That means a new, additional connection will be made each time the button is pressed --- putqDebug()
s inslotPaintColor()
to see.connect()
s should be done during initialization only. -
@JonB
Dear Jon thx for this precise input to me.If you insist on using old-style signal/slot syntax, this is telling you you have got it wrong.
I would not insist here ...
Where did you get/copy
connect(this, SIGNAL(signalPaintColor(int i)), this, SLOT(slotPaintColor(int i)));
from?Honestly I do not remember.
Please copy examples, should be:
connect(this, SIGNAL(signalPaintColor(int)), this, SLOT(slotPaintColor(int)));
Do yourself a favor and switch over now to new-style for everything:
connect(this, &MainWindow::signalPaintColor, this, &MainWindow::slotPaintColor);
Isn't that neater? :)
Ofcourse it is neater. Doesn't the new syntax need to know about variables to be passed?
Separately: why are you doing
connect()
signals/slots insideon_startButton_released()
? That means a new, additional connection will be made each time the button is pressed --- putqDebug()
s inslotPaintColor()
to see.connect()
s should be done during initialization only.Its a leftover from trying things out - in this minimal code ...
Ofcourse I will move it back toMainWindow::MainWindow(QWidget *parent)
.What I am still wondering is how to get the loop from the measurement device together with painting the colors. Paint -> Measure -> Paint -> Measure ...
Would I need a "signal" telling me, that the color has changed on the monitor?thx for your help - it is most appreciated!
-
I changed the minimal example to this using two signals and two slots.
Please note " QThread::sleep(1);" is used only here - for simulating the measurement routine and the device - which is a black box routine eating some time ...The problem is still that with out
QApplication::processEvents();
inMainWindow::slotPaintColor(int i)
the color does not change.How can I achieve a loop which first changes the color and than - when this color is shown on screen (not before) - the measurement takes place ... and again until the colorlist is at the end ... all without using
QApplication::processEvents();
#include "mainwindow.h" #include "ui_mainwindow.h" using namespace std; struct rgb { int R; int G; int B; char cn [10]; } colors[5]; MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); connect(this, &MainWindow::signalPaintColor, this, &MainWindow::slotPaintColor); connect(this, &MainWindow::signalMeasureColor, this, &MainWindow::slotMeasureColor); scene = new QGraphicsScene(this); ui->graphicsView->setScene(scene); scene->addItem(&pixmap); colors[0] = { 255, 255, 255, "white" }; colors[1] = { 255, 0, 0, "red" }; colors[2] = { 0, 255, 0, "green" }; colors[3] = { 0, 0, 255, "blue" }; colors[4] = { 0, 0, 0, "black" }; } MainWindow::~MainWindow() { delete ui; } void MainWindow::slotPaintColor(int i){ QPixmap pix(ui->graphicsView->size()); pix.fill(QColor(colors[i].R, colors[i].G, colors[i].B)); pixmap.setPixmap(pix); ui->statusbar->showMessage("Color: " + QString(colors[i].cn), 0); QApplication::processEvents(); emit signalMeasureColor(); } void MainWindow::slotMeasureColor() { //Sleep is for simulation of external task here ... QThread::sleep(1); } void MainWindow::on_startButton_released() { ui->statusbar->showMessage("Start loop ...", 0); for( int i = 0; i < 5; i++ ) { emit signalPaintColor(i); } }
-
Well: do not block the event loop.
Since you are doing measures "on demand", use the worker object approach. Encapsulate the measurement part in its own class. Move the instance of that class to a dedicated QThread. And then use signals and slots to trigger the measure and pass back the data. Doing so you will decouple the controlling part and the UI.
-
@SGaist thx for the fast response.
Is there a "Qt standard Example" using this technique ?
That I can have a look how his works.Is this what you are talking about? https://wiki.qt.io/QThreads_general_usage
Am I right: This examples uses still old signal/slots syntax?
-
The wiki page is a bit outdated.
Check the QThread documentation. It has vastly improved and shows both technique with modern syntax.