Crash on QwtPlotZoomer in QT5 - qt

I am trying to use a QwtPlotZoomer in QT5, but I get a seg fault when its constructor runs.
class Qwt_widget : public QwtPlot
{
Q_OBJECT
public:
Qwt_widget(QWidget* parent = 0) :
QwtPlot(parent),
m_canvas()
{
QwtPlotZoomer zoomer(&m_canvas); // Crashes here
}
private:
QwtPlotCanvas m_canvas;
};
The above widget is added to a simple main window so that it will be created (full code at https://github.com/chrisburnham/Qwt_qt5_crash). Also here are the QWT lines that I have added to the QT creators default pro file:
INCLUDEPATH += /usr/include/qwt
LIBS += -lqwt-qt5
I am running on Ubuntu 18.04 and installed Qt5 with libqt5-default (5.9.5) and QWT with libqwt-qt5-dev (6.1.3). I have checked all my libraries with LDD and looked at the headers I am using and they are all coming from the packages that were installed with libqwt-qt5-dev. I have previously used QwtPlotZoomers in this way in Qt4 (with the Qt4 version of QWT) and as far as I can tell from the documentation it should still work here.

You need to assign the QwtPlotCanvas to the QwtPlot before you ask QwtPlotZoomer to work on it. Note that QwtPlot::setCanvas() takes ownership, so do not use a member variable for the canvas.
But since you are using a default QwtPlotCanvas and not some derived class you could probably just use the canvas QwtPlot comes with:
QwtPlotZoomer zoomer(this->canvas()); // Should not crash here

Related

libconfig used in Qt readFile failed

firstly,I have downloaded the libconfig x64-windows via vcpkg like this:
PS E:\vcpkg> .\vcpkg install libconfig:x64-windows
then,I added the lib to my Qt project with three steps
1.add the headfile
#include "libconfig.h++"
2.specify the lib path in the pro file(I have tried two ways)
LIBS += -L$PWD -llibconfig++
#LIBS += libconfig++.lib
3.in the build debug folder,I added the libconfig++.dll
after doing those steps,project was been built successfully without any error.then I added some code like these:
libconfig::Config cfg;
try{
cfg.readFile("D:\test.cfg");
}
catch(const libconfig::FileIOException&filex) {
Q_UNUSED(filex);
qDebug()<<"error";
return;
}
qDebug()<<"success";
and the output was always :"error",I have done the everything and the Qt window was displayed successfully.Once I called readFile api,it occured an error .
I have got the answer to my issue. The file path in Windows must have the double backslash.
So I must switch:
cfg.readFile("D:\test.cfg");
to
cfg.readFile("D:\\test.cfg");

QML_IMPORT_NAME seems to be an unknown variable, qml module not found

I'm trying to run this example with qt 5.15.1
When I declare QML_IMPORT_NAME, the variable seems to be unknown from qt (see the font's color
on the below screenshot) and when I import "com.mycompany.messaging" in my qml file, I have an error "QML module not found."
Edit:
After some investigations, the code runs as it should but I have this error in Qt Creator. If I want to edit qml file with the gui editor, I need to comment out all code related to the backend in text mode before otherwise, it fails to open the file.
What is the trick?
With this, I assumed I should have added
CONFIG += qmltypes
to .pro file. But since, I switched to cmake and did not find an equivalent, so I used the old method with:
qmlRegisterType<Person>("People", 1,0, "Person");
in main.cpp (see above link).
This is a known bug that is not fixed yet (https://bugreports.qt.io/browse/QTCREATORBUG-24987).
The reason for the error is that QtCreator requires the generated .qmltypes and metatypes.json files next to the application binary.
A workaround for this is to add the following to your pro file:
CONFIG(debug, debug|release) {
EXE_DIR = $${OUT_PWD}/debug
} else {
EXE_DIR = $${OUT_PWD}/release
}
CONFIG += file_copies
COPIES += qmltypes metatypes
qmltypes.files = $$files($${OUT_PWD}/$${TARGET}.qmltypes)
qmltypes.path = $${EXE_DIR}
metatypes.files = $$files($${OUT_PWD}/$${TARGET}_metatypes.json)
metatypes.path = $${EXE_DIR}

QT_STATIC_CONST does not name a type

I was using QT5.4.1 version with the QWT-6.1.2 library that I've installed but in compiling I receive the error "QT_STATIC_CONST does not name a type". After some researsh how to fix that, I found that I should install the QT version 5.5.
I have that already achieved but the error appears although!
I use Kubuntu by the way
What should I do ?
QT_STATIC_CONST’ does not name a type.
By defining QT_STATIC_CONST just before inserting Qwt file, compilation works.
- In Qt5.4 , QT_STATIC_CONST was removed from qglobal.h
- Qwt 6.1.1 uses this macro, but fixed it in Qwt 6.1.2
Here is the content that was removed from qglobal.h that created this issue. Adding this to your code prior to including the header file should resolve the issue.
/*
Workaround for static const members on MSVC++.
*/
#if defined(Q_CC_MSVC)
# define QT_STATIC_CONST static
# define QT_STATIC_CONST_IMPL
#else
# define QT_STATIC_CONST static const
# define QT_STATIC_CONST_IMPL const
#endif

How to get the DPI of the display screen in Qt

I need to get the DPI value of display in Qt. I am able to get it in Qt 5.0 using the following:
#include <QScreen>
......
......
QScreen *srn = QApplication::screens().at(0);
qreal dotsPerInch = (qreal)srn->logicalDotsPerInch();
But the same code throws a compilation error in Qt version 4.x. My project is developed in Qt version 4.x. So I need the equivalent of the above code in Qt version 4.x.
In Qt 4.8 this seem to do the trick:
#include <QDesktopWidget>
...
int dpiX = qApp->desktop()->logicalDpiX();
...
Another way of getting information in Qt5:
QWidget contain physicalDpiX, physicalDpiY, logicalDpiY et cetera...
(QWidget inherit them from QPaintDevice.)
(thought the OP said Qt4, but Qt5 is in development currently.)
Story:
I was facing the same issue.
After #code_fodder answer (though in-complete but still deserve credit).
mentioned of QPaintDevice contain those relevant methods.
after reading more over, in noticed,
QWidget inherits QObject and QPaintDevice and when i saw, that was it!.
I think this is a Qt5 addition. For Qt4 or older (I think its supported in 3... but can't remember) you can use the QPaintDevice to get similar information.
Here are the functions that will be useful to you depending what you need to do:
#include <QPaintDevice>
...
QPaintDevice paint;
int dpiX = paint.logicalDpiX();
int dpiY = paint.logicalDpiY();
int width = paint.widthMM();
int height = paint.heightMM();
Note: This is not an implementation, just example function calls.

How to subclass QGLWidget to use glew?

I'm struggling to get a subclass of QGLWidget compiled that uses glew. I'm running Qt 5.0.1 + MinGW 4.7 on Windows 7. Glew 1.7.0 has been compiled with said MinGW.
I have subclassed QGLWidget with UGGLWidget and included "GL/glew.h" before the QGLWidget includes. When compiling I get 200 Errors about:
'function' redeclared as different kind of symbol
conflicting declaration 'typedef'
So my Question:
How do I get a subclass of QGLWidget that uses glew compiled under Qt5?
Edit: I tried the same approach with another setup (Qt4.7 + MinGW4.4). It compiles and runs just fine. I have also tried using a more recent version of glew (Qt5.0.1 + MinGW4.7 + glew1.9.0) this doesn't compile either and gives the same compilation errors.
Here's a messy snippet of the compiler output:
> In file included from
> ..\..\..\..\..\..\Qt\Qt5.0.1\5.0.1\mingw47_32\include/QtGui/qopengl.h:55:0,
> from ..\..\..\..\..\..\Qt\Qt5.0.1\5.0.1\mingw47_32\include\QtOpenGL/qgl.h:47,
> from ..\..\..\..\..\..\Qt\Qt5.0.1\5.0.1\mingw47_32\include\QtOpenGL/QGLWidget:1,
> from src\UGGLWidget.h:4,
> from src\UGGLWidget.cpp:2: ..\..\..\..\..\..\Qt\Qt5.0.1\5.0.1\mingw47_32\include/GLES2/gl2.h:614:153:
> error: 'void __glewVertexAttribPointer(GLuint, GLint, GLenum,
> GLboolean, GLsizei, const GLvoid*)' redeclared as different kind of
> symbol In file included from src\UGGLWidget.cpp:1:0:
> ..\3rdPartyLibs\glew\include/GL/glew.h:13609:46: error: previous
> declaration of 'void (__attribute__((__stdcall__)) *
> __glewVertexAttribPointer)(GLuint, GLint, GLenum, GLboolean, GLsizei, const GLvoid*)'
Last but not least the header and source file of the subclass. It's really just empty for now.
Source:
//UGGLWidget.cpp
#include "GL/glew.h"
#include "UGGLWidget.h"
UGGLWidget::UGGLWidget(QWidget *parent) : QGLWidget(parent) { ... }
Header:
//UGGLWidget.h
#ifndef UGGLWIDGET_H
#define UGGLWIDGET_H
#include <QGLWidget>
class UGGLWidget : public QGLWidget
{
Q_OBJECT
public:
explicit UGGLWidget(QWidget *parent = 0);
};
#endif // UGGLWIDGET_H
Oh, and maybe relevant parts from the .pro file
QT += core gui opengl
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
LIBS += -L$$quote(../3rdPartyLibs/glew/lib/) -lglew32
LIBS += -lopengl32 -lglu32
INCLUDEPATH += $$quote(../3rdPartyLibs/glew/include)
The following fixed my problem, this goes everywhere, where I include glew:
#include "GL/glew.h"
#define QT_NO_OPENGL_ES_2
Another option: building Qt5.0.1 from source with -opengl desktop during the configure step.
For some reason QT_OPENGL_ES_2 is defined in my Qt5.0.1 installation. Looking through the inlcudes from QGLWidget.h I found the following in QtCore/qconfig.h
#if defined(QT_OPENGL_ES_2) && defined(QT_NO_OPENGL_ES_2)
# undef QT_OPENGL_ES_2
#elif !defined(QT_OPENGL_ES_2)
# define QT_OPENGL_ES_2
#endif
This leads to inclusion of GLES2/gl2.h which doesn't get along with glew. Knowing that the issue was related to GLES2 I came across a bug report and a QtProject post.
If I understand correctly, then the provided QtSDK installer for Qt5.0.1 has been built such that GLES is enabled.
Have a good day,
~Moritz
Note that QT 4.8RC release notes say:
Including QtOpenGL will not work in combination with GLEW, as QGLFunctions will undefine GLEW's defines.

Resources