如何从/向QListView的SIGNAL / SLOT传递/检索参数?

时间:2015-02-12 10:33:18

标签: c++ qt

我制作了名为on_listView_currentChanged的自定义 SLOT ,并将其与 QListView currentChanged SIGNAL 相关联。但 SIGNAL / SLOT 无效。

如何从/向QListView的SIGNAL / SLOT传递/检索参数?

以下是我最基本的代码:

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
&#13;
&#13;
&#13;

mainwindow.cpp

&#13;
&#13;
#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
}
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:2)

从连接调用中删除参数名称,它不适用于它们。

即。使用

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

}

不是

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

}

顺便说一句,它会向stdout打印一条警告信息

相关问题