C++ (Qt)#include "tableModel.h"#include "tableRepository.h" #include <QtGui>#include <QFile>#include <QStringList>#include <QDebug>#include <Analysis_of_accidents.h> #define ADIITIONAL_ROWS 10 AccidentModelTable::AccidentModelTable(int rows, QWidget *parent) : QAbstractTableModel(parent), tableColumnCount(60), tableRowCount(rows){ p_parentObj = dynamic_cast<Analysis_of_accidents* > (parent); if (!p_parentObj) qDebug() << "Error cast in AccidentModelTable::AccidentModelTable"; p_tableDataRepositoryVector = new QVector<Data::TableDataRepository* > (tableRowCount); // fill all vector to default value "0" - it's indicate free space in vector p_tableDataRepositoryVector->fill(0); dataCounter = 0; tableTitle = new QStringList; qDebug() << "In Model"; connect(this, SIGNAL(dataCounterChanged()), this, SLOT(tableNeedRow()));} // ****** Realization Function for MVC int AccidentModelTable::rowCount(const QModelIndex &parent) const{ return tableRowCount;} int AccidentModelTable::columnCount(const QModelIndex &parent) const{ return tableColumnCount;} QVariant AccidentModelTable::data(const QModelIndex &index, int role) const{ if (!index.isValid()) return QVariant(); if (role == Qt::TextAlignmentRole) { return int(Qt::AlignCenter); } else if (role == Qt::DisplayRole && p_tableDataRepositoryVector->at(index.row())) { if (index.row() == p_tableDataRepositoryVector->at(index.row())->get_tablePlacementRowIdentifier()) return p_tableDataRepositoryVector->at(index.row())->At(index.column()); } return QVariant();} bool AccidentModelTable::setData(const QModelIndex &index, const QVariant &value, int role){ if (index.isValid() && role == Qt::EditRole) { qDebug() << "In setData Func"; emit dataChanged(index, index); return true; } return false;} Qt::ItemFlags AccidentModelTable::flags(const QModelIndex &index) const{ if (!index.isValid()) return Qt::ItemIsEnabled; return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;} QVariant AccidentModelTable::headerData(int section, Qt::Orientation orientation, int role) const{ if (role == Qt::DisplayRole) { if (orientation == Qt::Horizontal) return tableTitle->at(section); else return section + 1; } return QVariant();} // store Data Objectvoid AccidentModelTable::setDataObjectToVector(Data::TableDataRepository* dataObj){ // if Data in such row exists input a QMessage dialog if ((*p_tableDataRepositoryVector)[dataObj->get_tablePlacementRowIdentifier()]) { bool result = p_parentObj->IsToContinueRewrite(); // if user click rewrite destroy old object and put new, else return if (result) p_parentObj->destroyDataRepositoryObject((*p_tableDataRepositoryVector) [dataObj->get_tablePlacementRowIdentifier()]); else { p_parentObj->destroyDataRepositoryObject(dataObj); return; } } (*p_tableDataRepositoryVector)[dataObj->get_tablePlacementRowIdentifier()] = dataObj; incrementDataCounter(); // emits when data object counter changed emit dataCounterChanged(); qDebug() << "Data Counter" << getDataCounter(); reset(); return;} // remove Data Objectbool AccidentModelTable::ClearRow(int row){ // return from function if user click on empty row if (!(*p_tableDataRepositoryVector)[row]) return false; // delete current Object p_parentObj->destroyDataRepositoryObject((*p_tableDataRepositoryVector)[row]); (*p_tableDataRepositoryVector)[row] = 0; // update Table View emit dataChanged(index(row, 0), index(row, tableColumnCount - 1)); return true;} int AccidentModelTable::columnCount() const{ return tableColumnCount;} // operate with data object countervoid AccidentModelTable::incrementDataCounter(){ ++dataCounter;} void AccidentModelTable::decrementDataCounter(){ --dataCounter;} uint AccidentModelTable::getDataCounter(){ return dataCounter;} void AccidentModelTable::tableNeedRow(){ if (getDataCounter() >= tableRowCount - 3) insertRows(tableRowCount, ADIITIONAL_ROWS); qDebug() << "Vector size -> " << p_tableDataRepositoryVector->size();} bool AccidentModelTable::insertRows(int position, int rows, const QModelIndex &parent) { beginInsertRows(QModelIndex(), position, position+rows-1); // put ten 0 - pointers to the end of vector for (int row = 0; row < rows; ++row) p_tableDataRepositoryVector->append(0); endInsertRows(); // Add more rows to Table tableRowCount += rows; return true; }
C++ (Qt)#ifndef TABLEWIDGET_H#define TABLEWIDGET_H #include <tableRepository.h>#include <QAbstractTableModel> class QStringList;class Analysis_of_accidents; class AccidentModelTable : public QAbstractTableModel{ Q_OBJECT int tableRowCount; const int tableColumnCount; // pointer to Parent object of Analysis_of_accidents class Analysis_of_accidents* p_parentObj; // vector for storing TableDataRepository objects in it QVector<Data::TableDataRepository* >* p_tableDataRepositoryVector; // save count of filled rows uint dataCounter; // header titles QStringList* tableTitle; public: explicit AccidentModelTable(int rows, QWidget *parent = 0); //void clear(int rowCount); // **** Function which used in MVC int rowCount(const QModelIndex &parent) const; int columnCount(const QModelIndex &parent) const; QVariant data(const QModelIndex &index, int role) const; bool setData(const QModelIndex &index, const QVariant &value, int role); Qt::ItemFlags flags(const QModelIndex &index) const; QVariant headerData(int section, Qt::Orientation orientation, int role) const; // store Data Object in vector void setDataObjectToVector(Data::TableDataRepository* dataObj); // remove Data Object from vector bool ClearRow(int row); int columnCount() const; //increment or decrement object data counter void incrementDataCounter(); void decrementDataCounter(); uint getDataCounter(); // insert empty rows bool insertRows(int position, int rows, const QModelIndex &index = QModelIndex()); // Count of columns depending on Title int ColumnCount; private: enum {MagicNumber = 0x3F871A2}; private slots: void tableNeedRow(); //void somethingChanged(); signals: void dataCounterChanged(); //void modified(); }; #endif // TABLEWIDGET_H
QModelIndex MyModel::moveRow(const Movement &moveTo, const QModelIndex &idx) { int newRow = idx.row(); if (moveTo == moveUp && idx.row() != 0) newRow = idx.row()-1; if (moveTo == moveDown && idx.row() < this->rowCount()-1) newRow = idx.row()+1; QList<QStandardItem *> lst = this->takeRow(idx.row()); this->insertRow(newRow, lst); QModelIndex newIdx = this->index(newRow,idx.column()); return newIdx;}
void TableEditWindow::moveRow() { MyModel *m = qobject_cast<MyModel *>(ui->table_fields->model()); QModelIndex newIdx; if (sender()->objectName() == "btnUp") { newIdx = m->moveRow(moveUp, ui->table_fields->currentIndex()); } if (sender()->objectName() == "btnDown") { newIdx = m->moveRow(moveDown, ui->table_fields->currentIndex()); } ui->table_fields->setCurrentIndex(newIdx);}