插槽调用了两次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(。我在存在的数据中添加了一个空格(在我的情况下它是"虚构的",我将其更改为"虚构"(。但是,在我按回车键后,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");
...
相关文章:
- g++的分段错误(在NaN上使用to_string两次时)
- 蛇在C++不会连续转两次
- 检查一个数组是否包含在另一个数组中,以相反的顺序,至少两次
- 从具有按值捕获的 lambda 移动构造 std::函数时,移动构造函数调用两次
- 我应该如何去缓解两次出现的cin?
- 通过插槽和信号在不同线程中的两个qt对象之间进行通信
- Realloc 两次无法在 Visual Studio 上运行
- 使用 getline(cin, var) 两次在进行字符串比较时会产生错误 (==)
- 为什么映射插入和 map.find() 的单次迭代比插入和 map.find() 的两次单独迭代慢得多
- C++析构函数调用两次,堆栈分配的复合对象
- Qt插槽调用了两次
- 插槽调用了两次qt
- 执行随机开关函数的QT方式是什么连续两次使用相同情况的方法
- Qt UDPsocket readyread() SIGNAL 发出两次
- Qt - 堆栈上具有父级的 QObject 如何被删除两次?
- 在Qt 5.4中可以对互斥对象进行两次解锁吗
- 使用 gdal 和 qt 打开相同的 tif 两次显示空白
- Qt 当列表小部件项被单击两次时崩溃
- C++Qt QFileSystemWatcher文件上传两次
- Qt插槽调用两次