QT 利用ListWidget 和 StackedLayout 配合实现 分页 选项

1. 如图, 左边为listwidget,右边为StackedLayout, 通过listwidget的不同选项,可以使右边的不同页显示出来。

 

2. dialog.h

#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>
#include <QtGui>

namespace Ui {
class Dialog;
}

class Dialog : public QDialog
{
    Q_OBJECT
    
public:
    explicit Dialog(QWidget *parent = 0);
    ~Dialog();
    
private:
    Ui::Dialog *ui;

    QListWidget *listWidget;
    QStackedLayout *stackedLayout;
    QHBoxLayout *mainLayout;

    QLabel *label1;
    QLabel *label2;
    QLabel *label3;

};

#endif // DIALOG_H

 

3. dialog.cpp

#include "dialog.h"
#include "ui_dialog.h"

Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    ui->setupUi(this);

    setWindowTitle("test stackedlayout");
    mainLayout = new QHBoxLayout;

    label1 = new QLabel("label1");
    label2 = new QLabel("label2");
    label3 = new QLabel("label3");

    listWidget = new QListWidget;
    listWidget->addItem(tr("item1"));
    listWidget->addItem(tr("item2"));
    listWidget->addItem(tr("item3"));

    stackedLayout = new QStackedLayout;
    stackedLayout->addWidget(label1);
    stackedLayout->addWidget(label2);
    stackedLayout->addWidget(label3);

    connect(listWidget, SIGNAL(currentRowChanged(int)),
            stackedLayout, SLOT(setCurrentIndex(int)));
    listWidget->setCurrentRow(0);

    mainLayout->addWidget(listWidget);
    mainLayout->addLayout(stackedLayout);

    setLayout(mainLayout);
}

Dialog::~Dialog()
{
    delete ui;
}



 

posted @ 2013-08-15 15:22  今晚打酱油_  阅读(2537)  评论(0编辑  收藏  举报