How to hide QMainWindow and show splashcreen during startup? - c++

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();
}

Related

Unembedding application from QWidget

Referring to this question.
After embedding an external application with the code:
#include <QApplication>
#include<QProcess>
#include<QDesktopServices>
#include<QUrl>
#include<QWindow>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QMainWindow *w = new QMainWindow();
w->setWindowTitle("Genome Embedded Calculator");
QWindow* window = QWindow::fromWinId(41943047); // Hardcoded Window Id for Genome Calculator
window->setFlags(Qt::FramelessWindowHint);
QWidget *widget = QWidget::createWindowContainer(window);
w->setCentralWidget(widget);
w->show();
return a.exec();
}
How to detach the QWindow after closing the application?
I want the gnome-calculator process to continue working after closing my application.

settings windowtitle in maximized mode in qt

I want to know how to set window title in Qt in maximized mode.
window->setWindowtitle("window");
But this does not work in maximized mode.
playback_main *pbw = new playback_main;
pbw->setWindowTitle("PlayBack");
pbw->showMaximized();
This is not working.
Your code works for me on a QMainWindow in, admittedly, qt 4.7. Do you need to force an update with either of:
pbw->update();
qApp->processEvents();
is pbw a QMainWindow or just a widget?
I've tried the following code on linux which works too:
#include <QApplication>
#include <QMainWindow>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QMainWindow w;
w.setWindowTitle("testing");
w.showMaximized();
return a.exec();
}

QT: how to exit application and close UI

I tried to use qApp->exit() to exit application and close UI. but I failed the UI is still there after qApp->exit() executed. Anyone can help to figure out why? thanks a lot.
#include "clsDownloadUpdateList.h"
#include <QApplication>
#include <qtranslator.h>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QTranslator translator;
translator.load("en-CN_upgrader");
qApp->installTranslator(&translator);
clsDownloadUpdateList w;
w.show();
return a.exec();
}
clsDownloadUpdateList::clsDownloadUpdateList(QWidget *parent) :
QMainWindow(parent),
_state(STOP),
ui(new Ui::clsDownloadUpdateList)
{
ui->setupUi(this);
this->setWindowTitle("GCS Upgrader");
// other code
// here comes the code to exit application
qApp->exit();
// but the UI is still there.
}
#thuga is right. The problem you have is caused by your wrong code: you call qApp->exit() before right in your constructor, where your application has not started it's message cycle yet (by a.exec()).
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QTranslator translator;
translator.load("en-CN_upgrader");
qApp->installTranslator(&translator);
clsDownloadUpdateList w; // <- you call qApp->exit() right here, before a.exec();
w.show();
return a.exec();
}
It will not help you in constructor, because of no event loop started yet.
In such case you can use QTimer::singleShot() with timeout equal zero. It will cause calling what you need when event loop started. Also it is good idea to use initialization method and check it in main:
Window w;
if ( !w.init() )
return 1;
w.show();
return a.exec();
Working code:
#include <QMetaObject>
//...
QMetaObject::invokeMethod(qApp, "quit",
Qt::QueuedConnection);
Or for widgets:
QMetaObject::invokeMethod(this, "close",
Qt::QueuedConnection);

doLayout: how to setGeometry on top of another widget?

I want to create custom Layout alike here: http://doc.qt.io/qt-5/qtwidgets-layouts-flowlayout-flowlayout-cpp.html
I want some methood to put checkbox on top of custom button. For now there are
setGeometry(QRect(QPoint(...
methods for either button and checkbox but whether I'm doing it for button or checkobox first still checkbox appears "under" the button and I can't see/click it.
How can I put checkbox on top of button there?
Simply make the checkbox a child of the button and call setGeometry relative to the button. Children are always drawn on top of their parents.
QPushButton button("Hello World!", &w);
button.setGeometry(0,0,100,100);
button.show();
QCheckBox checkBox(&button);
checkBox.setGeometry(button.rect());
checkBox.show();
No need to put the checkbox into a layout.
I have just made this snippet to check the checkbox on the top of the button and it works for me.
#include "mainwindow.h"
#include <QApplication>
#include <QPushButton>
#include <QCheckBox>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget w;
QPushButton button("Hello World!", &w);
button.setGeometry(0,0,100,100);
button.show();
QCheckBox checkBox(&w);
checkBox.setGeometry(30,30,50,50);
checkBox.show();
w.show();
return a.exec();
}
and here is if you will change the order of "parenting" and want checkbox still be on the top:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget w;
QCheckBox checkBox(&w);
checkBox.setGeometry(30,30,50,50);
checkBox.show();
QPushButton button("Hello World!", &w);
button.setGeometry(0,0,100,100);
button.show();
checkBox.setParent(NULL);
checkBox.setParent(&w);
w.show();
return a.exec();
}

How to start a GUI application in the background?

How can I prevent a QApplication with a QMainWindow from stealing the keyboard focus when it starts? I'd like it to become active only after clicking on or alt-tabbing to it.
You could use showMinimized() instead of show() for you main window.
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MainWindow w;
w.showMinimized();
return app.exec();
}