V
V
Vitali2010-09-09 14:25:12
Qt
Vitali, 2010-09-09 14:25:12

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

4 answer(s)
A
Artemzr, 2010-09-10
@vitall

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

ps I changed the example for simplicity, in real life all my widgets are inherited from one abstract class, in which common methods are placed, as a result, the code became smaller and more readable.

A
Artemzr, 2010-09-09
@Artemzr

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

}

To switch widgets, you need to associate, for example, such a slot with some kind of signal (click on the buttons):
void MainWidget::buttonClicked()
{
   stackedWidget->setCurrentIndex(index);
}

Where index is your widget number.

V
Vasily Sorokin, 2010-09-09
@Vass

I wrote a program at the Maemo conference in Moscow, the code is here . There is a QStackedWidget, MB will do.

S
silvansky, 2011-07-05
@silvansky

My project: code.google.com/p/qcheckersmobile/
There the main window uses it. Simple and, I think, understandable.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question