Qt translations loaded after window load - c++

I'm trying to setup translations for a qt app but it seems that the translations files are loaded after the window because the only translated text are the ones in messages box
This is the code:
#include <QApplication>
#include <QTranslator>
#include "mainwindow.h"
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
QCoreApplication::setOrganizationName("Lebbadi");
QCoreApplication::setOrganizationDomain("lebbadi.fr");
QCoreApplication::setApplicationName("zNavigo");
QTranslator translator;
if(translator.load(QLocale(QLocale::French), "app", "_", ":/translations"))
app.installTranslator(&translator);
else
exit(-1);
MainWindow window;
window.show();
return app.exec();
}
Thanks

Related

QPaintDevice: Cannot destroy paint device that is being painted

I want to create a PDF with Qt. This is my code:
#include <QPdfWriter>
#include <QTextDocument>
int main() {
QPdfWriter pdfWriter{R"(C:\TEMP\test.pdf)"};
QTextDocument document;
document.setPlainText("Hello world");
document.print(&pdfWriter);
}
It crashes on the last line with
QPaintDevice: Cannot destroy paint device that is being painted
What did I do wrong?
You didn't create a QApplication. Change the code to
#include <QApplication>
#include <QPdfWriter>
#include <QTextDocument>
int main(int argc, char *argv[]) {
QApplication app{argc, argv};
QPdfWriter pdfWriter{R"(C:\TEMP\test.pdf)"};
QTextDocument document;
document.setPlainText("Hello world");
document.print(&pdfWriter);
}
and it will create the PDF. Running app.exec() is not required.

How to hide QMainWindow and show splashcreen during startup?

I am trying to hide the MainWindow of my Qt desktop app during startup, and to show a splashscreen. Both only happens after the loading phase, even though I call both splash.show() and window.hide() before the loading phase. I tried to split loading phase and constructor, but result is the same. How can I achieve both before the loading phase ?
Update 1
To display the splash screen, I had to add a call to QApplication::processEvents()
Update 2
The black window was actually not the MainWindow, but a ghost window that popped because scrollArea->setVisible(true) was called in the constructor.
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QPixmap pixmap(QStringLiteral(":/ressources/icons/icon.png"));
QSplashScreen splash(pixmap);
splash.show();
MainWindow window; // this loads for 5-6 seconds
a.processEvents();
window.showLoginPrompt();
splash.finish(&window);
return a.exec();
}
Based on your code and some example I could make it run like you are trying to do.
You only need to call your promptLogin function instead.
#include <QApplication>
#include <QTimer>
#include <QSplashScreen>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QSplashScreen *splash = new QSplashScreen;
splash->setPixmap(QPixmap("D:\\Projects\\SplashScreen\\TestSplashScreen\\splash.png"));
splash->show();
MainWindow mainWin;
QTimer::singleShot(2500, splash, SLOT(close()));
QTimer::singleShot(2500, &mainWin, SLOT(show()));
return app.exec();
}

QDialog expanding base on the QLabel content

I'd like to show and then close a dialog after 5 seconds. The dialog needs to be automatically resized (horizontally and vertically) based on the content of a label. Here is my code:
#include <QApplication>
#include <QDialog>
#include <QLabel>
#include <QTimer>
void notify (int intTime=1000)
{
QDialog notify;
notify.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
notify.setWindowFlag(Qt::FramelessWindowHint);
QLabel *lbl = new QLabel(&notify);
lbl->setText("This is a test This is a test This is a test This is a test This is a test This is a test This is a test");
QApplication::processEvents();
notify.adjustSize();
QTimer::singleShot(intTime, &notify, SLOT(close()));
notify.exec();
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
notify(5000);
exit(0);
// return a.exec();
}
It does not not expand the dialog based on the label size. Here is how it looks:
How can I fix it? (Please also let me know if there is better way of doing this.)
I am using Qt5 in Linux.
Since you have not used a QLayout the QLabel will be displayed as large as you can, a possible request is to change the size of QDialog to the recommended size of QLabel with sizeHint():
#include <QApplication>
#include <QDialog>
#include <QLabel>
#include <QTimer>
void notify (int intTime=1000)
{
QDialog notify;
notify.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
notify.setWindowFlag(Qt::FramelessWindowHint);
QLabel *lbl = new QLabel(&notify);
lbl->setText("This is a test This is a test This is a test This is a test This is a test This is a test This is a test");
QApplication::processEvents();
notify.resize(lbl->sizeHint());
QTimer::singleShot(intTime, &notify, SLOT(close()));
notify.exec();
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
notify(5000);
exit(0);
// return a.exec();
}
The other possible solution is to use a QLayout:
#include <QApplication>
#include <QDialog>
#include <QLabel>
#include <QTimer>
#include <QVBoxLayout>
void notify (int intTime=1000)
{
QDialog notify;
QVBoxLayout *lay = new QVBoxLayout(&notify);
//notify.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
notify.setWindowFlag(Qt::FramelessWindowHint);
QLabel *lbl = new QLabel;
lay->addWidget(lbl);
lbl->setText("This is a test This is a test This is a test This is a test This is a test This is a test This is a test");
QApplication::processEvents();
QTimer::singleShot(intTime, &notify, SLOT(close()));
notify.exec();
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
notify(5000);
exit(0);
// return a.exec();
}

Program crashes when play video with QWebView

Here is the code
#include <QApplication>
#include <QtWebKitWidgets/QWebView>
int main(int argc, char * argv[])
{
QApplication app(argc, argv);
QWebView view;
QCoreApplication::addLibraryPath("./plugins");
view.settings()->setAttribute(QWebSettings::PluginsEnabled, true);
// crash here
view.load(QUrl("http://hon.qq.com/act/20140320video/Aluna.html"));
// OK to show youtube
//view.load(QUrl("http://www.youtube.com/watch?v=KMU0tzLwhbE"));
view.show();
return app.exec();
}
It crashes when play video on hon.qq.com, but works well playing video on Youtube.

Error in Qt program

This code
#include <QtWidgets/QApplication>
#include <QLabel>
int main(int argc,char *argv[])
{
QApplication app(argc,argv);
QLabel *label = new QLabel("my first app");
label->show();
return app.exec();
}
Causing an error:
QLabel: there is no such directory
I am using Qt 5.0.1 in Windows
change
#include <QLabel>
to #include <QtWidgets/QLabel>
This is where QLabel actually resides (if this is that QLabel that you want)
Put QT += widgets in your .pro file.