In the previous article we used Setcellwidget to help us with editing in the table, and it was a bit tedious when there were many lines that needed the same action. Especially when signals/slots are needed. Fortunately, QT offers us a much bigger approach: delegate (commissioned, also translated as proxies). So how do we do that?
First derive one of your own classes from the item proxy class. Such as:
Class Mytableitemdelegate:public Qstyleditemdelegate
Then rewrite the Createeditor function:
if (index.column () = = 3)
{
qcombobox* CBX = new Qcombobox (parent);
Cbx->additem (Qstringliteral ("male"));
Cbx->additem (Qstringliteral ("female"));
return CBX;
}else{
return Qstyleditemdelegate::createeditor (parent,option,index);
}
Determine if the current 4th column (subscript 3) Creates a drop-down box and adds two item and then returns, and the other columns are processed using the default method. Finally let the table use this proxy:
Ui->tablewidget->setitemdelegate (new mytableitemdelegate);
We see the same effect as using Setcellwidget, but in a way that's a little more concise. Not only can we set the proxy for the table, but we can also set the proxy for the specified row or column.
void setitemdelegateforcolumn (int column, qabstractitemdelegate *delegate)//set agent void Setitemdelegateforrow for the column
( int row, Qabstractitemdelegate *delegate//set proxy for line
If you use Ui->tablewidget->setitemdelegateforcolumn (3,new mytableitemdelegate) above. Then there is no need to judge the current column in the Createeditor function.
The story is not over yet, if we use the picture to replace the text in the drop-down box, we will find a problem:
if (index.column () = = 3)
{
qcombobox* CBX = new Qcombobox (parent);
Cbx->additem (Qicon ("f:/05_test/tx.jpg"), ""); Show only picture
Cbx->additem (Qicon ("f:/05_test/tx2.jpg"), "");
return CBX;
}else{
return Qstyleditemdelegate::createeditor (parent,option,index);
}
The contents of the table are empty after the drop-down box is selected. In fact, there is another problem, that is, when you set the contents of the table, the contents of the dropdown box will not be changed. In other words, the cell and the editor content created are not synchronized.
When this happens, we also need to rewrite two functions to manually match the data on both sides:
Virtual void setmodeldata (Qwidget *editor, qabstractitemmodel *model, const qmodelindex &index) const;//Set the data//virtual void seteditordata for the model (QWidget * Editor, const qmodelindex &index) const; //Set the editor's data void mytableitemdelegate: : Setmodeldata (qwidget *editor, qabstractitemmodel *model, const qmodelindex &index) const { if (Index.column () == 3) {
QComboBox* cbx = qobject_cast<QComboBox*> (editor); if (Cbx->currentindex () == 0) {
model->setdata (Index,qstringliteral ("male")); }else{ model->setdata (index,qStringLiteral ("female")); } }} void mytableitemdelegate:: Seteditordata (Qwidget *editor, const qmodelindex &index) const { if (Index.column () == 3) {&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;QCOMBOBOX*&NBSP;CBX
= qobject_cast<QComboBox*> (editor); qstring str = index.model ()->data (index).
ToString (); if (Str == qstringliteral ("male")) {
cbx->setcurrentindex (0); }else{
cbx->setcurrentindex (1);
} }}