PDA

View Full Version : QTableWidget move row?



whitefurrows
22nd August 2006, 17:11
Hi,

i have a QTablewidget with "SingleSelection". How can i move the rows up and down?

Thanks in advance,

Whitefurrows

jpn
22nd August 2006, 22:26
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.

whitefurrows
23rd August 2006, 10:20
Hi,

the problem is i have many items in a row. I try this to remove the row.


QModelIndexList idxList = tableWidget->selectionModel()->selectedIndexes();

QListIterator<QModelIndex> idxIt(indexList);
idxIt.toBack();

while(idxIt.hasPrevious()){
QModelIndex idx = idxIt.previous();
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

jpn
23rd August 2006, 11:50
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));
}
}

whitefurrows
23rd August 2006, 13:47
Hi,

thank you for that great example, that's work fine.

Greetings,

Whitefurrows

roleroz
24th August 2006, 21:12
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?



QTableWidgetItem *item1 = colorizerTable->takeItem(row1, 0);
QTableWidgetItem *item2 = colorizerTable->takeItem(row2, 0);
colorizerTable->setItem(row1, 0, item2);
colorizerTable->setItem(row2, 0, item1);

jpn
24th August 2006, 21:17
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.

roleroz
24th August 2006, 21:23
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

yunxiaodong
4th August 2011, 11:01
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));
}
}


it is only can moveup or movedown.cant move tow or more rows.actually,it is called swap two rows seemed more reasonable.

bmn
17th January 2014, 19:49
You can move row using method "moveSection" in table->verticalHeader()

aftos
27th February 2014, 10:37
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?


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.

How can i swap the cell widget by hand?
Thanx in advance

aftos
27th February 2014, 21:32
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:

marculin
21st April 2017, 11:36
Damn !
the function 'editorDestroyed' is protected and not accessible :(

d_stranz
21st April 2017, 21:47
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++.