Results 1 to 2 of 2

Thread: Coordinates of a widget inside QGridLayout

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2014
    Posts
    1
    Qt products
    Qt4 Qt5

    Default Coordinates of a widget inside QGridLayout

    Hi everyone !

    I am trying to find the coordinates of a class derived from a QWidget inside a QGridLayout relative to its parent. I have tried several things such as x() and y() and pos() (returned 0,0) from QWidget class, adding a QRect to the class and using getCoords from QRect class and the mapToParent class from QWidget but it always returns 0. I can't seem to find the anser so I seek your help !

    thanks in advance for your help, you can find the code of my program attached to this post.

    render_area.cpp

    Qt Code:
    1. #include "render_area.hpp"
    2.  
    3. #include <GL/glu.h>
    4. #include <iostream>
    5. #include <cmath>
    6.  
    7. #include <QPainter>
    8. #include <QMouseEvent>
    9. #include <QKeyEvent>
    10. #include <QGridLayout>
    11.  
    12.  
    13. render_area::render_area(QWidget *parent)
    14. :QWidget(parent)
    15. {
    16. setFocusPolicy(Qt::StrongFocus);
    17. setBackgroundRole(QPalette::Base);
    18. setAutoFillBackground(true);
    19.  
    20. bar=new QRect(width()/2,height()+50,150,10);
    21. grid=new Grid(this);
    22. grid->fill_grid(this);
    23. std::cout<<"Grid filled !"<<std::endl;
    24.  
    25. QPoint point;
    26. point=grid->getBrick(5)->pos();
    27. grid->getBrick(5)->mapToParent(point);
    28. std::cout<<point.y()<<std::endl;
    29.  
    30. dt=1/30.0f;
    31. connect(&timer, SIGNAL(timeout()), this, SLOT(update_timer()));
    32. timer.start(5);
    33.  
    34. draw_circle=true;
    35.  
    36. x_old=0;
    37. y_old=0;
    38. x_old2=0;
    39. y_old2=0;
    40.  
    41. xc=500;
    42. yc=500;
    43. diametre=15;
    44.  
    45. vxc=20.0f;
    46. vyc=-20.0f;
    47.  
    48. is_fixed=false;
    49. }
    50.  
    51. render_area::~render_area()
    52. {}
    53.  
    54.  
    55.  
    56. void render_area::paintEvent(QPaintEvent*)
    57. {
    58. QPainter painter(this);
    59. painter.setRenderHint(QPainter::Antialiasing, true);
    60. painter.drawRect(*bar);
    61.  
    62. QPen pen;
    63. pen.setWidth(1.0);
    64. pen.setColor(Qt::blue);
    65. painter.setPen(pen);
    66.  
    67. QBrush brush = painter.brush();
    68. brush.setColor(Qt::gray);
    69. brush.setStyle(Qt::SolidPattern);
    70. painter.setBrush(brush);
    71.  
    72. if(draw_circle)
    73. painter.drawEllipse(xc,yc,diametre,diametre);
    74. }
    75.  
    76. void render_area::change_draw_circle()
    77. {
    78. draw_circle=!draw_circle;
    79. repaint();
    80. }
    81.  
    82. void render_area::keyPressEvent(QKeyEvent *event)
    83. {
    84. switch(event->key())
    85. {
    86. case Qt::Key_Right:
    87. bar->translate(15,0);
    88. break;
    89. case Qt::Key_Left:
    90. bar->translate(-15,0);
    91. break;
    92. default:
    93. break;
    94. }
    95. }
    96.  
    97. void render_area::mousePressEvent(QMouseEvent *event)
    98. {
    99. x_old=event->x();
    100. y_old=event->y();
    101. std::cout<<event->x()<<"|"<<event->y()<<std::endl;
    102. is_fixed=true;
    103. }
    104.  
    105. void render_area::mouseReleaseEvent(QMouseEvent *event)
    106. {
    107.  
    108. float x=event->x();
    109. float y=event->y();
    110.  
    111.  
    112. vxc=60*(x-x_old2);
    113. vyc=60*(y-y_old2);
    114.  
    115. is_fixed=false;
    116.  
    117. }
    118.  
    119. void render_area::mouseMoveEvent(QMouseEvent *event)
    120. {
    121. int x=event->x();
    122. int y=event->y();
    123.  
    124. float R=diametre/2.0f;
    125. float x0=xc+R;
    126. float y0=yc+R;
    127.  
    128. if( (x-x0)*(x-x0)+(y-y0)*(y-y0)<R*R )
    129. {
    130. xc+=(x-x_old);
    131. yc+=(y-y_old);
    132. }
    133.  
    134. x_old2=x_old;
    135. y_old2=y_old;
    136. x_old=x;
    137. y_old=y;
    138.  
    139. repaint();
    140. }
    141.  
    142. void render_area::update_timer()
    143. {
    144. int x1,x2,y1,y2;
    145. bar->getCoords(&x1,&y1,&x2,&y2);
    146.  
    147. if(is_fixed==false)
    148. {
    149. //gestion du sol
    150. if(yc+diametre>height())
    151. {
    152. vyc=-vyc;
    153.  
    154. }
    155.  
    156. if(xc>x1 && xc<x1+150)
    157. {
    158. if(yc+diametre>y1)
    159. {
    160. vyc=-vyc;
    161. }
    162. }
    163. //plafond
    164. if(yc-diametre<0)
    165. {
    166. vyc=-vyc;
    167. }
    168.  
    169. //bord droit
    170. if(xc+diametre>width())
    171. {
    172. vxc=-vxc;
    173. }
    174.  
    175. //bord gauche
    176. if(xc<0)
    177. {
    178. vxc=-vxc;
    179. }
    180.  
    181. //integration (Euler explicite)
    182. xc=xc+dt*vxc;
    183. yc=yc+dt*vyc;
    184.  
    185. }
    186. repaint();
    187. }
    188.  
    189. Grid* render_area::getGrid() const
    190. {
    191. return grid;
    192. }
    To copy to clipboard, switch view to plain text mode 

    grid.cpp

    Qt Code:
    1. #include "grid.hpp"
    2.  
    3. Grid::Grid(QWidget *parent) :
    4. QWidget(parent)
    5. {
    6. grid=new QGridLayout(parent);
    7. grid->setContentsMargins(0,0,0,0);
    8. grid->setAlignment(Qt::AlignTop);
    9. grid->setSpacing(2);
    10. }
    11.  
    12. void Grid::fill_grid(QWidget* parent)
    13. {
    14. for (int i=0;i<10;i++)
    15. {
    16. for (int j=0;j<12;j++)
    17. {
    18. brick* fillingBrick=new brick(parent);
    19. grid->addWidget(fillingBrick->getLabel(),j,i);
    20. qDebug()<<fillingBrick->parentWidget();
    21. brickVector.push_back(fillingBrick);
    22. }
    23. }
    24. }
    25.  
    26. std::vector<brick*> Grid::getBrickVector() const
    27. {
    28. return brickVector;
    29. }
    30.  
    31. brick* Grid::getBrick(int indice)
    32. {
    33. return brickVector.at(indice);
    34. }
    35.  
    36. QGridLayout* Grid::getGrid() const
    37. {
    38. return grid;
    39. }
    To copy to clipboard, switch view to plain text mode 

    brick.cpp

    Qt Code:
    1. #include "brick.hpp"
    2.  
    3. brick::brick(QWidget *parent) :
    4. QWidget(parent)
    5. {
    6. label=new QLabel;
    7. brickPic=new QPixmap(100,15);
    8. brickPic->fill(QColor(255,0,0));
    9. label->setPixmap(*brickPic);
    10. }
    11.  
    12. brick::~brick()
    13. {
    14. }
    15.  
    16. QLabel* brick::getLabel() const
    17. {
    18. return label;
    19. }
    20.  
    21. QPixmap* brick::getPixmap() const
    22. {
    23. return brickPic;
    24. }
    25.  
    26. QRect* brick::getRect() const
    27. {
    28. return rect;
    29. }
    30.  
    31. void brick::setRect(QRect recttmp)
    32. {
    33. int x,y,w,h;
    34. recttmp.getRect(&x,&y,&w,&h);
    35. rect=new QRect(x,y,w,h);
    36. }
    To copy to clipboard, switch view to plain text mode 

    I'll put here the things I tried to solve the problem :
    in the fill_grid member function : tried to change the parent of the brick by using setparent(parent) -> failed
    in the render_area contructor : tried adding show() before getting grid->getBrick(5)->pos() failed
    Even after showing the main windows fenetre.getRender()->getGrid()->getBrick(5)->x() shows 0
    in fill_grid member function : qDebug() << fillingBrick->parentWidget() shows "render_area(0x64f570)" which is good but the brick still can't find its position inside its parent (0,0)
    Attached Files Attached Files
    Last edited by kolqhoz; 29th March 2014 at 12:03.

Similar Threads

  1. Replies: 0
    Last Post: 28th March 2013, 00:22
  2. Replies: 14
    Last Post: 18th June 2011, 10:32
  3. getting to the buttons inside a QGridLayout
    By mr_kazoodle in forum Newbie
    Replies: 2
    Last Post: 19th February 2011, 16:48
  4. Resizing QGraphicsView inside a QGridLayout
    By mckinnon in forum Newbie
    Replies: 3
    Last Post: 9th September 2010, 10:15
  5. Absolute widget coordinates
    By rbp in forum Qt Programming
    Replies: 4
    Last Post: 28th January 2009, 04:30

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.