QSplashScreen, how to add my QWidget!
-
Well, I'm trying to add my QWidget Screen to a QSplashScreen.
I "see at docs":http://doc.qt.nokia.com/4.7-snapshot/qsplashscreen.html that QSplashScreen supports on constructor a QWidget parameter, so I created the screen using QWidget and Iḿ trying to show this QSPlashScreen but the compiler shows me a error. Follow the code and the error:@
...
//QWidget splashScreen(0);
SplashScreeen splashScreen(0);
QSplashScreen *splash = new QSplashScreen(splashScreen);
splash->show();
...
@Error:
no matching function for call to 'QSplashScreen::QSplashScreen(QWidget&)'
candidates are: QSplashScreen::QSplashScreen(const QSplashScreen&)
QSplashScreen::QSplashScreen(QWidget, const QPixmap&, Qt::WindowFlags)
QSplashScreen::QSplashScreen(const QPixmap&, Qt::WindowFlags)*If you see has the option "QWidget* ...." At constructor, but I can't use this constructor! Why?
Thanks
- The SplashScreen code (splashscreen.h):
@
#ifndef SPLASHSCREEEN_H
#define SPLASHSCREEEN_H#include <QWidget>
namespace Ui {
class SplashScreeen;
}class SplashScreeen : public QWidget
{
Q_OBJECTpublic:
explicit SplashScreeen(QWidget *parent = 0);
~SplashScreeen();private:
Ui::SplashScreeen *ui;
};#endif // SPLASHSCREEEN_H
@ -
Well, I fix the compiler problem!
how?
@
...
SplashScreeen splashScreen(0);
QSplashScreen *splash = new QSplashScreen(&splashScreen);
splash->show();
...
@But the splashScreen is not showing...
-
Of course not. The SplashScreen object will be out of scope before the QSplashScreen has been shown...
-
I don't understand why, because the SplashScreen is only some texts and one animated gif.
Where I can find some explanations about this Andre?Thanks
-
It is basic C++, and it has nothing to do with what is on that widget. You'll find more on this issue in your favourite C++ book, in the chapter that deals with variable scopes.
-
From The Docs:
[quote]
@QSplashScreen::QSplashScreen ( QWidget * parent, const QPixmap & pixmap = QPixmap(), Qt::WindowFlags f = 0 )@This is an overloaded function.
This function allows you to specify a parent for your splashscreen. The typical use for this constructor is if you have a multiple screens and prefer to have the splash screen on a different screen than your primary one. In that case pass the proper desktop() as the parent.[/quote]
As far as I understand, the widget you give is the parent and not the widget splash screen uses... your QSplashScreen makes a default (and empty) QPixMap and displays it as splash screen, but it is not visible as it is empty(NULL to be more specific)
-
:-)
True, there is also a mixup of parent and child in the code, but I thought I'd point out one issue at a time :-) -
raaghuu, I don't read that! Very important information! This change all!
Well, the QSplashScreen only works with QPixMap! I try to use a animated GIF to Pixmap and the pixmap shows a freeze image. I will look for a solution...
Thanks raaghuu, thanks Andre.
-
[quote author="Andre" date="1342012422"]there is also a mixup of parent and child in the code[/quote]
How? I don't seem to see it...
-
Animated GIF's can only be shown using [[doc:QMovie]], which you can use on a [[doc:QLabel]]. However, that excludes QSplashScreen, at least via the normal API.
It could be (I did not check) that QSplashScreen internally uses a QLabel to display the pixmap, and if so, you can probably get a handle to it using QObject::findChildren<T> and set a QMovie on it that way. However, this is undocumented, and may or may not work in a future version.
Use the source of QSplashScreen to find out...
-
Andre, my splashscreen already works with animated gif as you write here using Qlabel e etc...
So, I think that's gonna be easier to use a QDialog to create the same viewer/resource of QSplashScreen... What do you think?
-
[quote author="raaghuu" date="1342012825"]
[quote author="Andre" date="1342012422"]there is also a mixup of parent and child in the code[/quote]How? I don't seem to see it...[/quote]
It is exactly what you indicated: he was passing in a QWidget pointer in the assumption that it would become a child of the QSplashScreen, while in reality it would become the parent. Just what you said. Sorry for the confusion.
-
[quote author="Andre" date="1342013403"]
It is exactly what you indicated: he was passing in a QWidget pointer in the assumption that it would become a child of the QSplashScreen, while in reality it would become the parent. Just what you said. Sorry for the confusion.
[/quote]
Oh... Ok... I think I was trying to see something else... Hence the confusion... Thanks
-
Why QDialog don't show the Dialog when we execute "show();"?
QDialog only shows when I user the method "exec();". I try to build a similar Splash Screen using QDialog, but I need to use the show method working... The exec lock the aplication and nothing else is executed except when we close the dialog, as everyone knows...Any workaround or tip to solved that?
-
@
myDialog->show(); //...starts the dialog
myDialog->raise(); //...makes it the topmost widget(i.e, visible)
mydialog->activate(); //...gives focus to the widget
@ -
[quote author="dcbasso" date="1342030078"]Why QDialog don't show the Dialog when we execute "show();"?
QDialog only shows when I user the method "exec();". I try to build a similar Splash Screen using QDialog, but I need to use the show method working... The exec lock the aplication and nothing else is executed except when we close the dialog, as everyone knows...[/quote]
Most probably because you have no running event loop. Use <code>application.processEvents()</code> after showing the dialog. -
Or just make sure you have an eventloop spinning quickly, and that you return to it regulary.
/me thinks processEvents() is evil. A sometimes nessecairy evil, but an evil nonetheless.
-
I'm not quite sure if it is actually evil, but it usually indicates that you might be doing something evil (which requires you to use processEvents()).
But as you said, a necessary evil sometimes. A prime example beeing splash screens.
-
Man that's a complex task to do!
I made something like this:@
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
DialogIniciandoSistema splash(0);
splash.show();
splash.raise();
Database db(0);
QTimer::singleShot(500, &db, SLOT(init()));
QTimer::singleShot(3000, &splash, SLOT(close()));
QTimer::singleShot(3000, &w, SLOT(show()));
return a.exec();
}
@Well, when the "init()" is executed my animated icon on the Splash freezes!
I think that I will need user Threads!!!! Real theard...the init() code:
@
oid Database::init()
{
if (database.open()) {
QSqlQuery query(database);
if (createTables(query))
{
if (populateTables(query))
{
qDebug() << "Populado com sucesso.";
} else {
qDebug() << "Erro ao popular tabelas. " << query.lastError() ;
}
} else {
qDebug() << "Erro ao criar tabelas. " << query.lastError();
}
}
}
QSqlDatabase Database::getDatabase()
{
return this->database;
}
bool Database::createTables(QSqlQuery query)
{
bool retorno = true;
bool temp = false;
QStringList lista = readCreateTable().split("//");
for (int i=1; i<lista.length(); i++)
{
temp = query.exec( lista[i] );
retorno &= temp;
}
return retorno;
}
bool Database::populateTables(QSqlQuery query)
{
bool retorno = true;
bool temp = false;
QStringList lista = readPopulateTable().split("//");
for (int i=1; i<lista.length(); i++)
{
temp = query.exec( lista[i] );
if (! temp)
{
qDebug() << query.lastError() << " --> SQL: " << lista[i];
}
retorno &= temp;
}
return retorno;
}
QString Database::readCreateTable()
{
QFile file(":/database/CreateTable.sql");
if(!file.open(QIODevice::ReadOnly))
{
qDebug() << "error opening file: " << file.error();
return "";
}
QTextStream instream(&file);
QString createTable = instream.readAll();
file.close();
return createTable;
}
QString Database::readPopulateTable()
{
QFile file(":/database/PopulateTable.sql");
if(!file.open(QIODevice::ReadOnly))
{
qDebug() << "error opening file: " << file.error();
return "";
}
QTextStream instream(&file);
QString populateTable = instream.readAll();
file.close();
return populateTable;
}
@ -
I resolved using QThread, connect, QDialog!
Thanks all!