Frage

In QT 4.7, I am trying to make one QTableWidgetItem in a QTableWidget Editable and the rest all columns should be read only for me. I am having problems here.

I have checked a number of samples through google and stackoverflow but failed to achieve this. Some of the options I tried are,

I create rows by calling insertRow(rownumber) for adding rows.

  1. Trial 1: I do the following while inserting a row dynamically

    • Enable Edit triggers in the UI Dialog
    • Add columns using the following code for disabling edit

      QTableWidgetItem qit(""); qit.setflags(qit.flags() & ~Qt::ItemIsEditable) qtable.setitem(row,column, &qit);

    • And for others columns I don't set the flags

    This above approach did not work. I am able edit all columns (even the one I negated the editable option)

  2. Trial 2: Do all the above with just qtable.setEditTriggers(Qt::NoEditTriggers) and then set the columns editable wherever required.

    But this option renders all columns non-editable.

But I don't see anyone complaining like this in any forums. So I must be making some stupid mistake.

Have someone come across such an issue, if yes please help by answering.

War es hilfreich?

Lösung

Working example of QTableWidget

First item in added row is editable, second one is not editable.

#include <QtGui>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    // Prepare layout
    QMainWindow *window = new QMainWindow;
    QTableWidget *tablewidget = new QTableWidget;
    window->setCentralWidget(tablewidget);

    // Add data
    tablewidget->insertRow(0);
    tablewidget->insertColumn(0);
    tablewidget->insertColumn(1);

    QTableWidgetItem *item;
    item = new QTableWidgetItem("editable");
    tablewidget->setItem(0,0,item);

    item = new QTableWidgetItem("non editable");
    item->setFlags(item->flags() & ~Qt::ItemIsEditable); // non editable
    tablewidget->setItem(0,1,item);

    window->show();
    return a.exec();
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top