简体   繁体   English

插槽两次调用qt

[英]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). onDoubleClick()仅被调用一次(由于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). 但是,在按Enter键之后,dataChanged()被调用了两次(再次通过QMessageBox看到)。

I don't emit the signal explicitly. 我没有明确发出信号。 It is emitted only by model. 它仅按模型发出。

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: 问题是由编辑策略引起的,默认情况下是QSqlTableModel::OnRowChange ,它期望更改行,从而发出信号来更新项目,而另一个信号来更新整个行,如果使用以下内容,则可以很容易地看出来:

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 : 解决方案是将编辑策略更改为QSqlTableModel::OnManualSubmit

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM