Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

dynamically adding and updating inside one QGraphicsScene



  • Hello,
    I want to see data coming from scanners in one QGraphicsScene together. I will get data in main window as vector of float for one scanner at a time.

    after few milliseconds again data come from another scanners or the same scanner, if data is from same scanner (ex scanner 1) I will delete data from earlier and update with new one. but if data comes from another scanner (ex scanner 2) I will add it in same QGraphicsScene while keeping older scanner data (ex scanner 1) and so on...only deleting data if new one come.

    for one scanner, with QGraphicsView and add ellipse I can see data in widget easily like below.

    but is it possible to do like above case in one QGraphicsView for more scanners? any hint?

    QWidget* widget_scans;
    QVBoxLayout* layout_scans;
    
    QGraphicsView* view_scans;
    QGraphicsScene* scene_scans;
    
    this->widget_scans = new QWidget();
    this->layout_scans = new QVBoxLayout();
    this->widget_scans->setLayout(layout_scans);
        
    this->layout_main->addWidget(this->widget_scans);
    
    this->view_scans = new QGraphicsView();
    this->scene_scans = new QGraphicsScene();
    this->view_scans->setScene(this->scene_scans);
    this->layout_scans->addWidget(this->view_scans);
    
    MainWindow::processDisplay(size_t _device_id, std::vector<float> _scan_vec_data) {
    this->scene_scans->clear();
    	
    	double SCALE = 1.0;
    	
    	QPen pen = QPen();
    	pen.setColor(Qt::black);
    
    	pen.setWidth(1);
    
    	for (int i = 0; i < _scan_vec_data.size() - 1; i += 2) {
    
    	 qreal x_draw = _scan_vec_data[i];
    	 qreal y_draw = _scan_vec_data[i + 1];
    	 double RADIUS = 0.5;
    
    	 QPen pen = QPen();
    	 pen.setColor(Qt::blue);
    		
    	 this->scene_scans->addEllipse((x_draw , -y_draw , (2.0 * RADIUS) * SCALE, (2.0 * RADIUS) * SCALE, pen);
    
    	}
    )
    

    thanks.


  • Lifetime Qt Champion

    Hi,

    Rather than deleting everything, why not store a map of scanner/ellipse(s) and update or add a new one depending on where the data comes from ?



  • @SGaist yes map is the solution.



  • @SGaist when I store data in map, and iterate through it I can see ellipses in widget, but when new data comes I can only see one ellipse is updated not others. ex I have simulated second scanner data and added into the map. ?

    std::vector<float> second_scan_data;
    	
    
    	for (int i = 0; i < _scan_vec_data.size(); ++i) {
    		second_scan_data.push_back(_scan_vec_data[i]-20);
    	}
    	
    this->data.insert(std::make_pair(d, second_scan_data));
    
    //show all scanners data
    	for (std::map<size_t, std::vector<float>>::iterator it = data.begin(); it != data.end(); ++it) {
    		
    		for (int i = 0; i < it->second.size() - 1; i += 2) {
    		qreal x_draw = it->second[i];
    		qreal y_draw = it->second[i + 1];
    		double RADIUS = 0.5;
    			
    		this->scene_scans->addEllipse((x_draw - RADIUS) * SCALE, -(y_draw - RADIUS) * SCALE, (2.0 * RADIUS) * SCALE, (2.0 * RADIUS) * SCALE, pen);
    
    		}
    		
    	}
    

  • Lifetime Qt Champion

    If you have a list of ellipses then you should store that list otherwise you are going to replace it each time.



  • This post is deleted!

Log in to reply