how can i get Delegate for QTableWidget?
-
@Jan-Willem now i get clear idea this error is coming when am trying to insert a delegate into column , so i want to know is there any thing wrong to insert delegates into the tablewidget, please help me to get out from this task . @mrjj
-
How does your code look like now, something like this?
void Notepad::test() { QTableWidgetItem* tableItem = new QTableWidgetItem(); table->setRowCount(1); table->setColumnCount(3); table->setItem(0,0,new QTableWidgetItem()); table->setItem(0,1,new QTableWidgetItem()); table->setItem(0,2,new QTableWidgetItem()); table->setMouseTracking(true); table->viewport()->setMouseTracking(true); table->installEventFilter(this); table->viewport()->installEventFilter(this); table->setSelectionBehavior(QAbstractItemView::SelectRows); table->setSelectionMode(QAbstractItemView::SingleSelection); QStandardItemModel model((table->rowCount()),(table->columnCount())); SpinBoxDelegate *sdelegate = new SpinBoxDelegate (); ComboBoxDelegate * comDel = new ComboBoxDelegate (); for (int row = 0; row < 4; ++row) { for (int col = 0; col < 2; ++col) { QModelIndex index = model.index(row, col, QModelIndex()); model.setData(index, QVariant((row + 1) * (col + 1))); } table->setItemDelegateForColumn(0, comDel); table->setItemDelegateForColumn(1, sdelegate); table->setHorizontalHeaderItem(0, new QTableWidgetItem("combobox")); table->setHorizontalHeaderItem(1, new QTableWidgetItem("spinbox")); table->setHorizontalHeaderItem(2, new QTableWidgetItem("lineEdit")); tableItem->setFlags(tableItem->flags() ^ Qt::ItemIsEditable); table->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding); setCentralWidget(table); }
-
@Jan-Willem yeah my code is like this only, but delegates are not assigning to the columns of table widget?
-
The comDel is assigned to the entire column at index 0 with table->setItemDelegateForColumn(0, comDel).
-
@Jan-Willem but that delegate is not assigned to columns of table widget .
-
Just for my curiosity?
Which version of Qt are you using? -
@Jan-Willem qt 4.8.3 on Visual Studio 2010
and my code looks like this
void Notepad::test() { QTableWidgetItem* tableItem = new QTableWidgetItem(); table->setRowCount(1); table->setColumnCount(3); table->setItem(0,0,new QTableWidgetItem()); table->setItem(0,1,new QTableWidgetItem()); table->setItem(0,2,new QTableWidgetItem()); table->setMouseTracking(true); table->viewport()->setMouseTracking(true); table->installEventFilter(this); table->viewport()->installEventFilter(this); table->setSelectionBehavior(QAbstractItemView::SelectRows); table->setSelectionMode(QAbstractItemView::SingleSelection); QStandardItemModel model((table->rowCount()),(table->columnCount())); //SpinBoxDelegate sdelegate; //ComboBoxDelegate comDel; int newrow =table->rowCount(); int newcol =table->columnCount(); SpinBoxDelegate *sdelegate = new SpinBoxDelegate (); ComboBoxDelegate * comDel = new ComboBoxDelegate (); for (int row = 0; row < 1; ++row) { for (int col = 0; col < 3; ++col) { QModelIndex index = model.index(row, col, QModelIndex()); model.setData(index, QVariant((row + 1) * (col + 1))); table->setItemDelegateForColumn( newcol, comDel); } } /*for (int row = 0; row < 4; ++row) { table->setItemDelegateForColumn( 2, &sdelegate); }*/ table->setHorizontalHeaderItem(0, new QTableWidgetItem("combobox")); table->setHorizontalHeaderItem(1, new QTableWidgetItem("spinbox")); table->setHorizontalHeaderItem(2, new QTableWidgetItem("lineEdit")); tableItem->setFlags(tableItem->flags() ^ Qt::ItemIsEditable); table->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding); setCentralWidget(table); }
-
Alright. Frankly I think you are making things more difficult than they have to be.
First of all you shouldn't set the columndelegate inside the for-statement, since you set it multiple times for no reason. Once will do for the entire column, so do it before or after the for-statement.
Second, you create a QStandardItemModel and use it with a QTabelWidget. A QTableWidget has it's own internal model.
The QModelIndex returned by model.index(row, col, QModelIndex()) is not in your table, but in your QStandardItemModel.Perhaps you should read this first: Model/View Programming
-
@Jan-Willem @mrjj Thank you very much. problem is solved. your right am making things difficult , thanks for your advice it makes me to keen observe of my code and solve , Thanks a lot.
Can't believe that there is people who spend their time for such noob as me. Thank you again. -
Glad it works so far.
Good luck with the rest.