QCustomPlot Discussion and Comments

Line appearance changed from v2.0.1 to v2.1.0 ("weird dashed line")Return to overview

I have a lab application which plots some rather simple polylines in real time.

I was using v2.0.1 for some time now, without (many) problems, so far so good. Now, I tried to "upgrade" the QCP to the newest version v2.1.0, and have noticed that now my lines look different - they are shown as some kind of weird dashed/dotted lines, instead of simple solid lines:

Before ("normal look"), using v2.0.1:
https://i.ibb.co/HFj2g5s/qcp201.jpg

After ("weird dashed/dotted lines"), using v2.1.0:
https://i.ibb.co/HNRW2m5/qcp210.jpg

Note: This happens only for the lines with width set to 0 or 1, and only when using v2.1.0. If I force width of 2, 3 or more, lines appear as solid lines also in v2.1.0. not as these "dashed lines".

Lines used are simple lines:

	
	pen.setCapStyle( Qt::FlatCap );
	pen.setWidth( 1 );
	pen.setStyle( Qt::SolidLine );
	pen.setBrush( Qt::SolidPattern );

There is no OpenGL active (target lab PC has problems when it's turned on, for some reason), antialiasing is also turned off, in hope it makes drawing faster:

	QFont font;
	setNotAntialiasedElements( QCP::aeAll );
	font.setStyleStrategy( QFont::NoAntialias );
	xAxis->setTickLabelFont( font );
	yAxis->setTickLabelFont( font );
	legend->setFont( font );

	// OpenGL is not turned on, i.e. should be off for the lab PC
	//setOpenGl( true );  
                
	setNoAntialiasingOnDrag( true );

Everything else is practically at "default" values, as constructed.

I suspect that there is some kind of optimization at play here for v2.1.0 that was not turned on by default in 2.0.1, but I'm not sure, of course (?)

So, what am I doing wrong, i.e. what might be the problem here?


...just to add to the original post:

The only difference are the qcustomplot.cpp and .h files from 2.0.1 and 2.1.0 versions, otherwise it's the same application, no other differences whatsoever, other than recompiling.


After some searching, I was able to find the culprit for this behaviour.

It's a part of the drawPolyline template function, added in 2.1.0:

template <class DataType>
void QCPAbstractPlottable1D<DataType>::drawPolyline(QCPPainter *painter, const QVector<QPointF> &lineData) const
{
  // if drawing lines in plot (instead of PDF), reduce 1px lines to cosmetic, because at least in
  // Qt6 drawing of "1px" width lines is much slower even though it has same appearance apart from
  // High-DPI. In High-DPI cases people must set a pen width slightly larger than 1.0 to get
  // correct DPI scaling of width, but of course with performance penalty.
  if (!painter->modes().testFlag(QCPPainter::pmVectorized) &&
      qFuzzyCompare(painter->pen().widthF(), 1.0))
  {
    QPen newPen = painter->pen();
    newPen.setWidth(0);
    painter->setPen(newPen);
  }

As it seems, forcing width to 0 at this point, results in those "weird dotted/dashed lines" in my original post.

Now, I was intrigued by this, and, after some more searching, I think I've found out what is happening, although I'm not really sure why.

Apparently, setting cosmetic pen width (i.e. pen width of 0) does not go well with high-DPI Qt environment variable setting.

I am using this:


	// for high DPI screens
	qputenv("QT_AUTO_SCREEN_SCALE_FACTOR", "1");
	

Background:
I am using a somewhat weird combination of high-DPI and non-high-DPI screen resolutions for development and on the target lab PC where app is deployed and running.

I'm doing the development on a laptop with a 4K screen, but also using a HD 1920x1080 monitor in concurrently on the same laptop (actually using that non-4K monitor as the "main").

Since I was getting all kinds of problems with my app when switching from 4K to HD screen (both on my dev laptop/monitor and on the target lab PC which also has a non-4K monitor) - problems with sizes/scaling of widgets, with alignment of buttons and panels, min/max size boundaries for some panels, etc.

I found an easy solution to all those scaling problems in setting that env variable featured above to 1, and had no problem with DPI switching or various resolutions since.

However...

It turns out that, when QT_AUTO_SCREEN_SCALE_FACTOR is 1, and pen width is 0 (cosmetic), lines get that weird "dashed" look. Once I take out setting the environment variable (which is set at the very beginning of my main() ), polylines of width 0 on QCP Graph look as they should be looking, i.e. as solid, non-dashed lines.


Unfortunately, at the moment I have no idea why this happens and I cannot simply comment that env variable out as a solution, since then all kinds of widget alignment and scaling problems rear their ugly head again, when switching from low- to high-DPI screens and back.

I guess the quick-and-dirty solution will be to comment out that setting of pen width to 0 in drawPolyline template in qcustomplot.h, and go with that, at least for now (I have deadlines to meet and cannot afford to spend too much time on solving this properly at the moment).

But I hope this might help someone else at some point in future :)


Note:
I'm currently using Qt 5.12.0 (i.e. not Qt 6.x), so it might be that his happen only when using Qt 5.x. Once I switch to Qt 6.x I'll try to remember and check this part out again.