SDL2 and OpenGL conflict - qt

I am new to graphical programming and following a simple tutorial on SDL.
Hereby, I am facing a problem which I cant find an answer to:
Whenever I open a Window, it just copies whatever is underneath it, and is not white like in the tutorial. I think this may be due to some hidden render buffer (?).
I am running the following code:
SDL_Init(SDL_INIT_EVERYTHING);
_window = SDL_CreateWindow("EvolutionEngine", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
_screenwidht, _screenheight, SDL_WINDOW_OPENGL);
SDL_Event evnt;
while(SDL_PollEvent(&evnt) == true)
{
switch (evnt.type)
{
case SDL_QUIT:
_gameState = GameState::EXIT;
break;
case SDL_MOUSEMOTION:
std::cout << "(" << evnt.motion.x << "," << evnt.motion.y << ")\n";
break;
}
}
(These are in different functions, but the only relevant code to the problem).
My qmake file looks like this:
QT += core
QT -= gui
TARGET = Evolution
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
CONFIG += link_pkgconfig
PKGCONFIG += sdl2
QMAKE_CXXFLAGS += -std=c++11
SOURCES += \
...
HEADERS += \
...
Has anybody got an idea on what might cause this an how I can fix it?

You are requesting a window with an opengl context form SDL. But you never draw anything to it (including clearing the buffer). I don't know why you would expect a white image from that. The contents of your frame buffer will just be completely undefined. And what you get is one of the many possibilities of how such undefined content might look.

To fix your mouse position printing, replace + with <<.

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}

Issue with space in qt pro file

I want to add macro (make constant) of the application name in qt pro file and use it later in code. For example:
QMAKE_TARGET_NAME = Foo Bar
DEFINES += APP_NAME=\\\"$$QMAKE_TARGET_NAME\\\"
When I use: qDebug() << APP_NAME it shows only "Foo". How to escape space in qt pro file? Thanks in advance for your help.
DEFINES += APP_NAME=$$shell_quote($$QMAKE_TARGET_NAME)
or if the " should be part of the define, then
DEFINES += APP_NAME=$$shell_quote(\"$$QMAKE_TARGET_NAME\")

Qt Creator and conditional build

In our project, we added some source and header files if a MACRO is defined. We do this like that, in the .pro file:
contains(DEFINES, MY_DEF) {
message("Support MY_DEF")
INCLUDEPATH += \
my_include_dir
SOURCES += \
source1.cpp \
source2.cpp
HEADERS += \
my_include_dir/header1.h \
my_include_dir/header2.h
FORMS += \
myform.ui
}
This works fine during the build. The files are not compiled if MY_DEF is not defined. MY_DEF is defined like that:
DEFINES += MY_DEF
Curiously, Qt Creator always display the files in the project tree, whereas MY_DEF is defined or not. If not defined, they are not used for the build, but they still are displayed and editable, searches can scan them, etc... Is it a bug of Qt Creator?
This is not a big issue, just a little annoying, because we don't know clearly if a file is part of the project or not.
It's intentional even. There's a special "accumulating" parsing mode to collect all files that are mentioned in .pro files (essentially the same that's used to collect "translatable strings") for display in the project tree. Otherwise things like "Replace in all files in a project" would yield different results depending on the platform or the context it is run in. [And it's not half of qmake that's included, but close to all of it...]
This seems to be an issue with QtCreator and how it reads the .pro files - it doesn't seem to actually fully parse the files, opting instead to just pick out certain bits. I've got the same issue with files that are only included on one platform or another - in QtCreator, they always show up.
I expect that the reason is either that they don't want to re-implement half of qmake just to get the file lists, OR that there are situations where trying to parse it 'correctly' would get the wrong answer, and they've chosen to be predictably wrong instead of randomly wrong.
In addition to the conditional includes in QMake, I add #ifdef around such conditional source code. That way I also see it visually drop out of compilation when the conditions are not met. It's not as good as having the files drop out entirely from the project tree, but it's better than allowing them to still appear like they are part of the build when editing them if they are not applicable.
Just for the sake of completeness and answer correctness. Probably someone else needs this example of root .pro file with conditional source tree:
TEMPLATE = subdirs
SUBDIRS = device
CONFIG -= debug_and_release
_SANDBOX_DIR = $$dirname(PWD)
_PLAYER_PRO = $${_SANDBOX_DIR}/player/player.pro
SUBDIRS = device
device.subdir = $${_SANDBOX_DIR}/proxy/libproxy
contains(QMAKE_PLATFORM, android) {
unset(_PLAYER_PRO)
} else {
SUBDIRS += player
player.file = $${_PLAYER_PRO}
player.depends = device
}
SUBDIRS += app
app.subdir = $${_SANDBOX_DIR}/display/display
app.depends = device
contains(SUBDIRS, player) {
app.depends += player
}

Qt auto software version?

Does Qt maintain any sort of versioning information about your program like .NET does? Like the build number? Or does it provide an easy way to access the SVN revision?
No.
But if you're using qmake then you can set compiler flags in the build system based on the results of arbitrary commands, which might be usable to do what you want.
For example, if you were using git, you could do something like this in your .pro file:
REVISION = $$system(git rev-parse HEAD)
DEFINES += APP_REVISION=$$REVISION
That would give you an APP_REVISION macro when compiling your program, which you could use like this:
// stringize macro
#define _STR(X) #X
#define STR(X) _STR(X)
QTextStream(cout) << "MyApp revision " STR(APP_REVISION) << endl;

Resources