Domanda

I am working on QTableView inside a QStandardItemModel. I am using the QTextEdit class inside a class derived from a QItemDelegate. It includes text edit inside cells of every column see the attached picture.

Is it possible to exclude tableView, Third column (all cells) from including this text edit ? Means I do not want this text edit inside 3rd column cells.

Here is my initialising code for tableview :--

//Set model & deligate
ui->testCaseTableView->setModel(model);
ui->testCaseTableView->setItemDelegate(mydeligate);

Here is my code for the deligate :---

QWidget* textViewDeligate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{

    QTextEdit *tableEdit = new QTextEdit(parent);
    return tableEdit;
}

void textViewDeligate::setEditorData ( QWidget * editor, const QModelIndex & index ) const
{

    QString value = index.model()->data(index,Qt::EditRole).toString();

    QTextEdit *tableEditCopy = static_cast<QTextEdit*>(editor);
    tableEditCopy->setPlainText(value);
}

void    textViewDeligate::setModelData ( QWidget * editor, QAbstractItemModel * model, const QModelIndex & index ) const
{
    QTextEdit *tableEditCopy = static_cast<QTextEdit*>(editor);
    QString str = tableEditCopy->toPlainText();

    model->setData(index, str, Qt::EditRole);

}

void    textViewDeligate::updateEditorGeometry ( QWidget *editor, const QStyleOptionViewItem & option, const QModelIndex & index ) const
{

}

enter image description here

È stato utile?

Soluzione 2

You can use QAbstractItemView::setItemDelegateForColumn method to disable the delegate in a column:

ui->testCaseTableView->setItemDelegate(mydelegate);
ui->testCaseTableView->setItemDelegateForColumn(2, 0);

Altri suggerimenti

To exclude third column from having QTextEdit as editor widget you can do the following:

QWidget* textViewDeligate::createEditor(QWidget *parent,
                                        const QStyleOptionViewItem &option,
                                        const QModelIndex &index) const
{
    if (index.column() != 2) {
        // QTextEdit as editor for all columns but third.
        QTextEdit *tableEdit = new QTextEdit(parent);
        return tableEdit;
    } else {
        // Or return 0 to prevent editing this.
        return QItemDelegate::createEditor(parent, option, index);
    }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top