我有以下提升的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();
};
绑定QRightClickableTableView的selectionChanged信号时,却收到错误。在.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);
leftClicked和rightClicked信号按预期工作,但我收到错误:
QObject::connect: Cannot connect (null)::selectionChanged(QItemSelection, QItemSelection) to MyApp::on_tableViewSelectionChanged(QItemSelection)
答案 0 :(得分:5)
由于table->selectionModel()
返回null,信号槽连接失败。
如果在进行信号槽连接之前为表设置模型,table->selectionModel()
将返回有效模型,使信号槽连接成功。