我无法在QListView中选择更改时获取信号发射。
到目前为止,我已经尝试了多种方法,但只有那些在没有调试器抱怨的情况下连接了:
// Approach 1
QStandardItemModel *standardModel = new QStandardItemModel(ui->instr_list);
ui->instr_list->setModel(standardModel);
QItemSelectionModel *selectionModel = ui->instr_list->selectionModel();
connect(selectionModel, SIGNAL(currentChanged(QModelIndex, QModelIndex)), this, SLOT (update_bin_instr(QModelIndex, QModelIndex)));
// Approach 2
QStandardItemModel *standardModel = new QStandardItemModel(ui->instr_list);
ui->instr_list->setModel(standardModel);
QItemSelectionModel *selectionModel = ui->instr_list->selectionModel();
connect(selectionModel, SIGNAL(selectionChanged(QItemSelection, QItemSelection)), this, SLOT (update_bin_instr(QItemSelection, QItemSelection)));
instr_list是QListView
信号应该发出,但没有任何反应。有人可以指出(对你们中的一些人(我做错了什么的明显事情吗?:-)
如果我正确理解了您的问题,您希望每次更改qlistview中的选择时都会触发update_bin_instr。
在主窗口中,我添加了
private slots:
void Update(const QModelIndex ¤t, const QModelIndex &previous);
private:
QStringListModel *m_Model;
QItemSelectionModel *selectionModel;
在主窗口构造函数中。
m_Model = new QStringListModel();
QStringList list;
list << "a" << "b" << "c";
m_Model->setStringList(list);
ui->listView->setModel(m_Model);
selectionModel = ui->listView->selectionModel();
connect(selectionModel, &QItemSelectionModel::currentChanged, this, &MainWindow::Update);
}
每次我更改选择更新时都会被调用。
void MainWindow::Update(const QModelIndex ¤t, const QModelIndex &previous)
{
qDebug() << "changed" << "current" << current.row() << "previous" << previous.row();
}