我创建了一个利用三个不同类对象的应用程序。基本上我有第一类"所有界面和主要代码"第二个是简单的rect对象,最后一个是不同rect之间的边缘(或多或少在elasticnode示例中)。基本上每次用户移动矩形时,边缘都会被修改,我需要播种"长度"到主班。
这是运动的代码:
void Edge::adjust()
{
QLineF line(mapFromItem(source, 0, 0), mapFromItem(dest, 0, 0));
prepareGeometryChange();
QPointF edgeOffset(5, 5);
sourcePoint = line.p1() + edgeOffset;
destPoint = line.p2() + edgeOffset;
length_reff = sqrt((source->x()-dest->x())*(source->x()-dest->x())+(source->y()-dest->y())*(source->y()-dest->y()));
emit length_COMPUTED(length_reff);
//Here I have to send the lenght_ref variable to the MainWindow class
}
我尝试以这种方式实现SIGNAL / SLOT:
Edge.h:
public:
Edge(MyItem *sourceNode, MyItem *destNode);
void adjust();
signals:
void length_COMPUTED(qreal &length_reff);
MainWindow.h:
class MainWindow: public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
...
public slots:
void official_length_computation();
...
Mainwindow.cpp中的:
this->connect(this, SIGNAL(length_COMPUTED(&length_reff)), this, SLOT(official_length_computation()));
我想我对连接功能完全错了。
任何帮助?
由于
Edge.cpp
Edge::Edge(MyItem *sourceNode, MyItem *destNode) : arrowSize(10)
{
setAcceptedMouseButtons(0);
source = sourceNode;
dest = destNode;
source->addEdge(this);
dest->addEdge(this);
adjust();
}
void Edge::adjust()
{
QLineF line(mapFromItem(source, 0, 0), mapFromItem(dest, 0, 0));
prepareGeometryChange();
QPointF edgeOffset(5, 5);
sourcePoint = line.p1() + edgeOffset;
destPoint = line.p2() + edgeOffset;
length_reff = sqrt((source->x()-dest->x())*(source->x()-dest->x())+(source->y()-dest->y())*(source->y()-dest->y()));
emit length_COMPUTED(length_reff);
}
连接声明:
test1 = new MyItem();
test2 = new MyItem();
Edge *myEdge = new Edge(test1,test2);
this->connect(myEdge, SIGNAL(length_COMPUTED( qreal )), this, SLOT(official_length_computation( qreal)));
答案 0 :(得分:2)
this-> connect(this,SIGNAL(length_COMPUTED(& length_reff )),这个, SLOT(official_length_computation()));
大胆是错误的部分。必须是:... SIGNAL(length_COMPUTED( qreal& )...
很可能你想要你的插槽中的变量......所以:
this-> connect(this,SIGNAL(length_COMPUTED( qreal& )),这个, SLOT(official_length_computation( qreal& )));
但是你必须将这个变量添加到official_length_computation中。
好的,不确定你是否只是缩短了代码。但是当Edge应该发出信号时,它必须是QObject并使用Q_OBJECT宏。
以这种方式更改Edge.h:
class Edge: public QObject {
Q_OBJECT
..并相应地调整其构造函数