Fork me on GitHub

Qt之布局管理——堆栈窗体

QStackedWidget类继承于QFrame类,用于堆栈窗体的管理。

Public Slots:

void setCurrentIndex( int index )            index参数的索引位置的部件,是可见的。(index默认为-1,表示堆栈为空)

void setCurrentWidget( QWidget * widget )        widget参数指定的部件,是可见的。


实现效果:

image

maindlg.h

#ifndef MAINDLG_H
#define MAINDLG_H

#include <QtGui>


class MainDlg : public QDialog
{
	Q_OBJECT

public:
	MainDlg(QWidget *parent = 0, Qt::WFlags flags = 0);
	~MainDlg();

private:
	QLabel *lab1;
	QLabel *lab2;
	QLabel *lab3;
	QListWidget *m_list;
	QStackedWidget *stack;
};

#endif // MAINDLG_H

maindlg.cpp

#include "maindlg.h"


MainDlg::MainDlg(QWidget *parent, Qt::WFlags flags)
	: QDialog(parent, flags)
{
	setWindowTitle(tr("Stacked Widgets example"));

	//左部的列表框:
	m_list = new QListWidget;
	m_list->insertItem(0,tr("Window 1"));
	m_list->insertItem(1,tr("Window 2"));
	m_list->insertItem(2,tr("Window 3"));

	//右部的堆栈窗:
	stack = new QStackedWidget(this);
	lab1 = new QLabel(tr("This is window 1"));
	stack->addWidget(lab1);
	lab2 = new QLabel(tr("This is window 2"));
	stack->addWidget(lab2);
	lab3 = new QLabel(tr("This is window 3"));
	stack->addWidget(lab3);	

	//布局;
	QHBoxLayout *mainLayout = new QHBoxLayout(this);
	mainLayout->addWidget(m_list);
	mainLayout->addWidget(stack,0,Qt::AlignHCenter);
	mainLayout->setMargin(5);
	mainLayout->setSpacing(5);
	mainLayout->setStretchFactor(m_list,1);
	mainLayout->setStretchFactor(stack,3);
	connect(m_list,SIGNAL(currentRowChanged(int)),stack,SLOT(setCurrentIndex(int)));

}

MainDlg::~MainDlg()
{

}

posted @ 2012-11-17 09:08  韩兆新  阅读(7851)  评论(0编辑  收藏  举报