Results 1 to 4 of 4

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

  1. #1
    Join Date
    Aug 2018
    Posts
    9
    Qt products
    Qt5
    Platforms
    Unix/X11

    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. b.style().subElementRect(QtWidgets.QStyle.SE_PushButtonFocusRect, QtWidgets.QStyleOptionButton())
    2. b.style().subElementRect(QtWidgets.QStyle.SE_PushButtonFocusRect, 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.

    Thanks,
    –DDR

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    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.


  3. #3
    Join Date
    Aug 2018
    Posts
    9
    Qt products
    Qt5
    Platforms
    Unix/X11

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

    Thank you. I was hoping to have the button clickable outside its visual boundaries, as the interface is accessed by a touchscreen. The only real way I know how to do this is by changing the margin and width, so the button appears to slide in but is actually always fully clickable - it makes it so you don't have to wait for the little animation if you know what you're doing by muscle memory. How would you suggest animating the button in, so that the target area is always clickable?

    (Sorry for the delayed reply. I thought subscriptions meant "via email", but they were just winding up as a notification in my forum inbox… which I wasn't checking until I got an email.)

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

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

    You can use QApplication::setGlobalStrut() to define the minimum size of all interactive parts of a widget in your application. Maybe it suits your needs better than the stylesheet.
    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, 10:47
  2. Replies: 0
    Last Post: 17th November 2015, 19:11
  3. PyQt5 button click not work in imported class
    By mongolor in forum Newbie
    Replies: 3
    Last Post: 26th September 2015, 11:32
  4. Remove the Margins
    By Serenity in forum Qt Programming
    Replies: 1
    Last Post: 12th August 2010, 11:33
  5. QScrollArea margins
    By munna in forum Qt Programming
    Replies: 12
    Last Post: 18th September 2006, 19:56

Tags for this Thread

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.