Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Items' pos return wrong position

Items' pos return wrong position

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
custom widgetdraw linesqlabel position
5 Posts 2 Posters 1.8k Views
  • 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 Offline
    M Offline
    mghqt
    wrote on last edited by mghqt
    #1

    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-worxR 1 Reply Last reply
    0
    • M 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-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @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
      0
      • M Offline
        M Offline
        mghqt
        wrote on last edited by mghqt
        #3
        This post is deleted!
        1 Reply Last reply
        0
        • raven-worxR raven-worx

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

          M Offline
          M Offline
          mghqt
          wrote on last edited by
          #4

          @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-worxR 1 Reply Last reply
          0
          • M mghqt

            @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-worxR Offline
            raven-worxR Offline
            raven-worx
            Moderators
            wrote on last edited by
            #5

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

            • Login

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