lundi 4 mai 2020

How to broadcast a specific QString on a QListView

The problem: I am trying to broadcast a specific QString on a QListView as shown below. So in case I have specific measurments, I see them and they are published in the QListView, but if there are no measurements, I would like to broadcast a specific string " No Broadcasted data "

Currently happening:

error

The Expected Result in case no measures are passed:

correct

Below a snippet of the code I am using for the specific problem:

qnode.hpp

class QNode : public QThread {
Q_OBJECT

public:
    QNode(int argc, char** argv, const std::string &name );
    virtual ~QNode();

    QStringListModel* getMinDistanceModelQuad3() { return &minDistanceModelQuad3; }

protected:
    QStringListModel minDistanceModelQuad3;

};

#endif /* NODE_HPP_ */

listener.h

class Listener : public QNode {
    Q_OBJECT
public:
    Listener(int argc, char** argv);
    ~Listener();

private:
    void minimumDistanceQuad3Callback(const lidar_boat_detection::min_distance::ConstPtr& msg);
    ros::Subscriber min_distance_quad3_sub;
    QString distanceBroadcasted;
};

listener.cpp <-- Where the messages are either published or no data is broadcasted

void Listener::minimumDistanceQuad3Callback(const lidar_boat_detection::min_distance::ConstPtr& msg)
{
  if(distanceBroadcasted.isEmpty()) {
    std::stringstream logging_msg;
    logging_msg << "[ INFO]"
        << " No Broadcasted data ";
    QVariant new_row(QString(logging_msg.str().c_str()));
  } else {
    ROS_INFO("Minimum Distance: [%f]", msg->distance);
    minDistanceModelQuad3.insertRows(0,1);
    std::stringstream logging_msg;
    logging_msg << "[ INFO]"
        << " Minimum Distance Quad 3: " << msg->distance;
    QVariant new_row(QString(logging_msg.str().c_str()));
    minDistanceModelQuad3.setData(minDistanceModelQuad3.index(0),new_row);
  }
}

I have been researching this problem for a while and I am not sure if this not proper behavior is due to a non-correct use of the QVariant property. Also I think I am correctly adding the correct message but despite that, it seems that the QListView receives measurements instead of "No Data Broadcasted " string. I am not sure if I am doing something wrong on the if - else loop.

Thanks for pointing to the right direction for solving this problem.

Aucun commentaire:

Enregistrer un commentaire