fritz
16th November 2006, 03:43
Hi all,
I have placed a QTableWidget with 3 columns in Designer and promoted it to DataTableWidget. In implementation I have provided item delegates to have the first and the third column acting like QLineEdit and the second column acting like QComboBox. When cellChanged(int row, int) signal is emitted and if (row == rowCount() - 1) then a new row is added else if there is no data in the currently edited row (deleted just now) then the row is removed. I have also reimplemented insertRow() like this:
void DataTableWidget::insertRow(int row)
{
QTableWidget::insertRow(row); // call the parent function
// now make sure that all the cells exist
setItem(row, 0, new QTableWidgetItem());
setItem(row, 1, new QTableWidgetItem());
setItem(row, 2, new QTableWidgetItem());
}
Everything works like a charm, but now I have to implement an ability of moving rows up or down. I have provided a context menu with these actions, so when the user right-clicks on the cell:
void DataTableWidget::contextMenuEvent(QContextMenuEven t* event)
{
// disable these actions by default
rowDownAction->setEnabled(false);
rowUpAction->setEnabled(false);
int row = rowAt(event->y());
if (row != -1) // if there is a row under cursor
{
selectRow(row);
// if it is not the last row which is designed for entering new data only
// and not the first row which cannot be moved up
if (row != rowCount() - 1 && row > 0)
rowUpAction->setEnabled(true);
// if it is not the last row with data
// then we can move it down
if (row < rowCount() - 2)
rowDownAction->setEnabled(true);
}
contextMenu->popup(QPoint(event->globalX(), event->globalY()));
event->accept();
}
The problem is that I don't know how to swap the rows. I mean
void DataTableWidget::onRowDown()
{
// what is here?
}
I have tried simple
for (int c = 0; c < columnCount(); c++)
{
QTableWidgetItem* src = item(currentCell()->row(), c);
QTableWidgetItem* dest = item(currentCell()->row() + 1, c);
QTableWidgetItem* buf = src;
src = dest;
dest = buf;
}
as well as
for (int c = 0; c < columnCount(); c++)
{
QTableWidgetItem* src = item(currentCell()->row(), c);
QTableWidgetItem* dest = item(currentCell()->row() + 1, c);
setItem(currentCell()->row(), c, dest);
setItem(currentCell()->row() + 1, c, src);
}
but neither worked for me. Then I have decided to insertRow(currentCell()->row() + 1) and copy all the elements of the current row to the new row, then remove the current row. But I have discovered that it doesn't insertRow(int row) with row set to anything other than rowCount(). I mean the function is called and executed but nothing happens.
How can I swap rows and insert a new row in random position of the table?
thanks
I have placed a QTableWidget with 3 columns in Designer and promoted it to DataTableWidget. In implementation I have provided item delegates to have the first and the third column acting like QLineEdit and the second column acting like QComboBox. When cellChanged(int row, int) signal is emitted and if (row == rowCount() - 1) then a new row is added else if there is no data in the currently edited row (deleted just now) then the row is removed. I have also reimplemented insertRow() like this:
void DataTableWidget::insertRow(int row)
{
QTableWidget::insertRow(row); // call the parent function
// now make sure that all the cells exist
setItem(row, 0, new QTableWidgetItem());
setItem(row, 1, new QTableWidgetItem());
setItem(row, 2, new QTableWidgetItem());
}
Everything works like a charm, but now I have to implement an ability of moving rows up or down. I have provided a context menu with these actions, so when the user right-clicks on the cell:
void DataTableWidget::contextMenuEvent(QContextMenuEven t* event)
{
// disable these actions by default
rowDownAction->setEnabled(false);
rowUpAction->setEnabled(false);
int row = rowAt(event->y());
if (row != -1) // if there is a row under cursor
{
selectRow(row);
// if it is not the last row which is designed for entering new data only
// and not the first row which cannot be moved up
if (row != rowCount() - 1 && row > 0)
rowUpAction->setEnabled(true);
// if it is not the last row with data
// then we can move it down
if (row < rowCount() - 2)
rowDownAction->setEnabled(true);
}
contextMenu->popup(QPoint(event->globalX(), event->globalY()));
event->accept();
}
The problem is that I don't know how to swap the rows. I mean
void DataTableWidget::onRowDown()
{
// what is here?
}
I have tried simple
for (int c = 0; c < columnCount(); c++)
{
QTableWidgetItem* src = item(currentCell()->row(), c);
QTableWidgetItem* dest = item(currentCell()->row() + 1, c);
QTableWidgetItem* buf = src;
src = dest;
dest = buf;
}
as well as
for (int c = 0; c < columnCount(); c++)
{
QTableWidgetItem* src = item(currentCell()->row(), c);
QTableWidgetItem* dest = item(currentCell()->row() + 1, c);
setItem(currentCell()->row(), c, dest);
setItem(currentCell()->row() + 1, c, src);
}
but neither worked for me. Then I have decided to insertRow(currentCell()->row() + 1) and copy all the elements of the current row to the new row, then remove the current row. But I have discovered that it doesn't insertRow(int row) with row set to anything other than rowCount(). I mean the function is called and executed but nothing happens.
How can I swap rows and insert a new row in random position of the table?
thanks