Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Language Bindings
  4. Find widget coordinates inside a layout
Forum Updated to NodeBB v4.3 + New Features

Find widget coordinates inside a layout

Scheduled Pinned Locked Moved Language Bindings
2 Posts 2 Posters 11.8k Views 1 Watching
  • 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
    Mviljamaa
    wrote on last edited by
    #1

    !http://s23.postimg.org/ev7ahy0rf/pic.png(pic)!

    I'm looking for a way to determine coordinates of a widget (QLabel) when it's placed inside a layout. In the above picture the QLabel is the only widget inside the layout (thus it fills the whole layout), but it's contents (specifically an image) have been centered in the layout. Now what I'm looking for is, what should I do in order to be able to know where the QLabel's content has been placed and get the content area's coordinates? Currently I get the coordinates of the layout, because the QLabel is the same size as the layout, only the visible content has been centered. I'm thinking that I need to place more layouts in to get the "centered effect", while having a way to get the position of the QLabel.

    I'm using PySide.

    Thanks

    1 Reply Last reply
    0
    • jazzycamelJ Offline
      jazzycamelJ Offline
      jazzycamel
      wrote on last edited by
      #2

      From what I understand you are trying to ascertain the (x,y) coordinate of the top left corner of the QLabel relative to the QLayout? If so, then you can use geometry().topLeft() on both the layout and the widget to get there global positions and then subtract the first from the second to get the relative position. The following example should demonstrate this. It's written with PyQt4 (I don't have a PySide install), but it should be trivial to to convert it:

      @
      from PyQt4.QtCore import *
      from PyQt4.QtGui import *

      class Widget(QWidget):
      def init(self, parent=None, **kwargs):
      QWidget.init(self, parent, **kwargs)

          self.resize(640,480)
      
          l=QHBoxLayout(self)
      
          self.l1=QVBoxLayout()
          self.label1=QLabel("QLabel<br />(centered)", self, alignment=Qt.AlignCenter)
          self.label1.setStyleSheet("border: 1px solid black;")
          self.label1.setFixedSize(100,50)
          self.l1.addWidget(self.label1, alignment=Qt.AlignCenter)
          l.addLayout(self.l1)
      
          self.l2=QVBoxLayout()
          self.label2=QLabel("QLabel<br />(centered)", self, alignment=Qt.AlignCenter)
          self.label2.setStyleSheet("border: 1px solid black;")
          self.label2.setFixedSize(100,50)
          self.l2.addWidget(self.label2, alignment=Qt.AlignCenter)
          l.addLayout(self.l2)
      
          fl=QFormLayout()
      
          self.label1pa=QLabel('0px', self)
          self.l1pa=QLabel('0px', self)
          self.label1pr=QLabel('0px', self)
      
          fl.addRow("Label 1 Position (absolute):", self.label1pa)
          fl.addRow("Layout 1 Position (absolute):", self.l1pa)
          fl.addRow("Label 1 Position (relative):", self.label1pr)
      
          self.label2pa=QLabel('0px', self)
          self.l2pa=QLabel('0px', self)
          self.label2pr=QLabel('0px', self)
      
          fl.addRow("Label 2 Position (absolute):", self.label2pa)
          fl.addRow("Layout 2 Position (absolute):", self.l2pa)
          fl.addRow("Label 2 Position (relative):", self.label2pr)
          l.addLayout(fl)
      
      def resizeEvent(self, event):
          QWidget.resizeEvent(self, event)
      
          label1pa=self.label1.geometry().topLeft()
          l1pa=self.l1.geometry().topLeft()
          label1pr=label1pa-l1pa
      
          self.label1pa.setText('({0},{1})px'.format(label1pa.x(), label1pa.y()))
          self.l1pa.setText('({0},{1})px'.format(l1pa.x(), l1pa.y()))
          self.label1pr.setText('({0},{1})px'.format(label1pr.x(), label1pr.y()))
      
          label2pa=self.label2.geometry().topLeft()
          l2pa=self.l2.geometry().topLeft()
          label2pr=label2pa-l2pa
      
          self.label2pa.setText('({0},{1})px'.format(label2pa.x(), label2pa.y()))
          self.l2pa.setText('({0},{1})px'.format(l2pa.x(), l2pa.y()))
          self.label2pr.setText('({0},{1})px'.format(label2pr.x(), label2pr.y()))
      

      if name=="main":
      from sys import argv, exit

      a=QApplication(argv)
      w=Widget()
      w.show()
      w.raise_()
      exit(a.exec_())
      

      @

      Hope this helps ;o)

      For the avoidance of doubt:

      1. All my code samples (C++ or Python) are tested before posting
      2. As of 23/03/20, my Python code is formatted to PEP-8 standards using black from the PSF (https://github.com/psf/black)
      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