void TableViewWidget::moveRow(bool up) { if (!m_model) { return; } const int sourceRow = currentIndex().row(); const int destinationRow = (up ? (sourceRow - 1) : (sourceRow + 1)); if ((up && sourceRow > 0) || (!up && sourceRow < (m_model->rowCount() - 1))) { m_model->insertRow(sourceRow, m_model->takeRow(destinationRow)); setCurrentIndex(getIndex(destinationRow, 0)); notifySelectionChanged(); emit modified(); } }
if (!model) { return; } model->setParent(this); if (model->inherits(QStringLiteral("QStandardItemModel").toLatin1())) { m_model = qobject_cast<QStandardItemModel*>(model); connect(m_model, SIGNAL(itemChanged(QStandardItem*)), this, SLOT(notifySelectionChanged())); } connect(selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)), this, SLOT(notifySelectionChanged())); connect(model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SIGNAL(modified())); } QStandardItemModel* ItemViewWidget::getModel() { return m_model; } QStandardItem* ItemViewWidget::getItem(int row, int column) const { return(m_model ? m_model->item(row, column) : NULL); } QModelIndex ItemViewWidget::getIndex(int row, int column) const {
void TableViewWidget::setModel(QAbstractItemModel *model) { QTableView::setModel(model); if (!model) { return; } model->setParent(this); if (model->inherits(QStringLiteral("QStandardItemModel").toLatin1())) { m_model = qobject_cast<QStandardItemModel*>(model); } connect(selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)), this, SLOT(notifySelectionChanged())); connect(model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SIGNAL(modified())); }
void TableViewWidget::setModel(QAbstractItemModel *model) { QTableView::setModel(model); if (model->inherits(QStringLiteral("QStandardItemModel").toLatin1())) { m_model = qobject_cast<QStandardItemModel*>(model); } connect(selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)), this, SLOT(notifySelectionChanged())); }