2

I want to call a function indexChanged() if an index in my treeView is changed. I used ui->treeView->currentChanged() signal but it didn't call indexChanged() slot, even though I connected the signal to the slot.

Here's my code :

.cpp file

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

    connect(ui->treeView->selectionModel(), &QItemSelectionModel::currentChanged, this, &TipManager::indexChanged);

    ...
}

void TipManager::indexChanged(const QModelIndex &current, const QModelIndex &previous)
{
    trimCurrentPath(previous); 
}

.h file

namespace Ui {
class TipManager;
}

class TipManager : public QWidget
{
    Q_OBJECT

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

public slots:
    void indexChanged(const QModelIndex &current, const QModelIndex &previous);

private:
    Ui::TipManager *ui;  
    ...
};

I also tested in debug mode, but the slot function indexChanged() is not even called. Plus, it shows this message instead : QObject::connect: invalid null parameter

Mohammad Kanan
  • 4,452
  • 10
  • 23
  • 47
Sean
  • 301
  • 1
  • 2
  • 10
  • 1
    You need to identify the `null` parameter being reported by the error message -- my guess would be the value returned by `QTreeView::selectionModel()`. Also, assuming you just want notification of a change to the `QTreeView`'s current index you might want to change your code to `connect(ui->treeView, &QTreeView::currentChanged, ...)`. – G.M. Jun 26 '18 at 17:39
  • @G.M. I can't call `&QTreeView::currentChanged` like this `connect(ui->treeView, &QTreeView::currentChanged, ...)`, because it's protected. – Sean Jun 26 '18 at 17:54
  • Sorry, I misread the documentation. But, as [`QTreeView::currentChanged`](http://doc.qt.io/qt-5/qtreeview.html#currentChanged) is a virtual protected member you could always override it in a subclass and perform the necessary actions there. – G.M. Jun 26 '18 at 17:59
  • @G.M. at this line in constructor `connect(ui->treeView, &QTreeView::currentChanged, this, &TipManager::indexChanged);` , this message shows up - **error: 'virtual void QTreeView::currentChanged(const QModelIndex&, const QModelIndex&)' is protected** – Sean Jun 26 '18 at 18:13
  • `QTreeView::currentChanged` is *not* a signal so you can't `connect` to it. It's a normal `virtual` member function so you need to define a class that inherits from `QTreeView` and override the `currentChanged` member as you would any other `virtual` member. – G.M. Jun 26 '18 at 18:31
  • 1
    @G.M. I'd love to do like that. But since I used **.ui** interface , I haven't found how to make this work. – Sean Jun 26 '18 at 18:45

1 Answers1

0

I hit a similar issue (the slot wasn't being triggered). Following @G.M.'s pointer in the comments to your question, it turned out that the call to ui->treeView->selectionModel() returns null if there's no model yet (and setSelectionModel() hasn't yet been called, presumably).

If I populated my QTreeView prior to calling ui->treeView->selectionModel() within the connect() call then I received my non-null response and the slot was triggered by the signal as desired.

eff
  • 385
  • 2
  • 8