×
Namespaces

Variants
Actions
Revision as of 09:35, 3 September 2009 by croozeus (Talk | contribs)

Implementing a widget stack in Qt

From Nokia Developer Wiki
Jump to: navigation, search


Article Metadata
Tested with
Devices(s): S60 Emulator
Compatibility
Platform(s): S60 3rd Edition, FP1, FP2
S60 5th Edition
Symbian
Article
Keywords: QStackedWidget
Created: (09 Jul 2009)
Last edited: croozeus (03 Sep 2009)

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.

Preconditions


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

How to use QStackedLayout?

83 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.

×