Search code examples
c++qtqpixmapqapplication

QApplication::processEvents not working in Windows


I am working on a project that presents live data acquired in real-time using the QCustomPlot plug-in for Qt. The display has a black background color, and the multiple channels of data are colored differently. When taking a screenshot, we would like to make it printer-friendly, so the background is white and all data is black. I am thinking of a solution like this:

  1. Change all colors the way I want by manipulating the pointers for the graphical objects
  2. Grab the screenshot using QWidget::grab() to get a QPixmap
  3. Change all the colors back to normal

This did not work at first, because the system could not change the colors in time for the screenshot to be taken. So I used a QApplication::processEvents(), and it all worked on my Mac.

However, it does not work on a Windows 7 (which is required). Any ideas what to do?

Code:

QSting fileLocation = "...";
toggleColors(false); //function to toggle the colors
QApplication::processEvents();
QPixmap shot = grab();
toggleColors(true);
shot.save(fileLocation, "png");

Again. It works on Mac, but not Windows.

Update 1. Content of toggleColors include:

  if(enable)
    ui->plot->setBackground(QBrush(Qt::black));
  else
    ui->plot->setBackground(QBrush(Qt::white));
  ui->plot->repaint();

I also tried with ui->plot->update() instead.


Solution

  • It appears the problem lies with QCustomPlot. It was solved by performing a ui->plot->replot() which is specific to QCustomPlot and not QWidget.