selectionChanged信号

selectionChanged信号

我有一个带有两个QListViews的界面,其中左边确定右边显示的内容:
c++ - QListView selectionModel不发送selectionChanged信号-LMLPHP

要更新右侧的列表,我具有以下功能:

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()。它不会产生任何错误,它什么都不做。谁能给我个个主意吗?

最佳答案

可能的错误:

  • 您的函数handleSelectionChanged()是否定义为专用/公用插槽?
  • 是您的默认SelectionMode QAbstractItemView::NoSelection吗?

  • 尝试强制选择一个/多个:
    ui->listPorts->setSelectionMode(QItemSelectionModel::::SingleSelection)
    

    检查您的QObject::connect是否运作良好:
    if (!connect(ui->listPorts->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)), this, SLOT(handleSelectionChanged())))
         qDebug() << "Something wrong :(";
    

    10-02 06:12