I have an interface with two QListViews
, where the left determines what is displayed in the right:
To update the list on the right, I have the following function:
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());
}
and it's connected to the selectionChanged signal here:
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()));
}
However, changing the selected item by either the keyboard or the mouse never triggers handleSelectionChanged()
. It doesn't generate any errors, it just doesn't do anything. Could anybody give me an idea why?
I figured it out, and it was silly.
When a new item was added to the list, I was calling setModel()
on listPorts
, which of course broke the connection. I suspect there is a better way to handle the change, so I'll try and fix that but for now, I'm reconnecting every time the model is changed.