Search code examples
c++qtsignals-slotsqt-signals

How to pass/retrieve the parameters from/to SIGNAL/SLOT of a QListView?


I made a custom SLOT called on_listView_currentChanged, and connected it to the currentChanged SIGNAL of a QListView. But the SIGNAL/SLOT are not working.

How can I pass/retrieve the parameters from/to SIGNAL/SLOT of a QListView?

mainwindow.h:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QStringListModel>
#include <QStringList>
#include <QFile>
#include <QString>
#include <QTextStream>
#include <QTimeEdit>
#include <QElapsedTimer>
#include <QItemSelection>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private slots:
    void on_listView_currentChanged(const QModelIndex & current, const QModelIndex & previous);

private:
    Ui::MainWindow *ui;

    void populateListView();

    QStringListModel *stringListModel;
    QStringList stringList;
};

#endif // MAINWINDOW_H

mainwindow.cpp:

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    populateListView();

    connect(ui->listView->selectionModel(), SIGNAL(currentChanged(const QModelIndex & current, const QModelIndex & previous)), this, SLOT(on_listView_currentChanged(const QModelIndex & current, const QModelIndex & previous)));
}
MainWindow::~MainWindow()
{
    delete ui;
}
void MainWindow::on_listView_currentChanged(const QModelIndex & current, const QModelIndex & previous)
{
    ui->textBrowser->setHtml(current.data().toString());
}
void MainWindow::populateListView()
{
    //My lengthy codes to populate the lsitView with items
}

Solution

  • Remove the parameter names from the connect call, it does not work with them.

    I.e., use:

    connect(ui->listView->selectionModel(), SIGNAL(currentChanged(const QModelIndex &, const QModelIndex & )), this, SLOT(on_listView_currentChanged(const QModelIndex & , const QModelIndex & )));
    

    not:

    connect(ui->listView->selectionModel(), SIGNAL(currentChanged(const QModelIndex & current, const QModelIndex & previous)), this, SLOT(on_listView_currentChanged(const QModelIndex & current, const QModelIndex & previous)));
    

    By the way, it prints a warning message about that to stdout.