QObject::connect 与 QAbstractItemModel 的问题
我有一个 QListView,从中我获得了一个 QAbstractItemModel 与 list->model();
之后,我想将 dataChanged 信号连接到我的自定义 QObject:
if( QObject::connect(model, SIGNAL(dataChanged (const QModelIndex , const QModelIndex ) ),
customObject_,SLOT(onText(const QModelIndex , const QModelIndex )) ) )
cout << "SIGNAL SLOT connection successful" << endl;
else
cout << "SIGNAL SLOT connection ERROR" << endl;
这是我的自定义对象:
class CustomObject : public QObject
{
Q_OBJECT
public:
CustomObject (QObject *parent);
~CustomObject ();
public slots:
void onText(const QModelIndex & topLeft, const QModelIndex & bottomRight );
private:
};
我做错了什么吗? QObject 调用返回 true,我在 onText 函数中有一个 cout,但是当 QListView 更改时不会打印任何内容。
I have a QListView from which I obtain a QAbstractItemModel with list->model();
After this, I want to connect the dataChanged signal to a custom QObject of mine:
if( QObject::connect(model, SIGNAL(dataChanged (const QModelIndex , const QModelIndex ) ),
customObject_,SLOT(onText(const QModelIndex , const QModelIndex )) ) )
cout << "SIGNAL SLOT connection successful" << endl;
else
cout << "SIGNAL SLOT connection ERROR" << endl;
here is my custom object:
class CustomObject : public QObject
{
Q_OBJECT
public:
CustomObject (QObject *parent);
~CustomObject ();
public slots:
void onText(const QModelIndex & topLeft, const QModelIndex & bottomRight );
private:
};
Am I doing anything wrong? The QObject call returns true, I have a cout in the onText function, but nothing is ever printed when the QListView is Changed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这可能意味着信号永远不会发出。 尝试调用
model->setData( model->index( 0, 0 ), Qt::EditRole, 3.14 );
如果该函数没有调用您的槽,则
setData()
的实现可能存在错误,并且不会发出dataChanged(QModelIndex,QModelIndex)
,否则customObject_
已被删除。如果情况并非如此,您需要向我们提供更多信息。
That probably means that the signal is never emitted. Try calling
model->setData( model->index( 0, 0 ), Qt::EditRole, 3.14 );
If that one doesn't invoke your slot, then the implementation of
setData()
is probably buggy and doesn't emitdataChanged(QModelIndex,QModelIndex)
, or elsecustomObject_
has since been deleted.If neither is the case, you need to give us more information.
也许有 & 在你的函数中......
但如果这是问题,它应该显示你的函数的错误......
可能这个信号没有发出。 尝试连接另一个信号..你可以这样测试..
maybe there is & in your function..
but if it was the problem, it should display an error by your function...
probably this signal is not emmited. Try to connect with another signal.. you can test it like that..
你尝试过吗
? 也就是确保参数是通过引用传递的。
查看本教程。
Did you try with
? aka make sure the parameters are passed by reference.
Check this tutorial.