Originally Posted by
naufalahad
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>
Q_OBJECT
public:
setCentralWidget(central);
central->setLayout(layout);
layout->addWidget(stack);
layout->addLayout(hbox);
// Construct the stack and buttons
for (int i = 0; i < 5; ++i) {
// Put something on the page
stack->addWidget(label);
// Add a button to select the page
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[])
{
MainWindow m;
m.show();
return app.exec();
}
#include "main.moc"
#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"
To copy to clipboard, switch view to plain text mode
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>
<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>
To copy to clipboard, switch view to plain text mode
You need to provide the five image files for the example to work.
image2.pngimage1.pngimage0.pngimage3.pngimage4.png
Bookmarks