1

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.

casparjespersen
  • 3,460
  • 5
  • 38
  • 63
  • Have you tried using [`QCustomPlot::savePng`](http://www.qcustomplot.com/documentation/classQCustomPlot.html#a7636261aff1f6d25c9da749ece3fc8b8)? I don't know how it actually works internally, but it seems to repaint everything, so you might not even need to call `QApplication::processEvents`. Worth a shot. – thuga Aug 05 '15 at 12:52

2 Answers2

0

I am not sure what is the problem on Windows specifically, but I recommend you calling QWidget::update() on the given widget. That forces the next update to re-render itself.

On the other hand, I'm not sure why toggleColors() didn't somehow cause that to happen.

Also, ensure that QWidget::setUpdatesEnabled(bool) wasn't set to "false."

macetw
  • 1,640
  • 1
  • 17
  • 26
  • I assume you mean `repaint` instead of `redraw` :-) Please take a look at update 1, I have included some of the content from toggleColors. – casparjespersen Aug 05 '15 at 12:30
  • SaZ, I do not see documentation of the function redraw(), not in QT4 or QT5. Perhaps you meant repaint(), which (I agree) could also be a solution. – macetw Aug 05 '15 at 12:31
  • Consider also using QWidget::render() to get what you're looking for. Note that your QPaintDevice* you render to is to a QPixmap that you create. – macetw Aug 05 '15 at 12:34
  • It appears the problem lies with QCustomPlot. It was solved by performing a `ui->plot->replot()` which is specific to QCustomPlot and not QWidget. – casparjespersen Aug 05 '15 at 12:53
  • Sure, I mean `repaint`. Anyway, it is not necessary to do, because `grab` method will call `paintEvent` directly. – Dmitry Sazonov Aug 05 '15 at 13:02
0

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

casparjespersen
  • 3,460
  • 5
  • 38
  • 63