我有一个与两个QListViews的接口,其中左边决定了在右边显示的内容:

要更新右边的列表,我有以下功能:
void CodePlug::handleSelectionChanged()
{
QModelIndex portIndex = ui->listPorts->currentIndex();
QString portItemText = portIndex.data(Qt::DisplayRole).toString();
ui->listPlugs->setModel(ListModelFromMap(plugs[portItemText]));
currentPort = portItemText;
qDebug(currentPort.toStdString().data());
}它与selectionChanged信号相连:
CodePlug::CodePlug(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::CodePlug)
{
ui->setupUi(this);
ui->listPorts->setModel(ListModelFromMap(ports));
QModelIndex portIndex = ui->listPlugs->currentIndex();
QString portItemText = portIndex.data(Qt::DisplayRole).toString();
ui->listPlugs->setModel(ListModelFromMap(plugs[portItemText]));
connect(ui->listPorts->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)), this, SLOT(handleSelectionChanged()));
}但是,通过键盘或鼠标更改所选项永远不会触发handleSelectionChanged()。它不会产生任何错误,它只是什么都不做。有人能告诉我为什么吗?
发布于 2016-09-02 03:56:59
我想出来了,这太傻了。
当一个新项目被添加到列表中时,我调用了setModel() on listPorts,这当然破坏了连接。我怀疑有更好的方法来处理更改,所以我将尝试修复它,但是现在,每次更改模型时,我都会重新连接。
发布于 2016-09-01 07:09:24
可点错误:
handleSelectionChanged()是否定义为私有/公共时隙?QAbstractItemView::NoSelection吗?尝试强制一个/多个选择:
ui->listPorts->setSelectionMode(QItemSelectionModel::::SingleSelection)检查您的QObject::connect是否运行良好:
if (!connect(ui->listPorts->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)), this, SLOT(handleSelectionChanged())))
qDebug() << "Something wrong :(";发布于 2016-09-01 06:59:00
在我看来连接还不错。您确定没有看到任何运行时错误?下面,很少有东西可以检查它们是否有帮助。
1)检查是否将Q_OBJECT宏添加到CodePlug类头中。如果没有,请添加它并再次运行qmake。
class CodePlug : public QMainWindow
{
Q_OBJECT2)检查是否将handleSelectionChanged定义为一个槽。
private slots:
void handleSelectionChanged();3)通过检查返回的内容来检查connect是否真的成功
bool ret = connect(ui->listPorts->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)), this, SLOT(handleSelectionChanged()));4)测试currentChanged信号是否通过连接到例如handleCurrentChanged插槽来触发。
https://stackoverflow.com/questions/39262284
复制相似问题