Difference between revisions of "Qt/ModelView/AbstractTableModelCheckable"
From ProgrammingExamples
< Qt
Daviddoria (Talk | contribs) m (moved Broken/Qt/ModelView/AbstractTableModelCheckable to Qt/ModelView/AbstractTableModelCheckable: Fixed.) |
Daviddoria (Talk | contribs) (Needed to return something for the CheckStateRole in data()) |
||
| Line 24: | Line 24: | ||
#include <vector> | #include <vector> | ||
| + | |||
| + | class Item | ||
| + | { | ||
| + | public: | ||
| + | Item(const float v, const Qt::CheckState s) {value = v; state = s;} | ||
| + | float value; | ||
| + | Qt::CheckState state; | ||
| + | }; | ||
class MyTableModel : public QAbstractTableModel | class MyTableModel : public QAbstractTableModel | ||
| Line 29: | Line 37: | ||
public: | public: | ||
MyTableModel(); | MyTableModel(); | ||
| − | + | ||
int rowCount(const QModelIndex& parent) const; | int rowCount(const QModelIndex& parent) const; | ||
int columnCount(const QModelIndex& parent) const; | int columnCount(const QModelIndex& parent) const; | ||
| Line 36: | Line 44: | ||
Qt::ItemFlags flags(const QModelIndex& index) const; | Qt::ItemFlags flags(const QModelIndex& index) const; | ||
bool setData(const QModelIndex & index, const QVariant & value, int role); | bool setData(const QModelIndex & index, const QVariant & value, int role); | ||
| − | |||
| − | |||
| − | |||
protected: | protected: | ||
| − | std::vector<std::vector< | + | std::vector<std::vector<Item> > Columns; |
}; | }; | ||
| Line 56: | Line 61: | ||
MyTableModel::MyTableModel() : QAbstractTableModel() | MyTableModel::MyTableModel() : QAbstractTableModel() | ||
{ | { | ||
| − | std::vector< | + | std::vector<Item> column1; |
| − | column1.push_back( | + | Item item(10, Qt::Checked); |
| − | column1.push_back( | + | column1.push_back(item); |
| − | column1.push_back( | + | item.value = 20; item.state = Qt::Checked; |
| − | column1.push_back( | + | column1.push_back(item); |
| + | item.value = 30; item.state = Qt::Checked; | ||
| + | column1.push_back(item); | ||
| + | item.value = 40; item.state = Qt::Checked; | ||
| + | column1.push_back(item); | ||
Columns.push_back(column1); | Columns.push_back(column1); | ||
| − | std::vector< | + | std::vector<Item> column2; |
| − | column2.push_back( | + | item.value = 50; item.state = Qt::Unchecked; |
| − | column2.push_back( | + | column2.push_back(item); |
| − | column2.push_back( | + | item.value = 60; item.state = Qt::Unchecked; |
| − | column2.push_back( | + | column2.push_back(item); |
| + | item.value = 70; item.state = Qt::Unchecked; | ||
| + | column2.push_back(item); | ||
| + | item.value = 80; item.state = Qt::Unchecked; | ||
| + | column2.push_back(item); | ||
Columns.push_back(column2); | Columns.push_back(column2); | ||
| Line 89: | Line 102: | ||
if(role == Qt::DisplayRole) | if(role == Qt::DisplayRole) | ||
{ | { | ||
| − | return Columns[index.column()][index.row()]; | + | return Columns[index.column()][index.row()].value; |
| + | } | ||
| + | if (index.column() == 1 && role == Qt::CheckStateRole) | ||
| + | { | ||
| + | return this->Columns[index.column()][index.row()].state; | ||
} | } | ||
return QVariant::Invalid; | return QVariant::Invalid; | ||
| Line 96: | Line 113: | ||
QVariant MyTableModel::headerData(int section, Qt::Orientation orientation, int role) const | QVariant MyTableModel::headerData(int section, Qt::Orientation orientation, int role) const | ||
{ | { | ||
| − | |||
if(role == Qt::DisplayRole) | if(role == Qt::DisplayRole) | ||
{ | { | ||
| Line 110: | Line 126: | ||
return QString(ss.str().c_str()); | return QString(ss.str().c_str()); | ||
} | } | ||
| − | |||
} | } | ||
| − | + | ||
return QVariant::Invalid; | return QVariant::Invalid; | ||
} | } | ||
| Line 119: | Line 134: | ||
bool MyTableModel::setData(const QModelIndex & index, const QVariant & value, int role) | bool MyTableModel::setData(const QModelIndex & index, const QVariant & value, int role) | ||
{ | { | ||
| − | + | if (index.column() == 1 && role == Qt::CheckStateRole) | |
| + | { | ||
| + | this->Columns[index.column()][index.row()].state = static_cast<Qt::CheckState>(value.toUInt()); | ||
| + | } | ||
| + | |||
| + | emit dataChanged(index, index); | ||
| + | return true; | ||
} | } | ||
| Line 125: | Line 146: | ||
{ | { | ||
Qt::ItemFlags returnFlags = QAbstractTableModel::flags(index); | Qt::ItemFlags returnFlags = QAbstractTableModel::flags(index); | ||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | + | if (index.column() == 1) | |
| − | + | ||
| − | + | ||
{ | { | ||
| − | + | returnFlags |= Qt::ItemIsUserCheckable; | |
| − | + | ||
} | } | ||
| − | + | return returnFlags; | |
| − | + | ||
} | } | ||
| Line 159: | Line 168: | ||
class Form : public QWidget, public Ui::Form | class Form : public QWidget, public Ui::Form | ||
{ | { | ||
| − | + | Q_OBJECT | |
public: | public: | ||
| − | + | Form(QWidget *parent = 0); | |
| − | + | ||
| − | + | ||
| − | + | ||
protected: | protected: | ||
| Line 188: | Line 194: | ||
{ | { | ||
setupUi(this); | setupUi(this); | ||
| − | + | ||
this->model = new MyTableModel; | this->model = new MyTableModel; | ||
this->tableView->setModel(model); | this->tableView->setModel(model); | ||
| − | + | ||
this->tableView->resizeColumnsToContents(); | this->tableView->resizeColumnsToContents(); | ||
| − | |||
} | } | ||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
</source> | </source> | ||
Latest revision as of 08:17, 29 December 2011
Contents
main.cpp
#include <QApplication> #include "form.h" int main(int argc, char *argv[]) { QApplication app(argc, argv); Form form; form.show(); return app.exec(); }
MyTableModel.h
#ifndef MyTableModel_H #define MyTableModel_H #include <QAbstractTableModel> #include <vector> class Item { public: Item(const float v, const Qt::CheckState s) {value = v; state = s;} float value; Qt::CheckState state; }; class MyTableModel : public QAbstractTableModel { public: MyTableModel(); int rowCount(const QModelIndex& parent) const; int columnCount(const QModelIndex& parent) const; QVariant data(const QModelIndex& index, int role) const; QVariant headerData(int section, Qt::Orientation orientation, int role) const; Qt::ItemFlags flags(const QModelIndex& index) const; bool setData(const QModelIndex & index, const QVariant & value, int role); protected: std::vector<std::vector<Item> > Columns; }; #endif
MyTableModel.cpp
#include "MyTableModel.h" #include <sstream> MyTableModel::MyTableModel() : QAbstractTableModel() { std::vector<Item> column1; Item item(10, Qt::Checked); column1.push_back(item); item.value = 20; item.state = Qt::Checked; column1.push_back(item); item.value = 30; item.state = Qt::Checked; column1.push_back(item); item.value = 40; item.state = Qt::Checked; column1.push_back(item); Columns.push_back(column1); std::vector<Item> column2; item.value = 50; item.state = Qt::Unchecked; column2.push_back(item); item.value = 60; item.state = Qt::Unchecked; column2.push_back(item); item.value = 70; item.state = Qt::Unchecked; column2.push_back(item); item.value = 80; item.state = Qt::Unchecked; column2.push_back(item); Columns.push_back(column2); } int MyTableModel::rowCount(const QModelIndex& parent) const { return Columns[0].size(); } int MyTableModel::columnCount(const QModelIndex& parent) const { return Columns.size(); } QVariant MyTableModel::data(const QModelIndex& index, int role) const { if(role == Qt::DisplayRole) { return Columns[index.column()][index.row()].value; } if (index.column() == 1 && role == Qt::CheckStateRole) { return this->Columns[index.column()][index.row()].state; } return QVariant::Invalid; } QVariant MyTableModel::headerData(int section, Qt::Orientation orientation, int role) const { if(role == Qt::DisplayRole) { std::stringstream ss; if(orientation == Qt::Horizontal) { ss << "H_" << section; return QString(ss.str().c_str()); } else if(orientation == Qt::Vertical) { ss << "V_" << section; return QString(ss.str().c_str()); } } return QVariant::Invalid; } bool MyTableModel::setData(const QModelIndex & index, const QVariant & value, int role) { if (index.column() == 1 && role == Qt::CheckStateRole) { this->Columns[index.column()][index.row()].state = static_cast<Qt::CheckState>(value.toUInt()); } emit dataChanged(index, index); return true; } Qt::ItemFlags MyTableModel::flags(const QModelIndex& index) const { Qt::ItemFlags returnFlags = QAbstractTableModel::flags(index); if (index.column() == 1) { returnFlags |= Qt::ItemIsUserCheckable; } return returnFlags; }
form.h
#ifndef FORM_H #define FORM_H #include "ui_form.h" #include "MyTableModel.h" class Form : public QWidget, public Ui::Form { Q_OBJECT public: Form(QWidget *parent = 0); protected: MyTableModel* model; }; #endif
form.cpp
#include <QAbstractTableModel> #include <QtGui> #include <iostream> #include <sstream> #include "form.h" Form::Form(QWidget *parent) : QWidget(parent) { setupUi(this); this->model = new MyTableModel; this->tableView->setModel(model); this->tableView->resizeColumnsToContents(); }
form.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Form</class>
<widget class="QWidget" name="Form">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<widget class="QTableView" name="tableView">
<attribute name="horizontalHeaderStretchLastSection">
<bool>true</bool>
</attribute>
</widget>
</item>
<item row="1" column="0">
<widget class="QPushButton" name="btnUpdate">
<property name="text">
<string>Update</string>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>CMakeLists.txt
cmake_minimum_required(VERSION 2.6) PROJECT(AbstractTableModelCheckable) FIND_PACKAGE(Qt4 REQUIRED) INCLUDE(${QT_USE_FILE}) QT4_WRAP_UI(UISrcs form.ui) QT4_WRAP_CPP(MOCSrcs form.h) include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR}) ADD_EXECUTABLE(AbstractTableModelCheckable main.cpp MyTableModel.cpp form.cpp ${MOCSrcs} ${UISrcs}) TARGET_LINK_LIBRARIES(AbstractTableModelCheckable ${QT_LIBRARIES})