PDA

View Full Version : A QListWidget in a QTableWidget cell ?



Nyphel
11th April 2007, 10:47
Hello,

I would like to add a QListWidget in a QTableWidget cell.
I know how to add checkboxes (simply set the cell item checked or unchecke, like this : Item_0->setCheckState(Qt::Checked); ) but I don't understand how to add other QListWidgets. For adding checkboxes, we simply modify the flags value of the QtableWidgetItem in the cell... But there isn't any flags concerning QlistWidgets.

Reading the QTableWidgetItem (http://doc.trolltech.com/4.2/qtablewidgetitem.html) documentation, they say :

Subclassing
When subclassing QTableWidgetItem to provide custom items, it is possible to define new types for them so that they can be distinguished from standard items. The constructors for subclasses that require this feature need to call the base class constructor with a new type value equal to or greater than UserType.
See also QTableWidget, Model/View Programming, QListWidgetItem, and QTreeWidgetItem.
And here (http://doc.trolltech.com/4.2/qtablewidgetitem.html#ItemType-enum) can be found the UserType documentation.

Is it the slution for adding QListWidgets in my QTableItems ?
If yes, could you explain me a bit more, please ? I don't understad very well what does it means :(

Thanks for your help :)





PS : Working code example :

void Table_impl::SLOT_add_row()
{
QListWidget *A_listWidget;
A_listWidget = new QListWidget(table);

QListWidgetItem *__item = new QListWidgetItem(A_listWidget);
__item->setText("Bla bla bla...");

table->setRowCount(table->rowCount() + 1);

QTableWidgetItem *Item_0 = new QTableWidgetItem();
QTableWidgetItem *Item_1 = new QTableWidgetItem("");
QTableWidgetItem *Item_2 = new QTableWidgetItem("");
QTableWidgetItem *Item_3 = new QTableWidgetItem("");

table->setItem(table->rowCount()-1, 0, Item_0);
//table->setItem(table->rowCount()-1, 1, Item_1);
table->setCellWidget(table->rowCount()-1, 1, A_listWidget);
table->setItem(table->rowCount()-1, 2, Item_2);
table->setItem(table->rowCount()-1, 3, Item_3);

Item_0->setCheckState(Qt::Unchecked);
Item_0->setFlags(Item_0->flags() & (~(Qt::ItemIsEditable)));
Item_1->setFlags(Item_1->flags() & (~(Qt::ItemIsEditable)));
Item_2->setFlags(Item_2->flags() & (~(Qt::ItemIsEditable)));

this->resize(table->width() + 4, this->height() + table->rowHeight(table->rowCount()-1));
}


.

high_flyer
11th April 2007, 11:26
Is it the slution for adding QListWidgets in my QTableItems ?
If yes, could you explain me a bit more, please ? I don't understad very well what does it means
It means, you need to subclass QTableWidgetItem, and give the constructor a user type.


ListTableItem: public QTableWidgetItem
{
//Where 'ListItem' is a constant defined somewhere like #define ListItem 100
ListTableItem(int type = QTableWidgetItem::UserType + ListItem):QTableWidgetItem(type){}
///The rest of the class
};

Nyphel
11th April 2007, 11:31
Ok, thanks, but I don't understand thoses UserType...

Trolltech seems to reserve the Types from 0 to 1000.
Why ? Is there 1000 deffirent base types in Qt ? :p

high_flyer
11th April 2007, 11:42
Ok, thanks, but I don't understand thoses UserType...
Its just a constant value, through which a type is known throughout the framework.


Why ? Is there 1000 deffirent base types in Qt ?
Probably not, but since there is no shortage on ints, why not reserve 1000?
It leaves more then enough for your user types, surely you will not need int range-1000 types of your own. ;)

Nyphel
11th April 2007, 11:46
Huhu hanks for thoses hints :)