bool BaseClientTable::setData(int row, int column, QTableWidgetItem &item){ array[BaseClientTable::column * row + column] = item;}QVariant BaseClientTable::data(const QModelIndex& index, int role) const{ if(role == HierarchicalHeaderView::HorizontalHeaderDataRole) { QVariant var; var.setValue((QObject*)&horizontalHeaderModel); return var; } if(role == HierarchicalHeaderView::VerticalHeaderDataRole) { QVariant var; var.setValue((QObject*)&verticalHeaderModel); return var; } if(role == Qt::DisplayRole && index.isValid()) { return array[BaseClientTable::column*index.row() + index.column()]; } return QVariant();}
class BaseClientTable: public QAbstractTableModel{ Q_OBJECTpublic: BaseClientTable(int row, int column, int language, QObject* parent = 0); int rowCount(const QModelIndex& /*parent*/) const; int columnCount(const QModelIndex& /*parent*/) const; QVariant data(const QModelIndex& index, int role) const; bool setData(int row, int column, QString item); void changeHeader(unsigned short);private: QStandardItemModel horizontalHeaderModel; QStandardItemModel verticalHeaderModel; QString *str; int row; int column; int language; void init(); void fillHeaderHorizontal(QStandardItemModel& headerModel);};class BaseClientDelegate : public QItemDelegate{public: BaseClientDelegate(); QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const; void setEditorData(QWidget *editor, const QModelIndex &index) const; void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;};
ui->tableView->setItemDelegate(new BaseClientDelegate); ui->tableView->setModel(baseClient); ui->tableView->show();
QString fileName = QFileDialog::getOpenFileName(this,tr("Open File"),QDir::currentPath(),"Text File (*.csv)"); if(QFile(fileName).exists()) { ui->tableClient->close(); baseClient->openFile(fileName); ui->tableClient->show(); } else QMessageBox::information(this,tr("Ошибка"),"Файла не существует, проверьте имя файла !");
emit layoutChanged();
proxyModel.setSourceModel(baseClient); ui->tableClient->setItemDelegate(new BaseClientDelegate); ui->tableClient->setModel(&proxyModel);
hierarHeader->setSortIndicatorShown(true);
ui->tableClient->setSortingEnabled(true);
HierarchicalHeaderView::HierarchicalHeaderView(Qt::Orientation orientation, QWidget *parent) :QHeaderView(orientation, parent), _pd(new private_data()){ connect(this, SIGNAL(sectionResized(int, int, int)), this, SLOT(on_sectionResized(int))); connect(this, SIGNAL(sortIndicatorChanged(int,Qt::SortOrder)), this, SLOT(on_sortIndicatorChanged(int, Qt::SortOrder)));}HierarchicalHeaderView::~HierarchicalHeaderView(){ delete _pd;}void HierarchicalHeaderView::on_sortIndicatorChanged(int logicalIndex, Qt::SortOrder state){ if(state) sorrt = SortUp; else sorrt = SortDown; paintSection(new QPainter(), this->rect(), logicalIndex);}