Cannot connect (null)::selectionChanged to QTableView
Asked Answered
B

1

7

I have the following promoted QTableView:

class QRightClickableTableView : public QTableView {
  Q_OBJECT
public:
  explicit QRightClickableTableView(QWidget *parent = 0): QTableView(parent) {}

private slots:
  void mouseReleaseEvent(QMouseEvent *e) {
    if(e->button()==Qt::RightButton)
      emit rightClicked();
    else if (e->button()==Qt::LeftButton)
      emit leftClicked();
  }

signals:
  void rightClicked();
  void leftClicked();
};

When binding the selectionChanged signal of QRightClickableTableView, but getting an error. In .cpp:

QRightClickableTableView *table = ui->dataTableView;
connect(table, SIGNAL(leftClicked()), this, SLOT(on_tableViewLeftClicked()));
connect(table, SIGNAL(rightClicked()), this, SLOT(on_tableViewRightClicked()));

connect(table->selectionModel(), SIGNAL(selectionChanged(QItemSelection, QItemSelection)),
    SLOT(on_tableViewSelectionChanged(QItemSelection)));
table->setModel(model);

The leftClicked and rightClicked signals work as expected, but I get error:

QObject::connect: Cannot connect (null)::selectionChanged(QItemSelection, QItemSelection) to MyApp::on_tableViewSelectionChanged(QItemSelection)
Bamford answered 12/6, 2015 at 1:2 Comment(2)
When do you set the model for your table view? Try setting it before making the signal slot connectionHorsefaced
Duh. Thanks. That was it. Can you make that an answer so I can accept?Bamford
H
9

The signal slot connection has failed since table->selectionModel() has returned null.

If you set the model for your table before making signal slot connection, table->selectionModel() will return a valid model, making the signal slot connection successful.

Horsefaced answered 12/6, 2015 at 1:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.