CodeBlocks Qt HelloWorld.exe has stopped working (C++) - c++

I'm a beginner "programmer", and I'm using quotation marks because I'm THAT green.
Windows 7 64-bit
Code::Blocks 13.12
OpenCV 2.4.10
Qt 4.8.5
I've been thrown into single-handedly creating a pretty big (for me) piece of software that uses OpenCV and mingw to track movement of a few different markers and calculate (and accurately guess...) a lot of things. I'm almost done, but and I have to incorporate some GUI elements into it, most importantly a dialog window with which you can look for files (came up yesterday). So I've tried setting up Qt with Code::Blocks and creating a basic Hello World app. I've set up the env Path variable, I've pointed the linker and compiler search directories where they should be. It still doesn't work.
#include <QApplication>
#include <QFont>
#include <QPushButton>
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
QPushButton quit("Quit");
quit.resize(75, 30);
quit.setFont(QFont("Times", 18, QFont::Bold));
QObject::connect(&quit, SIGNAL(clicked()), &app, SLOT(quit()));
quit.show();
return app.exec();
}
This is the thing I am trying to run. Compiles fine, no errors or warnings. But when I run it, it immediatly stops working, as in "Qt.exe has stopped working" and process returns -1073741819... it crashes the moment it tries to do something Qt-specific (QApplication...)
I added a simple cout << "Hello world"; before QApplication app(argc, argv);, and it displayed in the console, and then stopped working.
Even when I bare the code down to
QApplication app(argc, argv);
return app.exec();
it still crashes the same way.
My first question... what could possibly be the problem? I ran out of ideas and Google doesn't want to help me either. I've tried using the Qt Creator, and it worked fine, but I couldn't get it to work... it'd just print "Naci" into the console, regardless of what project I tried to run, and I have no idea what "Naci" is and where it came from.
PS: And another question. Is it possible to create a console app that at one point calls a function that has the QDialog window and gets filename from it?
edit:
I'm trying to run examples attached to Qt release. They all give me undefined reference to vtable errors and none of the solutions I've found around work. Jesus... this is not friendly towards new people
edit2: I'm going to rebuild and reconfigure Qt... on my 1ghz netbook it's probably going to take a while...

I reconfigured Qt and then rebuilt it via MingW again. Even though after 6 hours compilation failed, it did enough... and now Qt works with Code::Blocks. Odd. I've done that before, no idea why this time it worked and the last time it didn't.
For those who don't know how to do that, run command line (if on Windows), go to the main folder of Qt (the one with include, bin, lib (etc) folders, in my case C:\Qt\4.8.5), type configure.exe (use configure.exe -help to see available parameters) and after it's done configuring just go mingw32-make and wait for an hour or ten.

Related

"Failed to setup resampler" when starting QAudioSink

I'm porting some QtMultimedia code from Qt 5.15 to 6.4.1. The following program, when built with Qt 6.4.1 on Windows:
int main (int argc, char *argv[]) {
QCoreApplication a(argc, argv);
QAudioDevice device = QMediaDevices::defaultAudioOutput();
QAudioFormat format = device.preferredFormat();
QAudioSink *output = new QAudioSink(device, format);
output->start();
return a.exec();
}
Fails to start the audio output, printing the following message:
qt.multimedia.audiooutput: Failed to setup resampler
The equivalent code in Qt5 (using QAudioDeviceInfo and QAudioOutput) seems to run fine. What am I missing here?
Apparently, it's a bug in Qt 6.4.1 on Windows, where, as the user johnco3 discovered in that forum post, for some reason QAudioSink is looking for a DLL named "mfplat.dll.dll" when it should be looking for "mfplat.dll" (it adds an extra ".dll" suffix).
The correctly named version of this DLL lives in the Windows system directory (e.g. C:\Windows\System32\mfplat.dll), so there are a couple of workaround until the bug is fixed:
Go back to Qt 6.4.0, apparently it's a new issue in 6.4.1, or
Copy mfplat.dll to somewhere in the DLL path then rename it to "mfplat.dll.dll":
Either copy it to the application executable's directory and rename it there, or
Create some folder somewhere, copy and rename it there, then add that folder to the PATH environment variable.
It's a somewhat silly bug, but alas. At least the workaround exists and can be easily undone when the bug is eventually fixed.
See also:
https://bugreports.qt.io/browse/QTBUG-108383 (johnco3's bug report)
https://bugreports.qt.io/browse/QTBUG-108669 (a duplicate bug report; I filed it before I found any of this)

compiled QMLs incompatible between minor version changes

I have a very simple QML app made with Qt 5.12 that runs fine on an linux embedded device. It consists of only one qml resource.
The device has recently gotten an OS update and now Qt 5.14 is available.
However the app now cannot run because of this error:
QQmlApplicationEngine failed to load component
qrc:/main.qml:-1 File was compiled ahead of time with an incompatible version of Qt and the original file cannot be found. Please recompile
It is a shame that with such a small version change i would need to keep both binaries for compatibility. I read some stuff on the internet saying to add CONFIG += qtquickcompiler to enable compilation before start, but it didn't change anything. The binary size is the same.
I haven't experienced such problems when not using QML, could even launch Qt5.9 compiled apps on Qt5.12 without problem.
That's basically all of my main.cpp:
int main(int argc, char *argv[])
{
QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;
return app.exec();
}
The QML file itself only imports
import QtQuick 2.5
import QtQuick.Window 2.2
Is there anything I can do to preserve compatibility without storing a binary for each version I'm going to support? I could probably store the raw qml file right next to the binary and load it from the disk, but I'd rather keep it stored in the app resources.
I have confirmed it works fine when using QUrl::fromLocalFile, but I really don't like to have all the qml files lying around. Is there any way to store the qml as a resource AND not compile it? I know it's worse for start performance but I'm willing to accept it for compatibility
I think I found the way myself, actually the qtquickcompiler is enabled by default and removing it from the config resulted in a smaller binary size and launched properly on the newer Qt environment without having any local QML files (still using qrc) and also after clearing the QML cache.
CONFIG -= qtquickcompiler
In case no one has any better way to preserve compatibility, I'll mark this as accepted

Invisible text in QTextEdit

I am learning Qt and running examples from Qt SDK 5.9.1. I run the code below and write inside QTextEdit but no text appears. Cursor moves as I write but no text is shown. Window title text is shown. I added addApplicationFont and setFont calls below I found from web to the sample but it didn't help.
#include <QtWidgets>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QFontDatabase::addApplicationFont("://Ubuntu-R.ttf");
app.setFont(QFont("Ubuntu", 11, QFont::Normal, false));
QTextEdit textEdit;
textEdit.show();
return app.exec();
}
I am on Ubuntu 16.04 and run following commands on bash to make executable:
qmake -makefile
make
./part1
I want the app to use the default Ubuntu system font. I learned that Qt uses fontconfig for fonts but I don't know how to trace the issue.
Edit
I thought QFontDatabase::addApplicationFont("://Ubuntu-R.ttf") call referenced system font but instead it is referencing font app resource file. I don't have resource file so obviously it won't work.
.pro file is below(unmodified sample file):
QT += widgets
SOURCES = main.cpp
# install
target.path = $$[QT_INSTALL_EXAMPLES]/widgets/tutorials/gettingStarted/gsQt/part1
INSTALLS += target
I tried to get system font using QFontDatabase but it didn't work:
app.setFont(QFontDatabase::systemFont(QFontDatabase::GeneralFont));
This doesn't do anything with any of enum values including QFontDatabase::GeneralFont
QFontDatabase database;
QStringList fam = database.families();
fam size is zero.
I will try to use embedded font next.
I don't know the exact reason of the problem but the reason was not configuring fontconfig dependency properly before building qt. I solved it by reconfiguring and recompiling qt again. You can find more details at qt forum.

Qt5: Preventing another instance of the application doesn't work anymore...!

I am using Qt5 on a Windows7 platform.
My application is some kind of TCP server listening on port 8002, so I only want one instance of it.
In order to prevent multiple instances of my application, I use(d) the code below (found here on StackOverflow):
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QSharedMemory sharedMemory;
sharedMemory.setKey("TcpServer-Key");
if(sharedMemory.create(1) == false)
{
QMessageBox::warning(NULL, "Warning!", "Another instance already running!");
a.exit(); // exit already a process running
return 0;
}
...
Well, the code above used to work just fine, until I upgraded my Qt to 5.5.1.
Now, with Qt 5.5.1, I don't see the warning message-box anymore!... When I try to start another instance, the running app disappears/stops and a new app is started!!!
Please help, what should I do? But don't tell me to switch back to Qt 5.4.x :(
Remark: I forgot to mention that I set & used msvc2012 compiler during tests (not minGW, as I wasn't able to build log4cxx for it).
UPDATE: Could it be an issue related to the antivirus installed on that PC (at the office, i.e. McAfee)?... Now I'm at home (AVG antivirus and MinGW compiler and log4cxx removed) and I am unable to reproduce the above described issue :(
I finally discovered where the problem was... and it's not the antivirus to be blamed :)
When I upgraded the Qt (Creator v3.6.0) to the newest version (5.5.1), there is a setting in Tools->Options->Build&Run named [Stop app before building]... that was set to Current project or something. Hence, the Qt Creator was killing the old instance before launching a new one(!).
Setting this option to None solved the issue.
So, it seems the code was just fine, antivirus was fine, yet launching the app from within Qt Creator was somehow restricted to only one instance :)
I decided to share this, maybe it will be helpful for other folks as well.
Remark : I checked again and now I can confirm: That setting didn't exist before, at least not in Qt Creator v3.3.2.

Qt Creator + OpenCV: Program runs from .exe but not from editor

Well, I need to start working with OpenCV and as I'm used to working with QtCreator, I'm trying to make it all work together. I downloaded the latest OpenCV version, and compiled it with MinGW. Then, I created this little console project to try it out. Below is the .pro file:
QT += core
QT -= gui
TARGET = OpenCV_test4
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
INCLUDEPATH += C:\\Librerias\\opencv2.3.1\\release\\include
LIBS += -LC:\\Librerias\\opencv2.3.1\\release\\lib \
-lopencv_core231.dll \
-lopencv_highgui231.dll \
-lopencv_imgproc231.dll \
-lopencv_features2d231.dll \
-lopencv_calib3d231.dll
Here is the main.cpp file:
#include <QtCore/QCoreApplication>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
// read an image
cv::Mat image= cv::imread("img.jpg");
// create image window named "My Image"
cv::namedWindow("OpenCV Window");
// show the image on window
cv::imshow("OpenCV Window", image);
// wait key for 5000 ms
cv::waitKey(5000);
return a.exec();
}
(I have tried this code with and without the QCoreApplication lines)
The deal is: It links and builds, and when runs from QtCreator only a terminal window called C:\QtSDK\QtCreator\bin\qtcreator_process_stub.exe appears with the line "Press RETURN to close this window..."
But, if I run the .exe from the project folder, it runs perfectly!! Why is QtCreator unable to launch the application? I found this really strange, and I would appreciate any hint about this. It's really not THAT important, but it is kind of a pain to have to run the .exe manually every time I change something to check how it works.
Thanks for your time :)
Additional Info:
I have tried both debug and release versions, the problem is the same in both of them.
Debugging does not work, it never stops at any breakpoint.
I'm running on Windows 7 Proffesional x64
SOLVED, I don't really know what I did, it suddenly worked and keeps working, I wish I could tell you how I fixed it but I have no idea, such a weird thing :(
Check Projects -> Run Settings -> Run in terminal. It have to be enabled, but seems to be disabled.
I have met same problem with QtCreator and OpenCL under Linux. Simple test program works after start from terminal and it does not work after start from the QtCreator.
I found the cause was hardcoded LD_LIBRARY_PATH in the project's run environment settings. I had dropped it to empty string and this had fixed issue.
I had the same issue with the following environment: Raspbian, Qt, openCV and a gui application.
old-ufo recommendation worked for me:
- First, enable "Run in terminal", which failed
- Then, disable "Run in terminal", which allowed me to correctly debug my app.
I understand that this is not scientific.