插槽叫qt两次

时间:2018-06-18 04:58:50

标签: c++ qt qt5 qlistview qsqltablemodel

我在停靠小部件中有一个可编辑的列表视图。我希望在用户编辑之前跟踪数据,并在用户编辑之后保留数据。完整的相关代码是:

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看到)。

我没有明确发出信号。它仅由模型发出。

1 个答案:

答案 0 :(得分:0)

问题是由编辑策略引起的,默认情况下是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");
...