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

how to append points in a QList from a signal sent to a slot?



  • I'm developing a program that is based in a mainwindow and in a QWidget named Diagrama(that is the central widget of this window).

    Besides that i have a class named CustomLabel ans its responsible to emit a signal when a click a label with its position. To deal with this signal, i have a public slot named showMousePosition that (should) append the point amitted by the signal in a QList.

    The goal here is to click in 2 labels and draw a line between them. I already did some tests, and the problem is that the slot doesn't append the points in a list at all :(

    How can I fix this?



  • @frnklu20 could you show your code? How the signal is defined, when it's emitted, how the slot is defined?



  • @Pablo-J.-Rogina
    Sure!

    customlabel.cpp

    /void CustomLabel::mousePressEvent(QMouseEvent *event){
    
    
        QPoint mouse_pos=event->pos();
    
        if(event->button()==Qt::RightButton){
    
        if(mouse_pos.x()<=this->size().width() && mouse_pos.y()<=this->size().height()){
    
        if(mouse_pos.x()>0 && mouse_pos.y()>0){
              emit sendMousePosition(this->pos());
           }
        }
        }
        else {
            event->ignore();
    
        }
    }
    
    

    diagrama.cpp

    ....
    void Diagrama::showMousePosition(const QPoint &pos)
    {
        QPoint p=pos;
        pontos.append(p);
        update();
    }
    ....
    void Diagrama::mousePressEvent(QMouseEvent *event)
    {....
    
    else if(modo=="linha")
        {
            if(pontos.length()==2){
    
                p_ini=pontos.first();
                p_fin=pontos.last();
                if(p_fin==p_ini)
                {pontos.removeLast();}
                drawLineTo(p_ini,p_fin);
                 drawing=true;
                event->accept();
                update();
               }
            update();
            }
    
            else {
                event->ignore();
                drawing=false;
            }
        }
    .....
    void Diagrama::drawLineTo(const QPoint &ini_point,const QPoint fin_point)
    {
        QPen canetinha;
        canetinha.setColor(mColor);
        canetinha.setWidth(mSize);
        mPainter->setPen(canetinha);
        mPainter->drawLine(ini_point,fin_point);
        update();
    }
    

    The idea here is to append the points in a list, and when the lenght of this list is equal 2, plot a line between these 2 points.


  • Lifetime Qt Champion

    Hi
    Are you sure its not appended?
    please check it enters ( i assume its here u add )
    void Diagrama::showMousePosition(const QPoint &pos)
    {
    qDebug() << "in showMousePosition with pos :" <<pos;
    QPoint p=pos;
    pontos.append(p);
    update();
    }
    One thing i wonder.
    You emit
    emit sendMousePosition(this->pos());
    but if thats is inside other class, the this->pos() might be in local coordinates
    (like 10,10 ) and not in parent coordinates like x,y of the label.



  • @mrjj

    QPoint p=pos;
     pontos.append(p);
    

    I don't know how this works (I'm PyQt not C++), does that copy p onto the list or does that not work because p goes out of scope and you need to use a heap object?


  • Lifetime Qt Champion

    @JonB
    It would be a good guess in many cases
    but here it will just copy pos to p and p will be copied to the internal instance in the list.



  • @mrjj
    OK., thanks. This is because the docs do not say it inherits from QObject, which I understand can't be copied, right?

    @fmkenna

    the problem is that the slot doesn't append the points in a list at all

    Well then check in the debugger that pontos.append(p) is indeed getting called, and also if/when the pontos.removeLast(); gets called? I don't think you've shown us the declaration/scope of pontos, which I presume is what you are saying does not get appended to?


  • Lifetime Qt Champion

    @JonB said in how to append points in a QList from a signal sent to a slot?:

    OK., thanks. This is because the docs do not say it inherits from QObject, which I understand can't be copied, right?

    Yes. QObjects cannot be copied due to signals and slots and identity. (with more)
    QPoint and most of QPen, QBrush etc are not QObjects and meant to be copied.


Log in to reply