Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. how to append points in a QList from a signal sent to a slot?

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 4 Posters 1.3k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    frnklu20
    wrote on last edited by
    #1

    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?

    Pablo J. RoginaP 1 Reply Last reply
    1
    • F frnklu20

      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?

      Pablo J. RoginaP Offline
      Pablo J. RoginaP Offline
      Pablo J. Rogina
      wrote on last edited by
      #2

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

      Upvote the answer(s) that helped you solve the issue
      Use "Topic Tools" button to mark your post as Solved
      Add screenshots via postimage.org
      Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • F Offline
        F Offline
        frnklu20
        wrote on last edited by frnklu20
        #3

        @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.

        1 Reply Last reply
        1
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #4

          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.

          JonBJ 1 Reply Last reply
          0
          • mrjjM mrjj

            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.

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #5

            @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?

            mrjjM 1 Reply Last reply
            1
            • JonBJ JonB

              @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?

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @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.

              JonBJ 1 Reply Last reply
              2
              • mrjjM mrjj

                @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.

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #7

                @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?

                mrjjM 1 Reply Last reply
                0
                • JonBJ JonB

                  @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?

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @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.

                  1 Reply Last reply
                  2

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved