0
votes

I have an embedded Qt application for Linux which has the following startup code:

int main(int argc, char *argv[])
{

QApplication app(argc, argv);

// Startup actions neeeded to display AppView correctly
// Black screen is shown for several seconds
// ...

QQuickView AppView;
AppView.setSource(QUrl(QStringLiteral("main.qml")));
AppView.resize(480, 800);
AppView.show();
return app.exec();

}

I'd like to remove the black screen shown before AppView and to display QML animation instead of it.

I see two possible options here but non of them is clear. Could you please advice which one of them is more correct and also comment/answer the questions in each.

Option 1: To display QSplashScreen at the beginnning of main().

int main(int argc, char *argv[])
{
 
QApplication app(argc, argv);
QSplashScreen *splash = new QSplashScreen();
splash->show();

// Startup actions neeeded to display AppView correctly
// Black screen is shown for several seconds
// ...  
     
}

The question here is what API to use to attach QML animation to QSplashScreen? QSplashScreen inherits from QWidget, and so as I understand, no API like QQuickWidget::setSource() can be used.

Option 2: To display another QQuickView at the beginnning of main() with attached QML animation.

int main(int argc, char *argv[])
{
       
QApplication app(argc, argv);
QQuickView SplashView;
SplashView.setSource(QUrl(QStringLiteral("SplashScreen.qml")));
SplashView.resize(480, 800);
SplashView.show();
app.exec();
     
// Startup actions neeeded to display AppView correctly
// Black screen is shown for several seconds
// ...
    
QQuickView AppView;
AppView.setSource(QUrl(QStringLiteral("main.qml")));
AppView.resize(480, 800);
AppView.show();
       
}

The question here is how to to close the SplashView and to display AppView on top of it?

Thanks

1
What kinds of things does AppView need to wait for? - JarMan
Some application startup code, needed for correct launch of AppView. - Maxim
I've done splash screens before, but the QQuickView was not the part I needed to wait for. The general idea is to display something as quick as possible and then generate a signal when the rest of the app is ready. If possible, I would try to rework things so that the app can start before that startup code is finished. - JarMan
It will be hard to change the code in this place since it is an already existing big app. But I will try, maybe I will succeed. Thanks for the advice. - Maxim

1 Answers

0
votes

Option 1 is not good idea. You should not mix Qt Quick and Qt Widgets applications.

QApplication::exec() starts event loop of main gui thread and never returns until application is quit. Although I do not like your way of handling splash screen, in this case, you can use something like this.

  QGuiApplication app(argc, argv);
  QQuickView view;
  view.setSource(QUrl("SplashScreen.qml"));
  view.resize(640, 480);
  view.show();

  QTimer::singleShot(2000,&view,[&view](){
    view.close();
    view.setSource(QUrl("main.qml"));
    view.show();
  });

  return app.exec();

I used timer to simulate "actions to display app correctly" . You should replace that part with your class, which does actions. When actions are done that class can emit a signal in receiving slot you can close current view which shows splash screen and show main app.