View Full Version : QTableWidget
rick_st3
24th June 2008, 11:12
Hi..
Can i create a table with certain cells with a "read only" property...??? i did look up the documentation of QTableWidget but did not find any answers... Any clues anyone...?? thanks...
munna
24th June 2008, 11:21
Each cell of QTableWidget is an object of QTableWidgetItem. You can use setFlags function of QTableWidgetItem class to make the cell read only.
rick_st3
24th June 2008, 11:28
thanks mate.... and also i wanted to know whether i can include a spinbox or a combobox in a cell...
munna
24th June 2008, 11:30
void setCellWidget ( int row, int column, QWidget * widget )
Spin Box Delegate Example (http://doc.trolltech.com/4.4/itemviews-spinboxdelegate.html)
rick_st3
25th June 2008, 07:03
got it..!! thanks all...!!
rick_st3
25th June 2008, 08:40
Each cell of QTableWidget is an object of QTableWidgetItem. You can use setFlags function of QTableWidgetItem class to make the cell read only.
i cannot set the flags off...!!! :crying: please help...!!!
i cannot set the flags off...!!! :crying: please help...!!!
Could you show us how you try to set them off?
rick_st3
25th June 2008, 09:27
Could you show us how you try to set them off?
thats exactly what i cannot think of..!! :crying:
I checked the documentation for setting the flag values but the problem is all of them are for enabling them...none of them are to disable them...!! i tried
exht->setFlags(Qt::ItemIsEditable(false)); and
exht->setFlags(Qt::ItemIsEditable = 0); and all possible combinations i could think of(Now i don't know if the code is right or no) but its just not helping...!!!:crying: please help...!!!
Use bitwise operators:
item->setFlags(item->flags() & ~Qt::ItemIsEditable);
rick_st3
25th June 2008, 14:09
Use bitwise operators:
item->setFlags(item->flags() & ~Qt::ItemIsEditable);
guru indeed...!!! thanks a ton mate...!!:)
rick_st3
26th June 2008, 10:39
Spin Box Delegate Example (http://doc.trolltech.com/4.4/itemviews-spinboxdelegate.html)
i went through the tutorial...and now i can include a spinbox or a doublespinbox using delegates... but now what i cannot understand is what if i wanted to include the spinbox(or any other widget) in just a few cells(keeping the other cells normal) instead of the all the cells..??? will have to make another delegate to implement the QTableWidgetItem ...?? or is there any way i can apply the delegate to just a few cells instead of making a model of the entire table and then including the delegates..??? please advice... thanks...
There are several ways to achieve that. One extremely simple but not that dynamic way would be to check the column and row number in createEditor(). For example:
QWidget* MyItemDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
if (index.column() == 3 && index.row() > 2) {
...
return customEditor;
}
return 0; // or return QItemDelegate::createEditor(parent, option, index);
}
A bit more flexible way would be to use user roles to store the information:
tableWidgetItem->setData(Qt::UserRole, true);
QWidget* MyItemDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
if (index.data(Qt::UserRole).toBool() == true) {
...
return customEditor;
}
return 0; // or return QItemDelegate::createEditor(parent, option, index);
}
You can also consider using item/cell widgets as already suggested, but just be aware that they don't really integrate to the model-view framework. They're just something laid by hand on top of views.
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.