Search code examples
c++qtqt5qlistviewqsqltablemodel

Slot called twice qt


I have an editable list view inside a dock widget. I wanted to keep the track of the data before the user edits and the data after the user edits. The complete concerning code is:

void MainWindow :: createDock()
{
    //initialize dockWidget
    QDockWidget *dock = new QDockWidget("Tags", this);
    dock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
    dock->setFeatures(QDockWidget::DockWidgetFloatable | QDockWidget::DockWidgetMovable);

//widget to store all widgets placed inside dock because dock cannot set layout but can set widget
QWidget *tags = new QWidget(dock);

//initiazlize treeViewModel
listViewModel = new QSqlTableModel(this);
listViewModel->setTable("tags");
listViewModel->select();
listViewModel->setHeaderData(0, Qt::Horizontal, "Tags");

//set the model for treeView
listView = new QListView(dock);
listView->setModel(listViewModel);

connect(listView, &QListView::doubleClicked, this, &MainWindow::onListViewDoubleClicked, Qt::UniqueConnection);
connect(listViewModel, &QSqlTableModel::dataChanged, this, &MainWindow::onLVDataChanged, Qt::UniqueConnection);

//add treeView to the dock
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(listView);
tags->setLayout(layout);

//add the dock widget to the main window and show it
dock->setWidget(tags);
this->addDockWidget(Qt::LeftDockWidgetArea, dock);
//dock->show();
}

void MainWindow :: onLVDataChanged(const QModelIndex& index, const QModelIndex& index2, const QVector<int> & roles)
{
    QMetaMethod metaMethod = sender()->metaObject()->method(senderSignalIndex());
    QMessageBox::information(this, "", metaMethod.name());

afterUpdate = index.data().toString();

//do somethings

beforeUpdate = "";
afterUpdate = "";
}

void MainWindow :: onListViewDoubleClicked(const QModelIndex &index)
{
    QMetaMethod metaMethod = sender()->metaObject()->method(senderSignalIndex());
    QMessageBox::information(this, "", metaMethod.name());

beforeUpdate = index.data().toString();
}

I do this: I double click an item so as to edit it. The onDoubleClick() is called only once (seen becuase of QMessageBox). I add a space to the data present (in my case it was "fiction", i changed it to "fiction "). But, after I press enter, dataChanged() is called twice (again seen through QMessageBox).

I don't emit the signal explicitly. It is emitted only by model.


Solution

  • The problem is caused by the editing strategy, by default it is QSqlTableModel::OnRowChange, this expects the row to be changed emitting a signal to update the item and another to update the entire row, that can be easily seen if we use the following:

    void MainWindow::onListViewDoubleClicked(const QModelIndex &index)
    {
        qDebug()<<__FUNCTION__<<index;
    }
    
    void MainWindow::onLVDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles)
    {
        qDebug()<<__FUNCTION__<<topLeft<<bottomRight<<roles<<topLeft.data();
    }
    

    Output:

    onListViewDoubleClicked QModelIndex(1,0,0x0,QSqlTableModel(0x562940043670))
    onLVDataChanged QModelIndex(1,0,0x0,QSqlTableModel(0x562940043670)) QModelIndex(1,0,0x0,QSqlTableModel(0x562940043670)) QVector() QVariant(QString, "tag2 ")
    onLVDataChanged QModelIndex(1,0,0x0,QSqlTableModel(0x562940043670)) QModelIndex(1,2,0x0,QSqlTableModel(0x562940043670)) QVector() QVariant(QString, "tag2 ")
    

    The solution is to change the editing strategy to QSqlTableModel::OnManualSubmit:

    ...
    listViewModel = new QSqlTableModel(this);
    listViewModel->setTable("tags");
    listViewModel->setEditStrategy(QSqlTableModel::OnManualSubmit); // <--
    listViewModel->select();
    listViewModel->setHeaderData(0, Qt::Horizontal, "Tags");
    ...