Skip to content

Instantly share code, notes, and snippets.

@propella
Created June 16, 2011 23:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save propella/1030560 to your computer and use it in GitHub Desktop.
Save propella/1030560 to your computer and use it in GitHub Desktop.
Qt model-view test
#include "MyModel.h"
int main(int argc, char **argv)
{
QApplication app(argc, argv);
QSplitter widget;
QStringList colors;
colors << "Red" << "Orange" << "Yellow" << "Green" << "Blue" << "Purple";
MyModel model(colors);
QListView list(&widget);
QTreeView tree(&widget);
QTableView table(&widget);
QComboBox combo(&widget);
list.setModel(&model);
tree.setModel(&model);
table.setModel(&model);
combo.setModel(&model);
widget.show();
return app.exec();
}
SOURCES = MyModel.cpp main.cpp
HEADERS = MyModel.h
#include "MyModel.h"
// お約束として、コンストラクタで親オブジェクトを渡す。
// 親オブジェクトが削除されると子オブジェクトも削除されるしくみ。
MyModel::MyModel(const QStringList &strings, QObject *parent)
: QAbstractListModel(parent)
{
stringList = strings;
}
// 行数を返す
int MyModel::rowCount(const QModelIndex &) const
{
return stringList.count();
}
// 桁数を返す
int MyModel::columnCount(const QModelIndex &) const
{
return 2;
}
// 位置と種類に応じて適切なデータを返す
// index.column : データの位置を示す。
// role == Qt::DisplayRole : 文字列の表示に使うデータ
// role == Qt::EditRole : 文字列編集の初期値
// role == Qt::DecorationRole : アイコン(色や図形)
QVariant MyModel::data(const QModelIndex &index, int role) const
{
QString str = stringList.at(index.row());
QColor color = QColor(str);
if (index.column() == 0 && role == Qt::DecorationRole) return color;
if (index.column() == 0 && role == Qt::EditRole) return str;
if (index.column() == 0 && role == Qt::DisplayRole) return str;
if (index.column() == 1 && role == Qt::DisplayRole) return color.name();
return QVariant();
}
// 編集可能かどうかを答える。
Qt::ItemFlags MyModel::flags(const QModelIndex &index) const
{
if (index.column() == 0)
return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
else
return QAbstractItemModel::flags(index) | Qt::ItemIsEnabled;
}
// 編集が完了すると呼ばれる。
bool MyModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (role != Qt::EditRole) return false;
stringList.replace(index.row(), value.toString());
emit dataChanged(index, index);
return true;
}
#include <QtGui>
// QAbstractListModel のサブクラスを定義する。
class MyModel : public QAbstractListModel
{
// おまじない: Qt 拡張のシグナルやスロットが使えるようになる。
Q_OBJECT
public:
MyModel(const QStringList &strings, QObject *parent = 0);
int rowCount(const QModelIndex &parent = QModelIndex()) const;
int columnCount(const QModelIndex &parent = QModelIndex()) const;
QVariant data(const QModelIndex &index, int role) const;
Qt::ItemFlags flags(const QModelIndex &index) const;
bool setData(const QModelIndex &index, const QVariant &value,
int role = Qt::EditRole);
// 実際のデータはプライベート変数の stringList に格納
private:
QStringList stringList;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment