Stacked Layout and Timer Usage in Qt

Stacked Layout Manager

The stacked layout manager (QStackedLayout) organizes interface elements in a stack-like manner:

  • All widgets are managed along the vertical axis perpendicular to the screen
  • Only one widget is visible at any given time
  • The topmost widget in the stack is displayed to the user

Key Characteristics

  • Each widget maintains uniform dimensions and occupies the entire parent container area
  • Direct nesting of other layout managers is not supported, though indirect nesting through intermediate widgets is possible
  • Provides flexibiltiy to switch between different visible widgets
  • Maintains exclusive visibility - exactly one widget shown at all times

Timer Functionality

Timers play a crucial role in appilcation development:

  • They generate events at regular intervals
  • Timer events are converted into function calls internally
  • From a high-level perspective, timers invoke specified functions periodically

Using QTimer

To implement timer functionality:

  • Create a slot function to handle timer events
  • Instantiate a QTimer object
  • Connect the timer's timeout signal to the handler slot
  • Configure the interval and start the timer
#ifndef WIDGET_H
#define WIDGET_H

#include <QtWidgets/QWidget>
#include <QPushButton>

class Widget : public QWidget
{
    Q_OBJECT
private:
    QPushButton buttonOne;
    QPushButton buttonTwo;
    QPushButton buttonThree;
    QPushButton buttonFour;
    void setupUI();
private slots:
    void handleTimeout();
public:
    Widget(QWidget *parent = 0);
    ~Widget();
};

#endif // WIDGET_H

Widget.h

#include "Widget.h"
#include <QStackedLayout>
#include <QHBoxLayout>
#include <QTimer>

Widget::Widget(QWidget *parent)
    : QWidget(parent), buttonOne(this), buttonTwo(this), buttonThree(this), buttonFour(this)
{
    setupUI();
}

void Widget::setupUI()
{
    buttonOne.setText("Button 1");
    buttonTwo.setText("Button 2");
    buttonThree.setText("Button 3");
    buttonFour.setText("Button 4");

    QStackedLayout* stackLayout = new QStackedLayout();

    QHBoxLayout* horizontalLayout = new QHBoxLayout();
    QWidget* container = new QWidget();
    buttonTwo.setParent(container);
    buttonThree.setParent(container);
    horizontalLayout->addWidget(&buttonTwo);
    horizontalLayout->addWidget(&buttonThree);
    container->setLayout(horizontalLayout);

    stackLayout->addWidget(&buttonOne);
    stackLayout->addWidget(container);
    stackLayout->addWidget(&buttonFour);

    stackLayout->setCurrentIndex(0);

    setLayout(stackLayout);

    QTimer* timerObj = new QTimer();
    connect(timerObj, SIGNAL(timeout()), this, SLOT(handleTimeout()));
    timerObj->start(2000);
}

void Widget::handleTimeout()
{
    QStackedLayout* currentLayout = dynamic_cast<QStackedLayout*>(layout());

    if(currentLayout != NULL)
    {
        int nextIndex = (currentLayout->currentIndex() + 1) % currentLayout->count();
        currentLayout->setCurrentIndex(nextIndex);
    }
}

Widget::~Widget()
{
}

Widget.cpp

#include <QtWidgets/QApplication>
#include "Widget.h"

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    Widget mainWindow;
    mainWindow.show();

    return app.exec();
}

main.cpp

Summary

  • QStackedLayout manages interface components using a stacking approach
  • Only one component can be displayed at a time within QStackedLayout
  • Component visibility can be switched freely within QStackedLayout
  • QTimer serves as Qt's timing mechanism
  • QTimer triggers events at configurable intervals

Tags: Qt QStackedLayout QTimer gui layout management

Posted on Wed, 03 Jun 2026 17:38:00 +0000 by salami1_1