how can i get Delegate for QTableWidget?
-
@srikanth
But you set them to the view and
void Notepad::test()
{
..
ComboBoxDelegate comDel;; <<< lives here. not in classWill die as soon as test() is executed and the view will point to invalid object.
here u give it address of this local variable that will be deleted soon.
table->setItemDelegateForColumn( column, &comDel);So it does NOT look right to me :)
-
@mrjj so what can i do ta add delegates , actually Notepad class is to create a window with tablewidget contains 3 columns and 1 row,am using mouse event to add rows and delete rows by using ActinEvent methods, up to here everything is doing good, now i am trying to add combobox delegate to 1st column and spinbox delegate to 2nd column so am not able to do please give me the solution please...
-
you just new them instead :)
ComboBoxDelegate * comDel = new ComboBoxDelegate ();
Rest is 100% the same.
except no & in
table->setItemDelegateForColumn( column, &comDel);
-->
table->setItemDelegateForColumn( column, comDel); -
@mrjj the error is showing like this "Unhandled exception at 0x778e3560 in add or remove colmns.exe:0xC00000FD:Stack overflow. "
and i make changes like this as you saidvoid 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; SpinBoxDelegate *sdelegate = new SpinBoxDelegate (); ComboBoxDelegate * comDel = new ComboBoxDelegate (); //ComboBoxDelegate comDel; for (int row = 0; row < 4; ++row) { QWidget *parent; QStyleOptionViewItem option; for (int column = 0; column < 1; ++column) { table->setItemDelegateForColumn( column, comDel); QModelIndex index = model.index(row, column, QModelIndex()); model.setData(index, QVariant((row + 1) * (column + 1))); } for (int column = 1; column < 2; ++column) { table->setItemDelegateForColumn( column, sdelegate ); QModelIndex index = model.index(row, column, QModelIndex()); model.setData(index, QVariant((row + 1) * (column + 1))); } } 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); }
-
Hi
That is a bug in your code, most likely.Please use debugger and single step to find the line
that gives it.Its very hard to guess by looking at code.
You get a Stack overflow - which often is a infinite loop or something like that.
-
@srikanth
Most likely in your delegate.
You can insert break point in each function and single step when you reach it.
That way you can find where it crashes.
No way i can just guess it, sadly.if you think it crash in Notepad::test()
single step should find it. -
Just wondering, but it seems you add a columnDelegate for every row you pass through. If I'm not mistaken, you should add a columnDelegate just once for the entire column.
And you seem to use a for-statement for setting the columDelegate, while you only execute it once. That doesn't make sense to me.
I'm not sure if this would fix anything of your problem, but I think you're code would benefit from it if you if you fixed this.
-
@Jan-Willem thanks a lot the error is fix, when i put for loop in a comment , now how can i assign ComboBoxDelegate for 1st column and SpinBoxDelegate to 2nd cloumn.
-
@srikanth
hi
Sounds like you crash when close down then/cleaning up.
I think
@Jan-Willem has a point of you setting multiple times.
setItemDelegateForColumn( column, comDel);
It might delete it multiple times.
So please try set only once. (for test) -
@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?