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

Problem with connect with object created dynamically



  • I have a class that emit a signal in this way:

    void CustomTracer::mouseMoveEvent(QMouseEvent *event)
    {
        /* let the crosshair cursor follow the mouse position */
    
        if (click_right == BTN_FALSE)
        {
        if(!mouseMarkerX) mouseMarkerX = new QCPItemStraightLine(this);
        if(!mouseMarkerY) mouseMarkerY = new QCPItemStraightLine(this);
    
         positionX = xAxis->pixelToCoord( event->pos().x() );
    
        emit sign_tracer_bis();
    }
    

    in MainWindow:

    void MainWindow::Altezza_LdC_Acq()
    {
        QDockWidget *dock = new QDockWidget;
        dock->setMinimumWidth(500);
        dock->setMaximumWidth(500);
        dock->setMinimumHeight(500);
        dock->setMaximumHeight(500);
    
        Altezza_acq_bis = new CustomTracer(dock);
        Altezza_acq_bis->resize(500,500);
        graph_Acq_bis = Altezza_acq_bis->addGraph();
        double a;
        double b;
        double m;
        double n;
        a= ui->Grafico_Altezza_LdC_Acquisizione->x1;
        b= ui->Grafico_Altezza_LdC_Acquisizione->x2;
        m= ui->Grafico_Altezza_LdC_Acquisizione->y1;
        n= ui->Grafico_Altezza_LdC_Acquisizione->y2;
        Altezza_acq_bis->xAxis->setRange(a,b);
        Altezza_acq_bis->yAxis->setRange(m,n);
        graph_Acq_bis->setData(graph2->data());
    
        dock->setFloating(true);
        dock->show();
    
        QObject::connect(Altezza_acq_bis,SIGNAL(sign_tracer_bis()),this,SLOT(Tracer_Acq()));
    }
    

    and the slot is:

    
    void MainWindow::Tracer_Acq()
    
    {
    
        double x = (Altezza_acq_bis->positionX);
        int j;
        double u;
        double posX;
    
    
        QString h = QString::number(x,'f',2);
        double x1 = h.toDouble();
        for (int i=0; i<1000; i++)
        {
            if  (abs(xalt[i] - x1) <0.001)
            {
    
               u= xalt[i];
               j=i;
    
               Altezza_acq_bis->positionY = f_altezza[j];
               posX = xalt[j];
               if((Altezza_acq_bis->mouseMarkerX) && (Altezza_acq_bis->mouseMarkerY))
    
               {
                  Altezza_acq_bis->mouseMarkerX->point1->setCoords(Altezza_acq_bis->positionX, 0);//VERTICALE
                  Altezza_acq_bis->mouseMarkerX->point2->setCoords(Altezza_acq_bis->positionX, 1);//VERTICALE
                  Altezza_acq_bis->mouseMarkerY->point1->setCoords(0,Altezza_acq_bis->positionY);//ORIZZONTALE
                  Altezza_acq_bis->mouseMarkerY->point2->setCoords(1, Altezza_acq_bis->positionY);//ORIZZONTALE
    
    
                  QString x_text = QString::number(Altezza_acq_bis->positionX,'f',2);
                  QString y_text = QString::number(Altezza_acq_bis->positionY);
    
                  QString coord_p = QString("%1, %2").arg(x_text,y_text);
                  QCursor cursor;
                  QToolTip::showText(cursor.pos(),coord_p,this,QRect(),2000);
    
               }}
    
    
            }
               Altezza_acq_bis->replot();
         }
    


  • What is the problem?
    First thing to try is replacing QObject::connect(Altezza_acq_bis,SIGNAL(sign_tracer_bis()),this,SLOT(Tracer_Acq())); with QObject::connect(Altezza_acq_bis,&CustomTracer::sign_tracer_bis,this,&MainWindow::Tracer_Acq);



  • @VRonin said in Problem with connect with object created dynamically:

    QObject::connect(Altezza_acq_bis,&CustomTracer::sign_tracer_bis,this,&MainWindow::Tracer_Acq);

    thanks very much!!! I wrote bad the connect...now it's ok!! thanks


Log in to reply