Namespaces

Variants
Actions

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

Implementing a widget stack in Qt

From Wiki
Jump to: navigation, search
Article Metadata
Tested with
Devices(s): S60 Emulator
Compatibility
Platform(s): Qt
Symbian
Article
Keywords: QStackedWidget
Created: mind_freak (09 Jul 2009)
Last edited: hamishwillee (11 Oct 2012)

Contents

Introduction

The QStackedWidget class provides a widget stack where only one widget is visible at a time. QStackedWidget provides no intrinsic means for the user to switch page.

Source Code

Header File

#ifndef STACK_H
#define STACK_H
 
#include <QtGui/QWidget>
#include<QWidget>
#include<QStackedWidget>
#include<QComboBox>
#include<QVBoxLayout>
#include<QPushButton>
class stack : public QWidget
{
Q_OBJECT
 
public:
stack(QWidget *parent = 0);
~stack();
 
private:
QPushButton *but;
QPushButton *but1;
QPushButton *but2;
QVBoxLayout *lay;
QVBoxLayout *lay1;
QVBoxLayout *lay2;
};
 
#endif // STACK_H

Source File

#include "stack.h"
stack::stack(QWidget *parent)
: QWidget(parent)
{
setStyleSheet("* { background-color:rgb(190,88,88);color:rgb(255,255,255); padding: 7px }}");
 
QWidget *firstPageWidget = new QWidget(this);//First Widget
lay=new QVBoxLayout(this);
but=new QPushButton("First Widget",this);
lay->addWidget(but);
firstPageWidget->setLayout(lay);
 
QWidget *secondPageWidget = new QWidget(this);//Second Widget
lay1=new QVBoxLayout(this);
but1=new QPushButton("second Widget",this);
lay1->addWidget(but1);
secondPageWidget->setLayout(lay1);
 
 
QWidget *thirdPageWidget = new QWidget(this);//Third Widget
lay2=new QVBoxLayout(this);
but2=new QPushButton("Third Widget",this);
lay2->addWidget(but2);
thirdPageWidget->setLayout(lay2);
 
 
QComboBox *pageComboBox = new QComboBox(this);
QStackedWidget *stackedWidget = new QStackedWidget(this);
stackedWidget->addWidget(firstPageWidget);
stackedWidget->addWidget(secondPageWidget);
stackedWidget->addWidget(thirdPageWidget);
 
 
pageComboBox->addItem(tr("Page 1"));
pageComboBox->addItem(tr("Page 2"));
pageComboBox->addItem(tr("Page 3"));
connect(pageComboBox, SIGNAL(activated(int)),stackedWidget, SLOT(setCurrentIndex(int)));
 
QVBoxLayout *layout = new QVBoxLayout(this);
 
layout->addWidget(stackedWidget);
layout->addWidget(pageComboBox);
setLayout(layout);
}
 
stack::~stack()
{
// No need to delete any object that got a parent that is properly deleted.
}

Screenshot

Stackwin.JPG

Stackwind.JPG

Related Article

Archived:How to use QStackedLayout

This page was last modified on 11 October 2012, at 01:17.
79 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×