分类: 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();
}