Answer the question
In order to leave comments, you need to log in
An example Qt program using QStackedWidget?
I'm just studying qt and decided to write a program whose window is redrawn after a button is pressed, as, for example, when installing a program, but at the same time I need some kind of cycling of transitions and, as I understand it, this class suits me, only in textbooks it is practically not considered.
Answer the question
In order to leave comments, you need to log in
Well, I'll show you a piece of my code, again, this is not the whole program, but exactly the piece where I use QStackedWidget.
MainWidget::MainWidget(QWidget *parent = 0) : QWidget(parent)
{
// Виджет для упражнения "мозаика".
mosaicWidget = new MosaicWidget(this);
// Виджет для упражнения "карточка".
cardWidget = new CardWidget(this);
// Виджет для упражнения "написание".
cardWidget = new CardWidget(this);
// Соединяем сигнал окончания упражнения со слотом настройки следующего виджета
connect(mosaicWidget, SIGNAL(finished()), this, SLOT(setupNextLesson()));
stackedWidget->addWidget(mosaicWidget);
.. // Аналогично для всех остальных
}
MainWidget::setupNextLesson()
{
// Получаем случайный индекс от 0 до 2
int randomIndex = qrand() % 3;
// Настраиваем определенный виджет для показ
switch(randomIndex)
0 : mosaicWidget->setup(); break();
1 : cardWidget->setup(); break();
2 : writeWidget->setup(); break();
// Непосредственно активируем нужный на виджет
stackedWidget->setCurrentIndex(randomIndex);
updateStatusBar();
}
what is there to consider? We take the widget on which we will manipulate, and in its constructor we write the following:
class MainWidget::MainWidget(QWidget *parent = 0) : QWidget(parent)
{
...
stackedWidget = new QStackedWidget(this);
stackedWidget->addWidget(someWidget1);
stackedWidget->addWidget(someWidget2);
...
setCentralWidget(stackedWidget);
}
void MainWidget::buttonClicked()
{
stackedWidget->setCurrentIndex(index);
}
I wrote a program at the Maemo conference in Moscow, the code is here . There is a QStackedWidget, MB will do.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question