<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[How to animate margins of QWidget]]></title><description><![CDATA[<p dir="auto">Is it possible to animate margins of QWidget ? I can't animate the geometry as it is set dynamically. It is not possible to directly animate the margins with QPropertyAnimation but I was thinking if there is an indirect way to animate the margins.</p>
]]></description><link>https://forum.qt.io/topic/116669/how-to-animate-margins-of-qwidget</link><generator>RSS for Node</generator><lastBuildDate>Thu, 06 Aug 2026 21:43:09 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/116669.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 05 Jul 2020 14:26:38 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to How to animate margins of QWidget on Mon, 06 Jul 2020 11:48:08 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/mrjj">@<bdi>mrjj</bdi></a> Thanks, it works great. The link helped a lot.</p>
]]></description><link>https://forum.qt.io/post/605049</link><guid isPermaLink="true">https://forum.qt.io/post/605049</guid><dc:creator><![CDATA[tIk90wT]]></dc:creator><pubDate>Mon, 06 Jul 2020 11:48:08 GMT</pubDate></item><item><title><![CDATA[Reply to How to animate margins of QWidget on Sun, 05 Jul 2020 20:15:23 GMT]]></title><description><![CDATA[<p dir="auto">Hi<br />
You can see the syntax here<br />
<a href="https://wiki.qt.io/Qt_for_Python_UsingQtProperties" target="_blank" rel="noopener noreferrer nofollow ugc">https://wiki.qt.io/Qt_for_Python_UsingQtProperties</a></p>
<p dir="auto">Im not sure QPropertyAnimation understands the contentsMargins directly as it 4 ints and not a QRect like<br />
Geometry</p>
<p dir="auto">I was thinking of</p>
<pre><code>
  def readPP(self):                                                                                   
        return self.ppval             

    def setPP(self,val):                                                                                
         self.ppval = val                                                                                
         self.wid2.setContentsMargins(val,val,val,val)
                                                                                                    
    pp = Property(int, readPP, setPP)     
</code></pre>
<p dir="auto">and then we cheat and set setContentsMargins for the int property</p>
]]></description><link>https://forum.qt.io/post/604906</link><guid isPermaLink="true">https://forum.qt.io/post/604906</guid><dc:creator><![CDATA[mrjj]]></dc:creator><pubDate>Sun, 05 Jul 2020 20:15:23 GMT</pubDate></item><item><title><![CDATA[Reply to How to animate margins of QWidget on Sun, 05 Jul 2020 19:57:35 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/mrjj">@<bdi>mrjj</bdi></a> By margin I mean, the margins that you set to a widget with setContentsMargins. I wanted to animate and decrease it on enterEvent. I couldn't figure out how to add the property and animate it in pyside2. Here is a little something I tried which doesn't work:</p>
<pre><code>import sys
from PySide2.QtWidgets import QMainWindow, QWidget, QHBoxLayout, QLabel, QDockWidget, QTabWidget, QApplication
from PySide2.QtCore import QMargins, QPropertyAnimation


class MainWindow(QWidget):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.resize(400, 300)

        self.lab1 = QLabel("text for first widget")
        self.lab2 = QLabel("text for second widget")

        self.wid1 = QWidget()
        self.wid1.setMaximumSize(150, 150)
        self.wid1.setStyleSheet("background: orange;")
        self.wid1Layout = QHBoxLayout()
        self.wid1Layout.addWidget(self.lab1)
        self.wid1.setLayout(self.wid1Layout)

        self.wid2 = QWidget()
        self.wid2.setMaximumSize(150, 150)
        self.wid2.setStyleSheet("background: teal;")
        self.wid2Layout = QHBoxLayout()
        self.wid2Layout.addWidget(self.lab2)
        self.wid2.setLayout(self.wid2Layout)

        self.layout = QHBoxLayout()
        self.layout.addWidget(self.wid1)
        self.layout.addWidget(self.wid2)
        self.setLayout(self.layout)

        self.wid1.setContentsMargins(QMargins(8, 8, 8, 8))
        self.wid2.setContentsMargins(QMargins(8, 8, 8, 8))

        self.wid1.setProperty("contentsMargins", self.wid1.contentsMargins())

        self.decMarginAnim = QPropertyAnimation(self.wid1, b"contentsMargins")

    def decMargin(self):
        self.decMarginAnim.setDuration(200)
        self.decMarginAnim.setEndValue(QMargins(0, 0, 0, 0))
        self.decMarginAnim.start()

    def enterEvent(self, event):
        self.decMargin()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    mw = MainWindow()
    mw.show()
    sys.exit(app.exec_())

</code></pre>
<p dir="auto">Would you mind helping me out here with adding the property ?</p>
]]></description><link>https://forum.qt.io/post/604903</link><guid isPermaLink="true">https://forum.qt.io/post/604903</guid><dc:creator><![CDATA[tIk90wT]]></dc:creator><pubDate>Sun, 05 Jul 2020 19:57:35 GMT</pubDate></item><item><title><![CDATA[Reply to How to animate margins of QWidget on Sun, 05 Jul 2020 14:34:30 GMT]]></title><description><![CDATA[<p dir="auto">Hi<br />
Im not sure what margins you mean but you can always add some properties<br />
<a href="https://doc.qt.io/qt-5/properties.html" target="_blank" rel="noopener noreferrer nofollow ugc">https://doc.qt.io/qt-5/properties.html</a><br />
and be able to animate almost any value you like by exposing them as property.</p>
]]></description><link>https://forum.qt.io/post/604820</link><guid isPermaLink="true">https://forum.qt.io/post/604820</guid><dc:creator><![CDATA[mrjj]]></dc:creator><pubDate>Sun, 05 Jul 2020 14:34:30 GMT</pubDate></item></channel></rss>