PDA

View Full Version : How to set spinner image for qstackwidget ???



naufalahad
20th December 2011, 08:05
Hi All,
I am trying to add a spinner to QStackwidget, i am having 5 frame pages in the stackwidget with 5 buttons below in a seperate frame and when i press the 1st button it should load the spinner image in the frame page and similarly for all others.. In my application,when i give these below codes i am not able to get it.

QAnimationLabel* spinner = new QAnimationLabel(":images/spinner-24x24.gif", this);
spinner->start();

QVBoxLayout* layout = new QVBoxLayout;
layout->addWidget(spinner, 0, Qt::AlignCenter);
QFrame* frame = new QFrame;
frame->setLayout(layout);
setCentralWidget(frame);

It's not accepting the SetCentralWidget(frame); and i could not find any solution for this.
Answers will be highly appreciated!!!

Thanks in Advance and Regards,
Naufal.A

ChrisW67
20th December 2011, 11:05
You start talking about a QStackedWidget and five buttons, show a code snippet that does not include any reference to a QStackedWidget or five buttons, and then complain that "It's not accepting the SetCentralWidget(frame)" (a QMainWindow function). We know that you are "not able to get it" (whatever it is). There's no error message for us to work with. What are you expecting us to do with this?

I have no idea what a QAnimationLabel is.

naufalahad
20th December 2011, 13:11
Hi Chris,
Actually sorry for that code!!! i need an alternate solution and i had tried using that code only!!!!
I will explain you clearly again. I am having a stackwidget and above it i kept 5 frames.. below all that i kept 5 buttons for all those pages.. if i press a button it goes to each and every page., i want to add a loader image into it!!! I tried the above codes but i could not find any solution.,

Can you get my point!!!

Thanks in Advance and Regards,
Naufal.A

amleto
20th December 2011, 21:03
really!!!??? ok!!!!

ChrisW67
21st December 2011, 00:51
I tried the above codes but i could not find any solution.
Your code above doesn't even try to address a stack of widgets or a set of buttons. It looks like you didn't even try to find a solution.

Here is an example with all the elements you describe. I suggest you read the excellent documentation for each class, work out what its purpose is, and come back with specific questions if you cannot work something out.


#include <QtGui>

class MainWindow: public QMainWindow {
Q_OBJECT
public:
MainWindow(QWidget *p = 0): QMainWindow(p) {
QWidget *central = new QWidget(this);
setCentralWidget(central);

QVBoxLayout *layout = new QVBoxLayout;
central->setLayout(layout);

QStackedWidget *stack = new QStackedWidget(central);
layout->addWidget(stack);

QHBoxLayout *hbox = new QHBoxLayout; // for the buttons
layout->addLayout(hbox);

// Construct the stack and buttons
QSignalMapper *mapper = new QSignalMapper(central);
for (int i = 0; i < 5; ++i) {
// Put something on the page
QLabel *label = new QLabel(stack);
label->setPixmap(QPixmap(QString(":/image%1.png").arg(i)));
stack->addWidget(label);

// Add a button to select the page
QPushButton *button = new QPushButton(QString("Page %1").arg(i), central);
hbox->addWidget(button);
mapper->setMapping(button, i);
connect(button, SIGNAL(clicked()), mapper, SLOT(map()));
}
connect(mapper, SIGNAL(mapped(int)), stack, SLOT(setCurrentIndex(int)));

}
};

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MainWindow m;
m.show();
return app.exec();
}
#include "main.moc"

and the resource file:


<RCC>
<qresource prefix="/">
<file>image0.png</file>
<file>image1.png</file>
<file>image2.png</file>
<file>image4.png</file>
<file>image3.png</file>
</qresource>
</RCC>

You need to provide the five image files for the example to work.
71957194719371967197

naufalahad
21st December 2011, 06:33
Hi Chris,
Thanks for your reply but my idea was to bring a loading page(.gif image) when i press a button at the loading time of every button is pressed to load a page. I will try your code and work it out. Had got some idea from your code.

Thanks again and Regards,
Naufal

ChrisW67
21st December 2011, 06:59
What are you "loading"? The QStackedWidget pages already exist, and you are just switching from one to another.

naufalahad
21st December 2011, 08:01
Hi Chris,
I want to load the loader image (ie, loader.gif image),while the page is loading when the button is pressed....... i need to show this image(loader) before the next page gets loaded!!!
So only i had given that Qanimation label in the previous code!!! I want to set that in the button part such that when i clicked it should start running the gif image on the screen.. i had ported the API's of the loading page but could not able to port them into the button.. I tried those code which i mentioned above in the button part but could not find that is running or not... Did u get my point!!!! In-case no tell me chris i will explain you with my codes too.

Thanks in advance and Regards,
Naufal.A

amleto
21st December 2011, 10:19
why are your pages loading so slow that you need a 'loader image'? How do you know when the page is finished loading?

naufalahad
21st December 2011, 10:50
Hi Amleto,
I will stop them when the page gets loaded from the server, i am getting the parse value., at that time i need to put this loading image into it.

Could you find me any answers for this!!! will be very much helpful

Thanks in advance and Regards,
Naufal.A

amleto
21st December 2011, 18:51
the loading image works fine from your code here (http://www.qtforum.org/post/116521/how-to-set-spinner-image-for-qstackwidget.html#post116521)

ChrisW67
22nd December 2011, 03:39
I think this is where we are at... Your actual query has:

Nothing to do with QStackedWidget
Nothing to do with QMainWindow and setCentralWidget()
Nothing to do with five buttons selecting pages in a stacked widget
Nothing to do with the loading of a stacked page

Excellent way to ask a smart question.

It is still vague, but I think:

You simply want to display a popup window (or unhide a widget) with an animated gif in it (instead of using something like QProgressBar). The popup should stay until dismissed/hidden at the end of some unspecified loading period.



#include <QtGui>

class LoadingWidget: public QLabel {
Q_OBJECT
public:
LoadingWidget(QWidget *p = 0): QLabel(p) {
movie = new QMovie(":/loading.gif", QByteArray(), this);
setMovie(movie);
}
protected:
void showEvent ( QShowEvent * event ) {
movie->start();
QLabel::showEvent(event);
}
void hideEvent ( QHideEvent * event ) {
movie->stop();
QLabel::hideEvent(event);
}
private:
QMovie *movie;
};


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

LoadingWidget l;
l.resize(64, 64);
l.show();

// Simulate a 5 second load time then close (or hide)
QTimer::singleShot(5000, &l, SLOT(close()));

return app.exec();
}
#include "main.moc"

You can provide your own loading.gif.