【发布时间】:2012-04-23 19:53:38
【问题描述】:
我有一个从 QGraphicsPixmapItem 派生的自定义类。它被称为 GraphPixmapItemCustom,它的重载方法是:
void GraphPixmapItemCustom::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
QGraphicsPixmapItem::mousePressEvent(event);
GraphMarkItemCustom *ptr;
if(event->button() == Qt::RightButton)
{
qDebug("Before emit");
emit addPoint(QPointF(event->pos().x(), event->pos().y()));
qDebug("After emit");
markList.append(new GraphMarkItemCustom(QPointF(event->pos().x(), event->pos().y())));
ptr = markList.last();
markGroup->addToGroup(ptr);
//this->scene()->addItem(ptr);
}
}
信号在标题中声明:
signals:
void addPoint(QPointF position);
在主类中,有一个指向名为 GraphPixmapItemCustom 的对象的指针
private:
GraphPixmapItemCustom *pixItemRGB;
在主类中我有一个名为:
private slots:
void pointAdd(QPointF position);
在主类构造函数中我有一个连接:
connect(pixItemRGB, SIGNAL(addPoint(QPointF)), this, SLOT(pointAdd(QPointF)));
在插槽中我只有qDebug("YUPPY IT ACTUALLY WORKS!");
但是插槽没有被触发。为什么?我已经删除了所有 moc 文件和所有不需要的东西。所以只有.pro、.h 和.cpp 文件以及.ui 表格留下了必要的文件。
我查看了 Google 的大多数点击量。什么是最好的(我不确定 - 也许我修改了一些东西)它起作用了!我记得这种连接的工作效果。救命!
【问题讨论】:
-
“发射前”和“发射后”是否打印在控制台上?对
connect的调用是否返回true? -
除了QGraphicsPixmapItem之外你忘了继承QObject吗?
-
是的,它们被打印出来并且连接返回 true。
-
是的,我从 QObject 继承 f。
-
您是否在 使用“new”创建 pixItemRGB 之后连接了信号?
标签: qt connection signals signals-slots slot