我有一个带有两个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()
。它不会产生任何错误,它什么都不做。谁能给我个个主意吗? 最佳答案
可能的错误:
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 :(";