Results 1 to 2 of 2

Thread: How to retrieve margins of a button? [PyQt5]

  1. #1
    Join Date
    Aug 2018
    Qt products

    Question How to retrieve margins of a button? [PyQt5]

    Hello everyone,

    I've assigned a stylesheet to a button. How would I go about animating the margins? I figure the first step is to retrieve the current margin, since that's what I want to animate to. I can't figure it out though...

    Qt Code:
    1. from PyQt5 import QtWidgets
    2. QtWidgets.QApplication([])
    3. b = QtWidgets.QPushButton()
    4. b.setStyleSheet("margin: 15px")
    5. b.contentsMargins().top() #is 0, should be 15
    To copy to clipboard, switch view to plain text mode 
    The final line produces 0 instead of the expected value of 15.

    Qt Code:
    1., QtWidgets.QStyleOptionButton())
    2., QtWidgets.QStyleOptionButton(), b)
    To copy to clipboard, switch view to plain text mode 
    Carrying on, both these lines then print "Segmentation fault" and exit Python. They didn't return anything particularly sensible in my actual app anyway.

    Does anyone have any suggestions? I'm trying to make a little 'slide-in' effect for my menu, but I want the buttons to always be clickable even when they haven't slid in yet. I was planning to do it by adjusting the margin-left from my menu width to the original value of margin-left.


  2. #2
    Join Date
    Jan 2006
    Warsaw, Poland
    Thanked 5,006 Times in 4,785 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Unix/X11 Windows Android Maemo/MeeGo
    Blog Entries
    Wiki edits

    Default Re: How to retrieve margins of a button? [PyQt5]

    Stylesheets are not good for animations and you should avoid using them. If you want a slide animation, use QPropertyAnimation to animation geometry (or x, y) of your buttons.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.

Similar Threads

  1. Replies: 5
    Last Post: 25th January 2017, 09:47
  2. Replies: 0
    Last Post: 17th November 2015, 18:11
  3. PyQt5 button click not work in imported class
    By mongolor in forum Newbie
    Replies: 3
    Last Post: 26th September 2015, 10:32
  4. Remove the Margins
    By Serenity in forum Qt Programming
    Replies: 1
    Last Post: 12th August 2010, 10:33
  5. QScrollArea margins
    By munna in forum Qt Programming
    Replies: 12
    Last Post: 18th September 2006, 18:56

Tags for this Thread


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.