[SOLVED] Qt4 - QTableWidget selectedItems QProgressBar
-
Hello,
I have a QTableWidget with 6 columns. In the last column, the 6th (index 5, so), I have a QProgressBar.I'm trying to change the value of the progress bar with :
@reinterpret_cast<QProgressBar*>(_table->selectedItems().at(5))->setValue(0);@
But it throw me an error :
"ASSERT failure in QList<T>::at: "index out of range", file c:\QtSDK\Desktop\Qt\4.8.1\msvc2010\include\QtCore/qlist.h, line 469"Because in fact :
@std::cout << _table->selectedItems().length() << std::endl;@
returns me 5.
How is this possible ? I have a missing element here, that's why I have that error, but why ?
-
Hi,
This a traditional counting vs programatic counting problem.
A size of 5 in standard counting means 1 to 5. But when you are programming, it means counting from 0 to 4.
-
Hi SGaist,
thanks for replying.I have personally never seen a "counting" in programming from 0. When you do an strlen (C language), it counts from 1, not from 0. It begins from 0 when we use indexes.
But anyway, you're saying the lengh in correct, so why does
@reinterpret_cast<QProgressBar*>(_table->selectedItems().at(5))->setValue(0);@
throw me an error ?
Because this :
@_table->selectedItems().at(4)@
is giving me the good item, the one just before the progress bar.
I think it has something to do with the fact that I had to "setCellWidget" to add the progress bar to the table. But my table is parametered like follow :
@_table->setSelectionBehavior(QAbstractItemView::SelectRows);
_table->setSelectionMode(QAbstractItemView::SingleSelection);@So it does select the progress the progress bar anyway. And I can see it selected.
-
In a string the first char is at position 0
I agree with you about the fact that setting a widget on a cell might change the cell selection reaction. Whether this is the right behavior, I can't tell.
Check the size of_table->selectedItems(), It's probably 5.
You can always use the selected row number combined with the known column to retrieve the progress bar
-
I've found the problem. The problem is that I'm using "selectedItems" which returns me correctly the "items" ! But QProgressBar here is a QWidget. And my cast is wrong.
I'm casting a QTableWidgetItem* instead of a QWidget* to a QProgressBar*. So as you said, I have to use :
@reinterpret_cast<QProgressBar*>(_table->cellWidget(_table->selectedItems().at(0)->row(), 5))->setValue(0);@
to get the widget and not the item.
-
Great you have it working !
But still, your error was about asking for an out of bound value, which has nothing to do with the faulty cast. So I am still wondering about the size of your selectItems()
-
I also think so, but I wonder if this is the correct/intended behavior