QT Console Application in Lubuntu - c++

I'm trying to write a console app in Qt under Lubuntu.
But when I'm trying to run something, i.e.:
#include <QCoreApplication>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
cout << " hello world";
return a.exec();
}
it fails, saying :
"Cannot change to working directory home/myusername/myproject: no such
a file or directory".
But this directory exists, there's even the build file in it, marked as executable. But when I trying to run it from the terminal, the response is: myproject-console is not a command.
What happened to "Hello, World!" app? Is there any way to solve it?

It appears that your application tries to look for a relative path home/myusername/myproject and not for the absolute path /home/myusername/myproject. Have you checked the path contained in argv?

Related

Qt plugin not loaded

I have problems loading the built-in plugins for iconengines for a deployed application. Below is a minimal program to show the problem. It is not exactly the same, because Qt scans the plugin directories at start and registers it according to the file suffix, but I hope when I can get the minimal program to run, then the automatic scanning works, too.
main.cpp:
#include <QCoreApplication>
#include <QPluginLoader>
#include <QDebug>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QPluginLoader loader("C:/Qt/5.15.1/msvc2019_64/plugins/iconengines/qsvgicon.dll");
if (loader.load()) {
qDebug() << "load ok";
} else {
qDebug() << "load failed";
}
return a.exec();
}
and PluginTest.pro:
QT -= gui
CONFIG += c++11 console
CONFIG -= app_bundle
SOURCES += main.cpp
When I start it from the Qt Creator IDE, version 4.13.1, with Visual Studio 2019, 64 bit compiled, it prints "load ok". Now I do this from a "x64 Native Tools Command Prompt for VS 2019" command prompt in the project directory:
set PATH=%PATH%;C:\Qt\5.15.1\msvc2019_64\bin
mkdir deploy
copy release\PluginTest.exe deploy
windeployqt --release release\PluginTest.exe --plugindir deploy\QtPlugins
When I start PluginText.exe from the command prompt in the deploy directory, I get the output "load failed".
Additional information: when I set set qt_debug_plugins=1 and start the real application, I see these errors:
QFactoryLoader::QFactoryLoader() checking directory path "C:/projects/deploy/QtPlugins/iconengines" ...
QFactoryLoader::QFactoryLoader() looking at "C:/projects/deploy/QtPlugins/iconengines/qsvgicon.dll"
"Failed to extract plugin meta data from 'C:/projects/deploy/QtPlugins/iconengines/qsvgicon.dll'"
not a plugin
The qsvgicon.dll file is identical to "c:\Qt\5.15.1\msvc2019_64\plugins\iconengines\qsvgicon.dll".
I found the problem. It used some other Qt DLLs from the path when starting the application. This fixed it:
xcopy /y c:\Qt\5.15.1\msvc2019_64\bin\*.dll deploy

QSound Unexpected null receiver while playing .wav files

I would like to play .wav samples of engine through out Qt creator. Of course the very first thought was on QSound class, but I did whatever was necessary to play it and it always shows me
QCoreApplication::postEvent: Unexpected null receiver
Which means that I entered wrong path for file, it seems simple but I have tried with absoulte paths and etc. Nothing new has happened.
Sourcecode and photos, I am trying it on windows but I would like to run it on Raspberry(fedora).
#include <QCoreApplication>
#include <QSound>
#include <iostream>
#include <QMediaPlayer>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QSound let("music/letitplay.wav");
let.play();
/*QMediaPlayer * music = new QMediaPlayer();
music->setMedia(QUrl("qrc:/sounds/letitplay.wav"));
music->play();
*/
return 0;
}
Snippet from .pro
QT += core
QT -= gui
QT += multimedia
TARGET = silnik1
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
RESOURCES +=
Screen of path and file directory
QSound plays sounds asynchronously; you're going to need to start your QCoreApplication first so there's an event loop running.

Qt qDebug not working with QConsoleApplication or QApplication

I currently have a terribly annoying problem while developing programs using Qt and Qt Creator. Whenever I try using qDebug() with a QCoreApplication or QApplication instantiated before using qDebug(), there isn't any output, whether I run the program in Qt Creator or from a normal shell(I'm using Fedora Linux btw). For example, even the following simple code fails:
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
qDebug() << "TestOutput!" << endl;
}
Does anybody know what to do about this problem?
Thanks in advance,
Marius
For better formatting, I add this new solution, marius still found it himself in this bugzilla.
Edit ~/.config/QtProject/qtlogging.ini and add:
[Rules]
*.debug=true
qt.qpa.input*.debug=false
The last line is to disable spammy debug logging for moved mouse messages.
Docs around this can be found here: http://doc.qt.io/qt-5/qloggingcategory.html#details
It can be configured in many ways. Few useful examples:
by env variable(cmd):
$ export QT_LOGGING_RULES="*.debug=true" ./app
by env variable(export):
$ QT_LOGGING_RULES="*.debug=true"
$ ./app
or in code:
#include <QCoreApplication>
#include <QLoggingCategory>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QLoggingCategory::setFilterRules("*.debug=true");
qDebug() << "Debugging;
a.exit();
}
OK, I found out what the problem was, it was indeed Fedora, but it's the new standard configuration. See here:
https://forum.qt.io/topic/54820/
This one help for me (In user bashrc file):
export QT_LOGGING_DEBUG=1

QLibraryInfo doesn't seem to load qt.conf

I jsut installed Qt 4.7.2 and I'm trying to use the QLibraryInfo class. My problem is that QLibraryInfo::location(QLibraryInfo::PrefixPath) always returns C:\work3\qt-4.7-vs2010-x86 which doesn't exist on my generation machine (should be C:\Qt\qt-4.7.2).
According to the documentation I tried to create a qt.conf file alongside my program, but the problem still remains. Here's its content:
[Paths]
Prefix=C:/Qt/qt-4.7.2/
For now I used a symlink to bypass the issue, but I'd like to know if there's a proper solution.
Thanks.
EDIT
Here's the program using the QLibraryInfo:
int main(int argc, char ** argv)
{
QCoreApplication app(argc, argv); //< added after Piotr's suggestion
QFile outf("qtdirs.out");
if (!outf.open(QIODevice::WriteOnly|QIODevice::Truncate|QIODevice::Text))
return 1;
QTextStream out(&outf);
out << QLibraryInfo::location(QLibraryInfo::PrefixPath) << '\n';
out << QLibraryInfo::location(QLibraryInfo::HeadersPath) << '\n';
...
}
A QCoreApplication must be created because that is how the QLibraryInfo is able to determine the application directory (QCoreApplication::applicationDirPath()) from which to load the qt.conf file. Alternatively, the qt.conf can be built into the application as a resource with the path ":/qt/etc/qt.conf".

Qt how to open a file in current dir ? or what's wrong with this?

I'm trying to open an xml file in the current location of the executable
QString path = QDir::currentPath();
path.append("/acc.xml");
QFile file(path);
if(!file.open(QIODevice::ReadOnly))
{
insertItem("IO ERR");
}
When I run it from Qt creator, everything works. currentPath() returns the path to the executable's folder
When I go to project-build-desktop/ folder and try to run it manually currentPath() returns /home/user/Documents
EDIT
also tried with same results:
Qt::current().path();
Qt::current().absolutePath();
Try to use QCoreApplication::applicationDirPath() instead of QDir::currentPath().
For details see http://doc.qt.io/qt-5/qcoreapplication.html#applicationDirPath
Check the returned value of QDir::currentPath(). I think when you run from Qt Creator, it returns the path where the project file (*.pro) is located. When you run from outside, you get path of the binary.
Edit
I never worked with Linux. However, you can try other functions/combinations from QDir:
QDir::current().path()
QDir::current().absolutePath()
etc.
To open a file in the current directory, you simply call QFile constructor
I tested this on my Linux machine and it works
#include <QtCore>
int main(int argc, char** argv){
QFile some_file("test.xml");
if(!some_file.open(QIODevice::ReadOnly | QIODevice::Text)){
qDebug() << "Unable to open file";
} else {
qDebug() << "File open successfully";
}
exit(-1);
}
I run ./TestQFile and if there is a test.xml in the current directory, it works.
UPDATE: I notice that the wording of your question says that you want the file in the same directory as the executable, this can be done as follow:
// Getting directory of the executable
QFileInfo exec_fileinfo(argv[0]);
qDebug() << "Executable is in" << exec_fileinfo.absolutePath();
UPDATE 2: Under the project panel of QtCreator, there is a field for Working Directory. This is the directory that is returned by QDir::currentPath() if you are running it via QtCreator.
I found this discussion while searching for a similar solution. I think that the most portable way of opening an external file that has a fixed name (and no dialogs and the user are involved) is to use the Resource System.
In my case I created a new resource file with the prefix /config and added the file (called settings.xml). Inside the code, I don't need to use any path functions at all. I use the resource system instead. Thus a call like QFile file(":/config/settings.xml") works fine. Using QT creator 2.0.1 and QT 4.7 on Windows.
My tip is this : name of the file + the absolute path to it before opening it. For that I call the method applicationDirPath on our QCoreApplication object (the a object in my case), which returns us the path to the executable of the application. No need going through the argv array. That's it and works
int main(int argc, char *argv[]) {
QCoreApplication a(argc, argv);
const QString fileName = "someJsonFile.JSON";
const QString currentExecDir = a.applicationDirPath();
QFile myFile(currentExecDir + '/' + fileName);
if (myFile.open(QIODevice::ReadOnly)) {
qDebug()<< "The file "<< fileName<< " has been opened successfully";
}
else {
qDebug()<< "Failed to open file "<< fileName<< " in "<< currentExecDir;
}
return a.exec();
}
NOTE
Please see the Qt docs for the applicationDirPath method, because it assumes that the current directory has not been changed by the application