Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QGraphicsView white flicker on refresh
Forum Updated to NodeBB v4.3 + New Features

QGraphicsView white flicker on refresh

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 1 Posters 515 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Codex231
    wrote on last edited by Codex231
    #1

    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

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Codex231
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved