Items' pos return wrong position
-
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. -
@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. -
@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); }
-
@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());