PDA

View Full Version : QTableWidget Internal Drag/ Drop Entire Row



davethomaspilot
16th December 2012, 00:31
I've seen several posts that describe issues with internal moves overwriting the target row instead of doing something like an insert, but I have a different problem.

I can move the row just fine as long as I drop on the first column. But, if I drop on other columns, I get varying undesirable behavior.

Sometimes it copies the row but shifts the cells. Sometimes it does nothing (what I want). If I keep choosing cells that aren't in column 0 for the drop, an exception will be raised.

So, I just want to prevent drops in anything other than column, or interpret a drop in any column as a drop in that row to column 0.

I have this in the constructor of the QTableWidget:

tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows );

tableWidget->setDragEnabled(true);
tableWidget->setDragDropOverwriteMode(false);
tableWidget->setDragDropMode(QTableWidget::InternalMove);
tableWidget->setDefaultDropAction(Qt::CopyAction);

When I create the TableWidget, I do this (Class LineUp has a QTableWidget named tableWidget):

void ClubRosters::add_lineup()
{
LineUp *newTab = new LineUp(tabWidget);
QTableWidgetItem *cell = new QTableWidgetItem;
for (int i=0; i<newTab->tableWidget->rowCount();i++)
{
for (int j=0;j<newTab->tableWidget->columnCount();j++)
{
cell = newTab->tableWidget->item(i,j);
if (!cell)
{
cell = new QTableWidgetItem();
if (j != 0)
{
cell->setFlags(cell->flags() & ~Qt::ItemIsDropEnabled);
cell->setFlags(cell->flags() & ~Qt::ItemIsDragEnabled);
}
newTab->tableWidget->setItem(i,j,cell);

}
}
}
int idx = tabWidget->currentIndex ();
bool ok = true;
QString newName = QInputDialog::getText (
this, tr ("Create new lineup"),
tr ("Lineup Name"),
QLineEdit::Normal,
tabWidget->tabText (idx),
&ok);

if (ok)
{
tabWidget->addTab(newTab, newName);
int count = tabWidget->count();
tabWidget->setCurrentIndex(count-1);
}
}

So, how do I prevent the drops to any columns but column 0? Or, is there a better way to implement drag/drop of entire row?

Thanks,

Dave Thomas

Added after 57 minutes:

Found this, and it does exactly what I want!

https://bugreports.qt-project.org/browse/QTBUG-13873?page=com.atlassian.jira.plugin.system.issuet abpanels:all-tabpanel

I haven't integrated into my code yet, but the example works perfectly.

Dave Thomas