Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2647365
  • 博文数量: 416
  • 博客积分: 10220
  • 博客等级: 上将
  • 技术积分: 4193
  • 用 户 组: 普通用户
  • 注册时间: 2006-12-15 09:47
文章分类

全部博文(416)

文章存档

2022年(1)

2021年(1)

2020年(1)

2019年(5)

2018年(7)

2017年(6)

2016年(7)

2015年(11)

2014年(1)

2012年(5)

2011年(7)

2010年(35)

2009年(64)

2008年(48)

2007年(177)

2006年(40)

我的朋友

分类: C/C++

2009-04-21 14:37:24

url:
=========== qitemchooserdialog.cpp ===========


#include "qitemchooserdialog.h"

#include "stringlistmodel.h"

#include
#include
#include
#include
#include
#include
#include

#include

QItemChooserDialog::QItemChooserDialog(QWidget *parent)
    : QDialog(parent)
{
    comboBox = new QComboBox(parent);
    QVBoxLayout *layout = new QVBoxLayout;
    layout->addWidget(comboBox);
    setLayout(layout);


    // Comment this block and uncomment the next one to try this with a tree
system.
    StringListModel* model = new StringListModel(QStringList() << "one"
<< "two" << "three");
    QListView* view = new QListView();
    view->setModel(model);

//     QDirModel* model = new QDirModel();
//     QTreeView* view = new QTreeView();
//     view->setModel(model);
//     view->setRootIndex(model->index(QDir::currentPath()));
//     view->expandAll();

    comboBox->setModel(model);
    comboBox->setView(view);


    // This index will really be passed into the dialog.
    QModelIndex index = model->index(1, 0);

    comboBox->view()->setCurrentIndex(index);

    // Shouldn't be neccessary?
    comboBox->setCurrentIndex(view->currentIndex().row());

    connect(comboBox, SIGNAL(activated( int )), this, SLOT(showItemIndex(
int )));

}

void QItemChooserDialog::showItemIndex( int ) {
    qDebug() << comboBox->view()->currentIndex();
}


=========== qitemchooserdialog.h ===========

#ifndef QITEMCHOOSERDIALOG_H
#define QITEMCHOOSERDIALOG_H

#include
#include

class QComboBox;

QT_FORWARD_DECLARE_CLASS(QComboBox)

class QItemChooserDialog : public QDialog
{
    Q_OBJECT

public:
    QItemChooserDialog(QWidget *parent = 0);

public slots:
    void showItemIndex( int );

private:
    QComboBox *comboBox;
};

#endif

=========== stringlistmodel.cpp ===========

#include "stringlistmodel.h"


#include
#include
#include

int StringListModel::rowCount(const QModelIndex &parent) const
{
    return stringList.count();
}

QVariant StringListModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid())
        return QVariant();

    if (index.row() >= stringList.size())
        return QVariant();

    if (role == Qt::DisplayRole)
        return stringList.at(index.row());
    else
        return QVariant();
}

QVariant StringListModel::headerData(int section, Qt::Orientation
orientation,
                                     int role) const
{
    if (role != Qt::DisplayRole)
        return QVariant();

    if (orientation == Qt::Horizontal)
        return QString("Column %1").arg(section);
    else
        return QString("Row %1").arg(section);
}

=========== stringlistmodel.h ===========

#include
#include
#include

class StringListModel : public QAbstractListModel
{
    Q_OBJECT

public:
    StringListModel(const QStringList &strings, QObject *parent = 0)
        : QAbstractListModel(parent), stringList(strings) {}

    int rowCount(const QModelIndex &parent = QModelIndex()) const;
    QVariant data(const QModelIndex &index, int role) const;
    QVariant headerData(int section, Qt::Orientation orientation,
                        int role = Qt::DisplayRole) const;

private:
    QStringList stringList;
};

=========== main.cpp ===========

#include "qitemchooserdialog.h"
#include

int main( int argc, char ** argv )
{

    QApplication a( argc, argv );
    QItemChooserDialog d;
    d.show();
    return a.exec();
}

阅读(1968) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~