Can't connect (null) :: selectionChanged in QTableView

I have the following raising a 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 signal, selectionChanged QRightClickableTableView but getting 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 the error:

QObject::connect: Cannot connect (null)::selectionChanged(QItemSelection, QItemSelection) to MyApp::on_tableViewSelectionChanged(QItemSelection)

      

+4


source to share


1 answer


Signal slot connection failed because it table->selectionModel()

returned null.



If you set a model for your table before connecting the signal slot, table->selectionModel()

will return a valid model, making the signal slot connection successful.

+5


source







All Articles