Qt Background Image for window show black screen - c++

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 ?

Related

Why Windows font disappered after running my Qt application?

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!

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.

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.

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.