QLibraryInfo doesn't seem to load qt.conf - c++

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".

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)

QSystemTrayIcon not showing after using windeployqt tool [duplicate]

This question already has answers here:
QSystemTrayIcon and Windows8
(2 answers)
Closed 3 years ago.
Note: Yes, this question has been asked and answered previously, but the duplicate's answer is incomplete. Please see my solution if you are stuck on this.
Using Qt 5.13.0 MSVC2017 64-bit and Windows 10, I'm experiencing some strange behavior from QSystemTrayIcon. When running in Debug or Release from QtCreator on a fresh build, there are no issues and the icon displays exactly how I would expect it to. However, after using the windeployqt.exe tool, the icon stops showing up.
This happens both when I run it from QtCreator after windeployqt.exe and from the installation directory via Qt Installer Framework. If I delete the build directory, clean, and rebuild, the problem goes away, but obviously this is an issue for deployment.
Command:
windeployqt.exe <RELEASE_DIR>
(also tried specifying the executable directly)
MainWindow constructor:
AltheaWindow::AltheaWindow(MacroListModel &macroListModel, Config *config, const QString &configPath, QWidget *parent)
: QMainWindow(parent),
ui(new Ui::AltheaWindow),
macros(macroListModel),
config(config),
trayIcon(QIcon(IconPath), this),
shortcutNew(ShortcutPtr(new QShortcut(NewMacro, this))),
shortcutSave(ShortcutPtr(new QShortcut(SaveMacro, this))) {
qDebug() << "AltheaWindow";
ui->setupUi(this);
ui->sidebar->createSettingsDialog(config, configPath);
initConnections();
QFile f(IconPath);
bool exists = f.exists();
qDebug() << "exists = " << exists; // "exists = true"
trayIcon.show();
ui->sidebar->setMacros(&macroListModel);
if ((*(*config)[AltheaConfig::StartMinimized]).toBool()) showMinimized();
}
Regardless of whether the icon is shown or not, exists is always true, so it doesn't seem to be an issue of resolving the resource. All other resources in the application work fine as well.
Found the solution, copy imageformats to the plugins directory.
Solution: https://stackoverflow.com/a/20594583/2472874
Additional note:
For anyone experiencing this issue, I found that I also needed to have the imageformats plugin on the Library Paths before the application object is initialized.
#define PLUGINS "plugins"
void preInit(char *argv[]);
int main(int argc, char *argv[]) {
#ifndef _DEBUG
preInit(argv);
#endif
Althea app(argc, argv);
AltheaWindow w(app.getMacros(), &app.getConfig(), app.getAppData().absoluteFilePath(Althea::ConfigFileName));
w.show();
return app.exec();
}
void preInit(char *argv[]) {
QDir releaseDir(QFileInfo(argv[0]).dir());
QCoreApplication::addLibraryPath(releaseDir.absoluteFilePath(PLUGINS));
}

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

QT Console Application in Lubuntu

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?

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