Cannot connect (null)::selectionChanged to QTableView

我怕爱的太早我们不能终老 提交于 2019-12-01 17:57:10

问题


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)

回答1:


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.



来源:https://stackoverflow.com/questions/30793799/cannot-connect-nullselectionchanged-to-qtableview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!