Qt Forum

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

    Unsolved How do I position QGraphicsItem rectangle to the top left of my QGraphicsView?

    General and Desktop
    2
    2
    137
    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.
    • L
      lansing last edited by

      I have created a QGraphicsView in designer and a custom QGraphicsItem. I want to have my item to be the same width as the view and position begin from the top left corner of the view.

      My view size was set to have width=1000px and height=120px, and my item is height=50px. I ran into a problem with my item positioned vertically in the center of my view and I don't know how to move to the the top left corner.

      my_app.cpp

      my_app constructor() {
          myScene = new QGraphicsScene(this);
          m_ui.myView->setScene(myScene );
          myItem = new MyQGraphicsItem();
      }
      
      void run_app() {
          myItem->setViewWidth(m_ui.myView->width());
          myScene->addItem(myItem);
      }
      
      

      MyQGraphicsItem.cpp

      QRectF MyQGraphicsItem::boundingRect() const
      {    
          return QRectF(0, 0, m_viewWidth, 50);
      }
      
      void MyQGraphicsItem::setViewWidth(int a_viewWidth)
      {
          m_viewWidth = a_viewWidth;
      }
      

      I tried changing the x and y arguments in the boundingRect but the item always stays vertically in the center of the view.

      1 Reply Last reply Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by SGaist

        Hi,

        You should likely call setPos on your objects.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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