QSystemTrayIcon not showing after using windeployqt tool [duplicate] - c++

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));
}

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)

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.

CodeBlocks Qt HelloWorld.exe has stopped working (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.

Compiler can't find ui_xyz.h file

I've install Netbeans 7.2 and Qt 5 on Windows7. Everything compiles fine.
But recently when I create a Qt form and try to use it in this way:
New Project > C/C++ Qt Application > Finish
Right click on new Qt project > New > Qt Form > Finish
After saving ui file in Designer
This error appears:
newForm.h:11:24: fatal error: ui_newForm.h: No such file or directory
The content of main.cpp is :
#include <QGuiApplication>
#include "newForm.h"
int main(int argc, char *argv[])
{
// initialize resources, if needed
// Q_INIT_RESOURCE(resfile);
QGuiApplication app(argc, argv);
// create and show your widgets here
return app.exec();
}
I tried to manually create ui_xwz.h file and add it to my project. But I want it work automatically as same as before. How can I solve it?
I have a ridiculous solution, Maybe we should report it as a bug to Netbeans or Qt.
Go and active QtSVG and QtXml modules of your project and rebuild it. I tested this way and the problem vanished.

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