How to get the DPI of the display screen in Qt - 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.

Related

Crash on QwtPlotZoomer in QT5

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

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.

How to use QT and GNU Autotools by using AutoTroll?

I would like to use Autotools and QT4 together. For this i would like to use AutoTroll.
As the Website says, it should be very easy to use it. Unfortunately i was not able to do so.
I have a working Hello World example:
This is how my configure.ac looks like:
AC_INIT([Hello], [0.1], [bug-report#hello.example.com], [hello], [http://hello.example.com/])
AC_PREREQ([2.59])
AM_INIT_AUTOMAKE([1.10 no-define foreign])
AC_PROG_CXX
AC_PROG_CC
AT_WITH_QT
AC_CONFIG_FILES([Makefile])
AC_OUTPUT
My Makefile.am:
AUTOMAKE_OPTIONS = subdir-objects
ACLOCAL_AMFLAGS = ${ACLOCAL_FLAGS}
bin_PROGRAMS = hello
hello_SOURCES = src/*.cpp
My simple main.cpp with a QT include:
#include <iostream>
#include <QApplication> \\Added this to test if Autotools works correctly
int main()
{
std::cout << "Hello Worlasdasdsadd!" << std::endl;
return 0;
}
Can someone please help mit with this? An short Step by step would be very nice.
Thank you in advance.
Edit:
I don't know how to integrate this m4 makro (Autotroll) in my configuration. This is why i cannot provide any "Errors" or something else. The of the website does not help me to understand what to do.
I won't recommend you to mix QT and autotools. For QT projects is most recommended to use cmake/qmake. When you get any problems it would be easier to find answers, etc.
Furthermore, Autotroll seems unmaintained since 2008 and not tested with QT >= 4.4+, and it has rained a lot (really a lot, QML and QT 5.0 as examples) since then:
AutoTroll has been tested successfully with Qt 4.0+, Qt 4.1+, Qt 4.2+ and Qt 4.3+
If you just want to learn autotools, gtk/gnome, gnu-tools or just any c++/c/whatever-language project would make it easier for you. Find projects using autotools and learn what they need and what they use.
Another recommendation is that you look for presentations and talks explaining the autotools basis and tools flows.
All I can say is that Autotools is a big world, be patient.

What API does Qt use for interacting with the Windows clipboard?

I've been browsing the Qt source code trying to find the actual system calls but it seems Qt doesn't use the Windows API documented on MSDN. For example grepping the source for "GetClipboardData" returns results in two files:
qclipboard_win.cpp:
#if defined(Q_OS_WINCE)
...
HANDLE clipData = GetClipboardData(CF_TEXT)
qaxserverbase.cpp:
STDMETHOD(GetClipboardData)(DWORD dwReserved, IDataObject** ppDataObject);
...
HRESULT WINAPI QAxServerBase::GetClipboardData(DWORD, IDataObject**)
{
return E_NOTIMPL;
}
and "SetClipboardData":
qclipboard_win.cpp:
#if defined(Q_OS_WINCE)
...
result = SetClipboardData(CF_UNICODETEXT, wcsdup(reinterpret_cast<const wchar_t *> (data->text().utf16()))) != NULL;
Neither of which seems useful, since they're being declared for Win CE/Mobile.
My Qt (4.8.1) uses OleSetClipboard and OleGetClipboard. The lines you got to are never reached in regular windows, as only in case of #if defined(Q_OS_WINCE) Qt uses #define OleSetClipboard QtCeSetClipboard and #define OleGetClipboard QtCeGetClipboard, and otherwise uses system-provided versions of those functions.
It was a little dificult to see this #if defined though, so you are excused ;)
It is so at least on my Qt version. If you are talking about qt, and especially about it's internals, you should menstion the version, right?

Getting values from pro files in Qt

I am using Qt 4.5 in Windows XP. My pro file has the variable VERSION = 1.0. Now i need to read this variable and get its value (1.0) from the source code. So that I don't have to maintain another variable for version inside my source code. I just read from the pro file and update it. So that the value remains consistent all over my project. Is it possible? Any pointers regarding this are welcome..
Use somethings like this:
DEFINES += VERSION=\\\"$$VERSION\\\"
This will define a macro that you can use in C source code. Get rid of the backslashes and quotes if you want a number, not a string.
I'll elaborate on this a bit.
In the YourApp.pro:
VERSION = 0.0.0.1
DEFINES += APP_VERSION=\\\"$$VERSION\\\"
In the main.cpp:
#include <QApplication>
QCoreApplication::setApplicationVersion(QString(APP_VERSION));
Wherever else in your sources, e.g. in the imaginary controller.cpp:
#include <QApplication>
QString yourAppVersion = QCoreApplication::applicationVersion();
somehow, when I tried qDebug() << QString(APP_VERSION); in a class.cpp not in main.cpp. has an error "C2065" APP_VERSION: undeclared identifier". but when I tried in main.cpp that worked.

Resources