Results 1 to 6 of 6

Thread: A question about position of widgets

  1. #1
    Join Date
    Jul 2010
    Location
    Tokyo
    Posts
    5
    Qt products
    Qt4
    Platforms
    MacOS X

    Default A question about position of widgets

    Hi all, I am trying to get the position information of widgets, here is my code,

    ok = QtGui.QPushButton("OK")
    cancel = QtGui.QPushButton("Cancel")

    vbox.addWidget(ok)
    vbox.addWidget(cancel)
    print ok.pos()
    print cancel.pos()

    which output (0,0) (0,0)
    How could I get the actual position of these widgets? Would anyone guide me to make it work? Thanks in advance

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: A question about position of widgets

    Have a look at geometry().

    using pos() is correct too, just don't forget that its relative to the parent.
    If you want screen position, then have a look at mapToGlobal().
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Apr 2010
    Location
    Bangalore
    Posts
    24
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: A question about position of widgets

    Hi,

    You can use :

    qDebug()<< okbutton->x() << okbutton->y();
    qDebug()<< cancelButton->x() << cancelButton->y();

    Again as said x() holds the x coordinate of the widget relative to its parent including any window frame.By default, this property has a value of 0.

  4. #4
    Join Date
    Jul 2010
    Location
    Tokyo
    Posts
    5
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: A question about position of widgets

    I still can't get this work. here is my code, I don't know what's wrong here.

    import sys
    from PyQt4 import QtGui


    class BoxLayout(QtGui.QWidget):
    def __init__(self, parent=None):
    QtGui.QWidget.__init__(self, parent)

    self.setWindowTitle('box layout')

    self.ok = QtGui.QPushButton("OK", self)
    self.cancel = QtGui.QPushButton("Cancel", self)

    vbox = QtGui.QVBoxLayout()
    vbox.addStretch(1)
    vbox.addWidget(self.ok)
    vbox.addWidget(self.cancel)

    print self.ok.geometry()
    print self.ok.parent()
    print self.cancel.geometry()
    print self.cancel.parent()
    print self.ok.mapToParent(self.ok.pos())
    print self.mapToParent(self.ok.pos())
    print self.mapToGlobal(self.ok.pos())


    self.setLayout(vbox)

    self.resize(300, 150)

    app = QtGui.QApplication(sys.argv)
    qb = BoxLayout()
    qb.show()
    sys.exit(app.exec_())

  5. #5
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: A question about position of widgets

    The positions are not properly calculated unless the widget is shown. So I doubt you getting proper values

  6. #6
    Join Date
    Jul 2010
    Location
    Tokyo
    Posts
    5
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: A question about position of widgets

    oh yes, I got it. The 'print' should be put after 'show', thank you guys a lot.

Similar Threads

  1. Replies: 5
    Last Post: 18th April 2010, 23:31
  2. Question: Moving Widgets (wiki Tutorial)
    By ericV in forum Newbie
    Replies: 3
    Last Post: 11th September 2009, 14:07
  3. Replies: 0
    Last Post: 15th May 2009, 15:38
  4. hello! i have a simple child widgets question
    By ht1 in forum Qt Programming
    Replies: 3
    Last Post: 17th November 2007, 23:49
  5. Designer Question About Sizing Widgets
    By pmabie in forum Qt Tools
    Replies: 1
    Last Post: 23rd September 2007, 11:05

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.