QGraphicsView white flicker on refresh
-
Hello , I am trying to make an overlay using QGraphicsView but it keeps showing a white flicker from time to time and it's very annoying.Any ideea on what could be causing this ?
void mainwindow::renderImage() { static bool init = true; QScreen* screen = QGuiApplication::primaryScreen(); QRect screenGeometry = screen->geometry(); if (map.size()) { if (init) { scene.addItem(&item); // add an item to scene ( in this case the image) graphics->setScene(&scene); // add the scene to the graphicsview object graphics->show(); init = false; } if (previousMap != map) { this->setFixedSize(map.size(), map[0].size()); // resize window to same size as the map this->move(screenGeometry.width() - this->width(), 0); // move window so its top right graphics->setGeometry(0, 0, map.size(), map[0].size()); // only resize graphicsview if map is changed std::cout << "New map received\n"; } // set UI opacity QGraphicsOpacityEffect* effect = new QGraphicsOpacityEffect(this); effect->setOpacity(0.7); graphics->setGraphicsEffect(effect); //update item using new image item.setPixmap(QPixmap::fromImage(image)); } previousMap = map; }
class mainwindow : public QWidget { Q_OBJECT public: mainwindow(QWidget *parent = Q_NULLPTR); ~mainwindow(); public slots: void renderImage(); private: Ui::mainwindow ui; std::thread threadObj; QImage image; std::vector<std::vector<int>> map,previousMap; QGraphicsView* graphics; QGraphicsScene scene; QGraphicsPixmapItem item; void init(); void updateImage(); };
Image is updated from a separate thread and this function is called using a QTimer.
QTimer* timer = new QTimer(this); connect(timer, SIGNAL(timeout()), this, SLOT(renderImage())); timer->start(100);
The flickering has gotten alot worse while recording with OBS and i am using Qt 6.2 .
And for a visual example : Youtube Link -
I have managed to fix problem. The issue was that the other thread that was populating the QImage was not synchronized with the render thread and it was trying to render half populated images and stuff like that. I have added a simple check to know when QImage was completely populated and only then render the image and the flickering is completely gone.