Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Unsolved Items' pos return wrong position

    QML and Qt Quick
    custom widget draw lines qlabel position
    2
    5
    1281
    Loading More Posts
    • 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.
    • M
      mghqt last edited by mghqt

      Hello all,
      I'm currently trying to make a custom widget which is consisted of some labels and in addition I have to connect some labels by drawing line between them. The problem I faced is that labels which are in layouts seem to returning wrong positions using their pos() function. I tried different types of mapTo functions but still there's a problem.

      Can anyone help me with that?
      thanks in advance for your help.

      raven-worx 1 Reply Last reply Reply Quote 0
      • raven-worx
        raven-worx Moderators @mghqt last edited by

        @mghqt said in Items' pos return wrong position:

        I tried different types of mapTo functions but still there's a problem.

        show some of the different types you already tried.
        Or elaborate your issue with a screenshot etc.

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        M 1 Reply Last reply Reply Quote 0
        • M
          mghqt last edited by mghqt

          This post is deleted!
          1 Reply Last reply Reply Quote 0
          • M
            mghqt @raven-worx last edited by

            @raven-worx
            thank you for your response. this is the paintEvent function and a screenshot of where it draws the rect according to different mapTo functions:

            void CWidget::paintEvent(QPaintEvent *event)
            {
                QPainter painter(this);
                QPen pen;
                int x,y,width,height;
                pen.setWidth(2);
                pen.setColor(Qt::blue);
                painter.setPen(pen);
                //    painter.drawRect(this->rect());
                QPoint points[]={
                    QPoint(.9*this->width(),20),
                    QPoint(.9*this->width(),160),
                    QPoint(.5*this->width(),90)
                };
            
                painter.setPen(pen);
                QPoint pos1=ui->label_2->pos();
                QPoint pos2=ui->label_2->mapToParent(pos1);
                QPoint pos3=ui->label_2->mapToGlobal(pos1);
                QPoint pos4=ui->label_2->mapTo(this,pos1);
                x=ui->label_2->mapToParent(pos1).x();
                y=ui->label_2->mapToParent(pos1).y();
                int form_x=(this->pos()).x();
                int form_y=(this->pos()).y();
                //        qDebug()<<"pos1= "<<pos1<<" pos2="<<pos2<<" pos2="<<pos2;
                qDebug()<<"form: "<<this->pos()<<", map to global: "<<this->mapToGlobal(this->pos());
                width=ui->label_2->width();
                height=ui->label_2->height();
            
                painter.drawRect(pos1.x(),pos1.y(),width,height);
                painter.drawRect(pos2.x(),pos2.y(),width,height);
                painter.drawRect(pos3.x()-form_x,pos3.y()-form_y,width,height);
                painter.drawRect(pos4.x(),pos4.y(),width,height);
            
            
            }
            

            0_1505194095180_Capture.PNG

            raven-worx 1 Reply Last reply Reply Quote 0
            • raven-worx
              raven-worx Moderators @mghqt last edited by

              @mghqt
              i assume CWidget class is the main widget (window)?
              How do you compose/layout the form items? Are you using sub-widgets containers?

              Anyways you should do like you did for pos4 if you want to map a child to a parent.

              QPoint childPos=ui->label_2->mapTo(this, ui->label_2->pos());
              

              --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
              If you have a question please use the forum so others can benefit from the solution in the future

              1 Reply Last reply Reply Quote 0
              • First post
                Last post