Drop on QTableView creates new columns



  • I have two QTableViews in a dialog. One table view contains "available items", and the other contains "selected items". Both QTableViews have three columns, and each table view is connected to its own QStandardItemModel.

    Dropping a row from "available items" to "selected items" works fine, as long as I drop the row at the first column. But if I drop the row at the second or third column (between two rows that has already been selected by drag & drop), new columns are created. The text is inserted from the second or third column onwards.

    Is it possible to prevent this from happening? A row that is dropped on "selected items" shall always be inserted from the first column.

    @
    Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
    {
    ui->setupUi(this);

    QStandardItemModel* availableModel_ = new QStandardItemModel();
    availableModel_->setHorizontalHeaderItem(0, new QStandardItem("First"));
    availableModel_->setHorizontalHeaderItem(1, new QStandardItem("Second"));
    availableModel_->setHorizontalHeaderItem(2, new QStandardItem("Third"));
    
    ui->tvAvailable->setModel(availableModel_);
    ui->tvAvailable->setSelectionBehavior(QAbstractItemView::SelectRows);
    ui->tvAvailable->setSelectionMode(QAbstractItemView::ExtendedSelection);
    ui->tvAvailable->verticalHeader()->setVisible(false);
    ui->tvAvailable->setEditTriggers(QAbstractItemView::NoEditTriggers);
    
    ui->tvAvailable->setDragEnabled(true);
    ui->tvAvailable->setDragDropOverwriteMode(false);
    ui->tvAvailable->setDefaultDropAction(Qt::MoveAction);
    
    QStandardItemModel* selectedModel_ = new QStandardItemModel();
    selectedModel_->setHorizontalHeaderItem(0, new QStandardItem("First"));
    selectedModel_->setHorizontalHeaderItem(1, new QStandardItem("Second"));
    selectedModel_->setHorizontalHeaderItem(2, new QStandardItem("Third"));
    
    ui->tvSelected->setModel(selectedModel_);
    ui->tvSelected->setSelectionBehavior(QAbstractItemView::SelectRows);
    ui->tvSelected->setSelectionMode(QAbstractItemView::NoSelection);
    ui->tvSelected->verticalHeader()->setVisible(false);
    ui->tvSelected->setEditTriggers(QAbstractItemView::NoEditTriggers);
    
    ui->tvSelected->setAcceptDrops(true);
    ui->tvSelected->setDropIndicatorShown(true);
    
    int row = 0;
    int col = 0;
    availableModel_->insertRows(0, 4);
    availableModel_->setData(availableModel_->index(row, col), QString("Row 1"));
    availableModel_->item(row,col++)->setDropEnabled(false);
    availableModel_->setData(availableModel_->index(row, col), QString("The first row"));
    availableModel_->item(row,col++)->setDropEnabled(false);
    availableModel_->setData(availableModel_->index(row, col), 1);
    availableModel_->item(row,col++)->setDropEnabled(false);
    
    row++;
    col = 0;
    availableModel_->setData(availableModel_->index(row, col), QString("Row 2"));
    availableModel_->item(row,col++)->setDropEnabled(false);
    availableModel_->setData(availableModel_->index(row, col), QString("The second row"));
    availableModel_->item(row,col++)->setDropEnabled(false);
    availableModel_->setData(availableModel_->index(row, col), 2);
    availableModel_->item(row,col++)->setDropEnabled(false);
    
    row++;
    col = 0;
    availableModel_->setData(availableModel_->index(row, col), QString("Row 3"));
    availableModel_->item(row,col++)->setDropEnabled(false);
    availableModel_->setData(availableModel_->index(row, col), QString("The third row"));
    availableModel_->item(row,col++)->setDropEnabled(false);
    availableModel_->setData(availableModel_->index(row, col), 3);
    availableModel_->item(row,col++)->setDropEnabled(false);
    
    row++;
    col = 0;
    availableModel_->setData(availableModel_->index(row, col), QString("Row 4"));
    availableModel_->item(row,col++)->setDropEnabled(false);
    availableModel_->setData(availableModel_->index(row, col), QString("The fourth row"));
    availableModel_->item(row,col++)->setDropEnabled(false);
    availableModel_->setData(availableModel_->index(row, col), 4);
    availableModel_->item(row,col++)->setDropEnabled(false);
    

    }
    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.