插槽调用了两次 qt
Slot called twice qt
我在停靠小部件中有一个可编辑的列表视图。我想跟踪用户编辑前的数据和用户编辑后的数据。完整的相关代码为:
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();
}
我这样做:
我双击一个项目以对其进行编辑。 onDoubleClick() 只被调用一次(因为 QMessageBox 而被调用)。我在现有数据中添加了一个 space(在我的例子中是 "fiction",我将其更改为 "fiction ")。但是,在我按下回车键后,dataChanged() 被调用了两次(再次通过 QMessageBox 看到)。
我没有明确发出信号。它仅由模型发出。
问题是由编辑策略引起的,默认情况下是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();
}
输出:
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 ")
解决方法是将编辑策略改为QSqlTableModel::OnManualSubmit
:
...
listViewModel = new QSqlTableModel(this);
listViewModel->setTable("tags");
listViewModel->setEditStrategy(QSqlTableModel::OnManualSubmit); // <--
listViewModel->select();
listViewModel->setHeaderData(0, Qt::Horizontal, "Tags");
...
我在停靠小部件中有一个可编辑的列表视图。我想跟踪用户编辑前的数据和用户编辑后的数据。完整的相关代码为:
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();
}
我这样做: 我双击一个项目以对其进行编辑。 onDoubleClick() 只被调用一次(因为 QMessageBox 而被调用)。我在现有数据中添加了一个 space(在我的例子中是 "fiction",我将其更改为 "fiction ")。但是,在我按下回车键后,dataChanged() 被调用了两次(再次通过 QMessageBox 看到)。
我没有明确发出信号。它仅由模型发出。
问题是由编辑策略引起的,默认情况下是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();
}
输出:
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 ")
解决方法是将编辑策略改为QSqlTableModel::OnManualSubmit
:
...
listViewModel = new QSqlTableModel(this);
listViewModel->setTable("tags");
listViewModel->setEditStrategy(QSqlTableModel::OnManualSubmit); // <--
listViewModel->select();
listViewModel->setHeaderData(0, Qt::Horizontal, "Tags");
...