Hi,
i have a QTablewidget with "SingleSelection". How can i move the rows up and down?
Thanks in advance,
Whitefurrows
Printable View
Hi,
i have a QTablewidget with "SingleSelection". How can i move the rows up and down?
Thanks in advance,
Whitefurrows
There isn't any single API method for doing that. Use QTableWidget::takeItem() to temporarily remove both "source" and "destination" items from the table without deleting them, and then use QTableWidget::setItem() to set them back in a reverse order.
Hi,
the problem is i have many items in a row. I try this to remove the row.
Code:
QModelIndexList idxList = tableWidget->selectionModel()->selectedIndexes(); QListIterator<QModelIndex> idxIt(indexList); idxIt.toBack(); while(idxIt.hasPrevious()){ if(index.column() == 0){ tableWidget->removeRow(idx.row()); } }
but im not sure how can i paste it to move up and down. Can you help me please?
Thanks in advance,
Whitefurrows
Code:
void TableWidget::move(bool up) { Q_ASSERT(selectedItems().count() > 0); const int sourceRow = row(selectedItems().at(0)); const int destRow = (up ? sourceRow-1 : sourceRow+1); Q_ASSERT(destRow >= 0 && destRow < rowCount()); // take whole rows QList<QTableWidgetItem*> sourceItems = takeRow(sourceRow); QList<QTableWidgetItem*> destItems = takeRow(destRow); // set back in reverse order setRow(sourceRow, destItems); setRow(destRow, sourceItems); } // takes and returns the whole row QList<QTableWidgetItem*> TableWidget::takeRow(int row) { QList<QTableWidgetItem*> rowItems; for (int col = 0; col < columnCount(); ++col) { rowItems << takeItem(row, col); } return rowItems; } // sets the whole row void TableWidget::setRow(int row, const QList<QTableWidgetItem*>& rowItems) { for (int col = 0; col < columnCount(); ++col) { setItem(row, col, rowItems.at(col)); } }
Hi,
thank you for that great example, that's work fine.
Greetings,
Whitefurrows
I have the same problem, but what i need to swap are the cell widgets, and getting the items don't work, the items get changed, but the cell widgets keep the same.
Is there any way of doing this?
Code:
colorizerTable->setItem(row1, 0, item2); colorizerTable->setItem(row2, 0, item1);
Apparently QTableWidget::setCellWidget() maps the widget to the specific cell, not to the specific item. So you will have swap the cell widget by hand, too.
The problem is that, when i set a widget for a specific cell, it deletes the previous one, so I cannot
The only way I've found is to have a Clone function in the widget's class
You can move row using method "moveSection" in table->verticalHeader()
Hi,
"moveSection" will move the section of the header and its contents (<- the row including QWidgets set with "setCellWidget"). But what if i want to move only the contents of the row without changing the header? More specifically, what should i do if want to swap two cells containing each of them a QWidget?
How can i swap the cell widget by hand?Quote:
jpn
Re: QTableWidget move row?
Apparently QTableWidget::setCellWidget() maps the widget to the specific cell, not to the specific item. So you will have swap the cell widget by hand, too.
Thanx in advance
Till now, i've manage to swap two cells with QWidgets but with a very ugly trick!
First, i get the pointers of the two QWidgets by calling "cellWidget" (let's say wid1 and wid2). Then, i call "editorDestroyed(wid1)" and "editorDestroyed(wid2)". This protected slot releases QWidget's pointer from the internal map but does NOT delete the widget! Finally, i call setCellWidget to set the QWidgets where i want to.
I know that this method is not cute, but it's the only i've got till now :rolleyes:
Damn !
the function 'editorDestroyed' is protected and not accessible :(
Then all you have to do is derive your own table widget class from QTableWidget and you have access to all of the protected methods of QTableWidget and its base classes. This is basic C++.Quote:
the function 'editorDestroyed' is protected and not accessible