QTableView selectionChanged

14

我有一个 QTableView,需要获取其 selectionChanged 事件。但是我好像无法让连接工作。我尝试了以下代码:

MyWidget.h

...

protected slots:
 void slotLoadTransaction(const QItemSelection & selected, const QItemSelection & deselected);
private:
 QTableView table;

MyWidget.cpp

这是一个文件名为MyWidget.cpp的文件。

 connect(
  table->selectionModel(),
  SIGNAL(selectionChanged(const QItemSelection & selected, const QItemSelection & deselected)),
  this,
  SLOT(slotLoadTransaction(const QItemSelection & selected, const QItemSelection & deselected))
 );

运行时出现“无此信号”错误。

1个回答

21
你需要从SIGNAL和SLOT宏中删除变量名:
 connect(
  table->selectionModel(),
  SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
  SLOT(slotLoadTransaction(const QItemSelection &, const QItemSelection &))
 );

Connect 的本质是查看函数签名和变量名称,这会让它感到困惑。


2
你似乎忘记在语句中加入“this”(SIGNAL和SLOTS之间的参数),是吗?+1 - Andy M
安迪,我想我可以理解那一点。卡莱布,感谢您先生!运行得很好 :) - David Souther
4
@Andy - 不,我有意忽略了它。上述语法更加简洁且在接收者为“this”时等效。有两个连接方法,一个是连接实例方法(我使用了它),另一个是静态方法。详见文档以获取更多信息。 - Kaleb Pederson
3
@Andy:他实际上没有忘记“this”……如果你从QObject继承,你会有一个重载版本的connect函数,它使用给定的参数并将“this”假定为信号对应的对象。 - Caleb Huitt - cjhuitt

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接