PDA

View Full Version : Making a QTableWidget Editable



llemes4011
20th October 2010, 21:38
Hello,

I am working with a QTableWidget the user will enter data into, but I cannot get the cells to become editable. Right now I'm doing the following:


void MyClass::initTable(){
// Add Cell 1
QTableWidgetItem *item = new QTableWidgetItem;
item->setFlags(item->flags() & ~Qt::ItemIsEditable);
item->setData(Qt::DisplayRole, "(0,0)");
tableWidget->setItem(0,0,item);
// Add Cell 2
item = new QTableWidgetItem;
item->setFlags(item->flags() & ~Qt::ItemIsEditable);
item->setData(Qt::DisplayRole, "(1,0)");
tableWidget->setItem(1,0,item);

tableWidget->setEditTriggers(QAbstractItemView::AllEditTriggers );
}


This is just an example, but it shows what I'm trying to do. I also have an Editor that's being applied to QColor objects that are inserted into the table. I don't know if that would change how it functions, but I figured it wouldn't hurt to mention. It's the Editor that's straight out of the Qt Tutorials.

Also, every other time I've tried to enter data into a table, it has worked. but not for this project. I don't know if it's the current version of Qt or what...

Thanks =)

tbscope
21st October 2010, 05:21
Why do you set the items to not editable when you want them to be editable?

chris_helloworld
21st October 2010, 09:22
Try using

item->setFlags(item->flags() | Qt::ItemIsEditable);

Chris.

llemes4011
21st October 2010, 19:12
Hello again. I know it doesn't make sense to use ~Qt::ItemIsEditable, but it has worked in every other case. I tried I tried replacing the AND with an OR and removing the ~, but it still doesn't work. I tried installing an event filter on the object, but it doesn't seem to recognize my mouse clicks. I gets other events, but not QEvent::MouseButtonPress....

EDIT:
I figured it out. It was the ItemEditor that was eating the events.
I had the following code:


// EDITOR CALLS
QItemEditorFactory *factory = new QItemEditorFactory;

QItemEditorCreatorBase *colorListCreator =
new QStandardItemEditorCreator<ColorListEditor>();

factory->registerEditor(QVariant::Color, colorListCreator);

QItemEditorFactory::setDefaultFactory(factory);

And by doing that, it seemed to have been consuming the events. I have decided to remove the editor, and write a custom delegate to display the combo boxes in only specific columns.

Thanks for all your input =)