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.
Related
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!
Here is the program. On the Web I found:
QSettings reg("HKEY_LOCAL_MACHINE\\SOFTWARE\\Classes\\.sp\\DefaultIcon",
QSettings::NativeFormat);
reg.setValue("Default", "C:\\Users\\Tomy\\Desktop\\package_directory"
"\\packages\\com.vendor.product\\data\\Spreadsheet.ico");
SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, NULL, NULL);
I put this in void MainWindow::writeSettings() and void MainWindow::readSettings() in the code. And then created an installer using Qt Installer Framework and installed it on Windows.
No icon! Any idea?
EDIT:
I this time removed the code above from both void MainWindow::readSettings() and void MainWindow::writeSettings() functions and added it to the MainWindow's constructor body, then re-did the rest and installed that new on my Windows.
Again, no changes in result.
There might be a problem either in the code or the script file. I'm using Qt 5.9.
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.
I'm facing the following problem:
I wrote a simple application to track working hours. Therefor I create a *.db file programmatically.
Launching the application from Qt Creator (debug or release) works perfectly fine.
I used the macdeployqt tool to get a *.dmg file. When starting the application now the method (see below) cannot open, respectively create the *.db file and I run out of ideas why. In addition to this, the application should output some *.csv files. This also fails.
One more information, running the application as administrator using the sudo command on terminal...well it works and the *.db file and *.csv files get created.
So I am quite sure it must deal with file permissions but I have no idea how to change this except for changing it in the information context menu but this didn't help at all.
Below the method for the *.db file which always returns false when not launching the app from Qt Creator:
QFile file(Globals::Environment::WORKING_DIRECTORY + "/" + "Records.db");
if(file.open(QIODevice::ReadWrite))
return true;
else
{
QMessageBox msg;
msg.setWindowTitle("Error");
msg.setText("Failed to create database file!");
msg.exec();
}
return false;
I am on MacOS 10.11.3.
Qt 5.5.1 (Clang 6.1 (Apple), 64 bit)
If more information is needed, I will provide it of course.
Thanks a lot for every help in advance.
I'm writing an application in C++ with Qt that utilizes the system tray. I have implemented the system tray using a QSystemTrayIcon class as shown in the examples, but it doesn't have the same behavior as other system tray icons that are present on my computer. For instance, I have Spotify installed on Ubuntu 12.04 and it shows a system tray icon with a drop down menu. With my application, it shows a system tray icon with a context menu, meaning you have to right click it to make the menu active. With Spotify, all that needs to be done is to click on the icon and the menu will show. What can I do to get native system tray icons in Ubuntu? I'm fine with using specific code for X11/Linux and not the built-in Qt functions. Thanks much.
Here's my code:
void MainWindow::closeEvent(QCloseEvent *event)
{
if (trayIcon->isVisible()) {
hide();
event->ignore();
}
}
void MainWindow::createActions()
{
restoreAction = new QAction(tr("&Show"), this);
connect(restoreAction, SIGNAL(triggered()), this, SLOT(show()));
quitAction = new QAction(tr("&Exit"), this);
connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
}
void MainWindow::createTrayIcon()
{
trayIconMenu = new QMenu(this);
accountsMenu = trayIconMenu->addMenu(tr("Accounts"));
trayIconMenu->addSeparator();
trayIconMenu->addAction(restoreAction);
trayIconMenu->addSeparator();
trayIconMenu->addAction(quitAction);
trayIcon = new QSystemTrayIcon(this);
trayIcon->setContextMenu(trayIconMenu);
}
Try to drop down menu from activated signal of QSystemTrayIcon.
void Window::iconActivated(QSystemTrayIcon::ActivationReason reason)
{
switch (reason) {
case QSystemTrayIcon::Trigger:
// show your menu here
}
}
I am commenting for the benefit of others here...
I had the same issue when we deployed our product (built using Qt)on Ubuntu 12.04 LTS.
We use the qt.conf way of deployment.
After a lot of hunting and going through the source on sni-qt I found that plugins need to be properly found out. So I created and copied the plugins from our build environment to the plugins directory relative to my application path mentioned in qt.conf against the 'Plugins = ' entry. Also made sure that sni-qt is update and installed on the deployed Ubuntu 12.04 machine. The menus appeared as they appear for other tray applications.
You can copy plugins from /usr/lib/i386-linux-gnu/qt4/plugins/ on a 32 bit machine or its equivalent path on 64 bit machine. For this problem plugin under systemtrayicon is the required one.
HTH.