无法连接(null):: selection更改为QTableView [英] Cannot connect (null)::selectionChanged to QTableView
问题描述
我有以下升级的QTableView:
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();
};
绑定QRightClickableTableView的selectionChanged信号时,出现错误.在.cpp中:
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);
leftClicked和rightClicked信号正常工作,但出现错误:
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)
推荐答案
由于 table-> selectionModel()
返回null,信号插槽连接失败.
The signal slot connection has failed since table->selectionModel()
has returned null.
如果您在建立信号插槽连接之前为表设置了模型,则 table-> selectionModel()
将返回有效的模型,从而使信号插槽连接成功.
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.
这篇关于无法连接(null):: selection更改为QTableView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!