vendredi 1 mars 2019

QUdpSocket not sending collision Datagram to client

Following my previous post I found out that the following modification should be part of the possible solution connect(item, &MyItem::collided, this, &Widget::on_boxCollision); and the code should have this possible implementation:

widget.h

public:
    explicit Widget(QWidget *parent = nullptr);
    ~Widget();
private slots:
    void on_sendBtn_clicked();
    void on_pushButton_clicked();
public slots:
   void on_boxCollision(int x, int y); // <-- Added
#endif // WIDGET_H

widget.cpp

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    // operations
    int itemCount = 1;
    for(int i = 0; i < itemCount; i++) {
        MyItem *item = new MyItem();
        scene->addItem(item);
        connect(item, &MyItem::collided, this, &Widget::on_boxCollision); // <-- Added connection 
    }
}

void Widget::on_boxCollision(int x, int y) // <-- Added new 
{
    auto datagram = QString("%1, %2").arg(x).arg(y).toLatin1();
    mSocket->writeDatagram(datagram, QHostAddress::Broadcast,ui->spinBox->value());
}

and that on myitem.h the following should be added:

signals:
    void collided(int x, int y); // <-- Added
private:
    void doCollision();
};
#endif // MYITEM_H

and finally on myitem.cpp

void MyItem::doCollision()
{
    // operations
    }
    // see if the new position is in bounds
    QPointF newPoint = mapToParent(-(boundingRect().width()), -(boundingRect().width() + 2));
    if(!scene()->sceneRect().contains((newPoint))){
        newPoint = mapToParent(0, 0); // move it back in bounds
    }else{
        setPos(newPoint); // set new position
    }
    collided((int)newPoint.x(), (int)newPoint.y()); // <-- Added
}

However the client is still not able to receive the information regarding the collision coordinates of the moving square with the boundaries of the QGraphicsScene and the square with the obstacle. I think the connect(item, &MyItem::collided, this, &Widget::on_boxCollision); could be correct but it may not and I am not sure about it.

What I tried so far in addition to what I already tried previously here:

I watched this tutorial to make sure that I was going in the right direction and also I was looking at this other source. They are very short tutorial but I thought that could solve my problem but no luck so far. Just to cross off a possible doubt you may have, I included this QT += core gui network in the .pro file both in Client and Server.

This is also a Link from official QT documentation that explains how the connection should happen.

I am not sure at this point that this could be possible to do in Qt but wanted to ask if anyone has ever encountered this problem and if it is solvable.

Aucun commentaire:

Enregistrer un commentaire