Why Windows font disappered after running my Qt application? - c++

Background
I write an uninstaller on Windows platform using Qt and C++. When running, It copy itself to windows temp folder and rerun the exe in that folder. The new process of uninstaller will stop and uninstall some service & driver I installed, then remove some files on the disk.
Problem
After the uninstallation is complete, the font of the Windows system will disappeared partly, as following picture shows:
As you can see, the oringe one is my uninstaller, and both the font of folder name in windows File explorer and the font in my blue button is disappeared, but some other font is still visible like the left side nav bar.
And most important, it just happend by accident(But it really happened many times on some machines). It cannot reappear stably.
I can recover it by restart the File explorer.exe or reboot the Windows system.
Environment
I used a static lib of Qt 5.15.2 which I build from source.
C++ 17
All the executable is compiled and linked by microsoft visual studio(2019) c++ kits
All the application is linked statically with MT option.
I tested the uninstaller on Win7 & Win10 (The problem had appeared on both platforms).
The font which is used in my uninstaller is 'Alibaba PuHui' and 'Roboto'.
Qt font usage
I think the most possible reason is the wrong usage of the Qt font loading in my uninstaller, so I put the method I used here.
Firstly, I embed the font in my application through .qrc file
<RCC>
<qresource prefix="/">
<file>res/font/Alibaba-PuHuiTi-Medium.ttf</file>
<file>res/font/Alibaba-PuHuiTi-Regular.ttf</file>
<file>res/font/Roboto-Regular.ttf</file>
<file>res/font/Roboto-Medium.ttf</file>
</qresource>
</RCC>
Then I init the font by the initFont() function
#include <QString>
#include <QFontDatabase>
inline QString ali_font_regular = "";
inline QString ali_font_medium = "";
inline QString roboto_font_regular = "";
inline QString roboto_font_medium = "";
inline void initFont() {
int med_font_id = QFontDatabase::addApplicationFont(":/res/font/Alibaba-PuHuiTi-Medium.ttf");
ali_font_medium = QFontDatabase::applicationFontFamilies(med_font_id).at(0);
int reg_font_id = QFontDatabase::addApplicationFont(":/res/font/Alibaba-PuHuiTi-Regular.ttf");
ali_font_regular = QFontDatabase::applicationFontFamilies(reg_font_id).at(0);
int roboto_reg_font_id = QFontDatabase::addApplicationFont(":/res/font/Roboto-Regular.ttf");
roboto_font_regular = QFontDatabase::applicationFontFamilies(roboto_reg_font_id).at(0);
int roboto_medium_font_id = QFontDatabase::addApplicationFont(":/res/font/Roboto-Medium.ttf");
roboto_font_medium = QFontDatabase::applicationFontFamilies(roboto_medium_font_id).at(0);
}
And Finally, the font setting of my Qt widget is like this:
logo_text_b = new QLabel(this);
logo_text_b->setMargin(0);
logo_text_b->setText("test");
QFont f_b(ali_font_medium);
f_b.setPixelSize(12);
logo_text_b->setFont(f_b);
logo_text_b->setStyleSheet("QLabel { color : white; }");
I have no thoughts what causes it happend, And I can't find a way to debug because it happend by accident. I hope someone can give me some direction. THANKS!

Related

Qt Background Image for window show black screen

first of all i'm new in qt, and then in my project i try to set background image for window with resource file, for this work i used from this code :
ui->setupUi(this);
QPixmap bkgnd(":/img/img/auto4.jpg");
int widWidth = this->width();
int widHeight = this->height();
bkgnd = bkgnd.scaled(widWidth, widHeight, Qt::KeepAspectRatioByExpanding);
QPalette palette;
palette.setBrush(QPalette::Background, bkgnd);
this->setPalette(palette);
after build, in debug mode or when i open exe file in my system all is correct and work perfectly !
but when i move file to other system, i just see a black window !!!
for move and run into new system i move only exe file and qt dll, here is list of all file in new system !
Autorun.exe
libEGL.dll
libgcc_s_dw2-1.dll
libstdc++-6.dll
libwinpthread-1.dll
Qt5Core.dll
Qt5Gui.dll
Qt5Network.dll
Qt5Widgets.dll
so i want to know whats wrong ? why only work in my system and show black window in other system ?

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.

How do I set my application version for Windows in Qt?

When my application crashes, the Windows Event Viewer always reports my application version as "0.0.0.0".
I can't figure how to set the application version in a way that the Windows Event Viewer recognizes. Changing it with QApplication::setApplicationVersion() doesn't seem to do it.
Obviously there are better ways to debug a program than the Windows Crash Log, but in lieu of all of that, how would I go about setting this value so that Windows recognizes it? My IDE is Qt Creator.
You can set the VERSION qmake variable in your pro file:
VERSION = 1.0.0.0
On Windows, triggers auto-generation of an .rc file if the RC_FILE and
RES_FILE variables are not set. The generated .rc file will have the
FILEVERSION and PRODUCTVERSION entries filled with major, minor, patch
level, and build number.
Use the QCoreApplication class.
QCoreApplication::setApplicationVersion("1.0");
If you develop a widget app or qml, maybe you want to show your version in WindowTitle full solution would be:
In .pro file add:
VERSION = 1.2.3
DEFINES += APP_VERSION=\\\"$$VERSION\\\"
and in QWdiget or QMainWindow
setWindowTitle("Qt " +qtVersion + " Version"+ APP_VERSION);
or in QML
ApplicationWindow {
title: "Qt " +qtVersion + " Version"+ APP_VERSION
...

Why are my qt file dialogs rendered incorrectly?

I'm currently creating a GUI with Qt 5.6 and I want to let the user open a file. I do this using the following code in my main window class:
#include <QFileDialog>
...
void MainWindow::loadScene()
{
QString loadPath = QFileDialog::getOpenFileName(this,
tr("Choose a scene file"), QString("."),
tr("Scene files (*.keyScene)"));
if(!loadPath.isEmpty())
resourceManager->loadScene(loadPath.toStdString());
}
I get the following dialog:
Screenshot of open file dialog
The system I use is Ubuntu 16.04, I've also tested on Ubuntu 14.04 and Mint 17.3. I get the exact same result on all of them.
When I compile the project in Windows everything works fine.
EDIT
It was suggested I play around with the DontUseNativeDialog flag when creating a file dialog, I can get a working Qt style file dialog with the following code:
QFileDialog dialog(this);
dialog.setAcceptMode(QFileDialog::AcceptOpen);
dialog.setOption(QFileDialog::Option::DontUseNativeDialog, true);
if(!dialog.exec())
return;
So the question is actually why native file dialogs aren't rendered correctly.

QSystemtrayicon: no image on Mac

I've got a problem with the QSystemTrayIcon class on Mac and Linux.
I made a program creating a System tray icon, and I have no problem on Windows, but under Linux (ubuntu 12) and Mac (OSX 10.8), the tray icon is here, but the image on it doesn't show up.
This is what I'm doing in the ctor of my class:
icon = new QIcon("trayIcon.png");
m_pTrayIcon = new QSystemTrayIcon();
m_pTrayIcon->setIcon(*icon);
m_pTrayContextMenu = new QMenu();
m_pTrayContextMenu->addAction(openSettings);
m_pTrayContextMenu->addAction(switchSyncMode);
m_pTrayContextMenu->addAction(openFolder);
m_pTrayContextMenu->addSeparator();
m_pTrayContextMenu->addAction(quit);
m_pTrayIcon->setContextMenu(m_pTrayContextMenu);
m_pTrayIcon->show();
I'm using the Qt 4.8 library.
Anyone have an idea?
Two possibilities
It cannot find the file. Try what happens if you give the global path.
It cannot load the png; see if loading QT module Multimedia helps.