3
votes

I have an interface with two QListViews, where the left determines what is displayed in the right: layout

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?

5

5 Answers

2
votes

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.

1
votes

Posible errors:

  1. Is your function handleSelectionChanged() defined as private/public slot?
  2. Is your default SelectionMode QAbstractItemView::NoSelection?

Try to force a single/multiple selection:

ui->listPorts->setSelectionMode(QItemSelectionModel::::SingleSelection)

Check if your QObject::connect is working well:

if (!connect(ui->listPorts->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)), this, SLOT(handleSelectionChanged()))) 
     qDebug() << "Something wrong :(";
1
votes

Connection looks ok to me. You sure you are not seeing any runtime errors? Below, few things to check if they help.

1) Check that you have added Q_OBJECT macro to your CodePlug class header. If not, add it and run qmake again.

class CodePlug : public QMainWindow
{
    Q_OBJECT

2) Check that you have defined handleSelectionChanged as a slot.

private slots:
    void handleSelectionChanged();

3) Check if connect actually succeeds by checking what it returns

bool ret = connect(ui->listPorts->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)), this, SLOT(handleSelectionChanged()));

4) Test if currentChanged signal is triggered by connecting it to e.g. handleCurrentChanged slot.

0
votes

Most likely you're not changing the selection, you're changing the current/active item. Connect to the activated(const QModelIndex &index) signal instead.

0
votes

Same problem, I confirm the model must be set before connect