Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Qt Style Sheets and Custom Painting Example
Forum Updated to NodeBB v4.3 + New Features

Qt Style Sheets and Custom Painting Example

Scheduled Pinned Locked Moved Solved Qt for Python
2 Posts 1 Posters 345 Views
  • 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.
  • alomA Offline
    alomA Offline
    alom
    wrote on last edited by alom
    #1

    Hi,
    I'm trying to follow the c++ example of assigning a qproperty on a custom widget to be update via stylesheet in a paint event.

    https://wiki.qt.io/Qt_Style_Sheets_and_Custom_Painting_Example

    I dont get any errors, but I also am not getting my stylesheet color. Is QtCore.Property the same as Q_PROPERTY in the example?

    I could only find futher readings on the QtCore.Property here:
    https://wiki.qt.io/Qt_for_Python_UsingQtProperties

    The expected behavior from the code below is to have to button change to Red (defined in stylesheet) when clicked.

    import sys
    import os
    from PySide2 import QtWidgets, QtGui, QtCore
    from PySide2.QtCore import Qt
    
    
    class CustomWidget(QtWidgets.QPushButton):
    
        def __init__(self, *args, **kwargs):
            super(CustomWidget, self).__init__(*args, **kwargs)
            self.setCheckable(True)
            self._shmee = None
    
        def get_shmee(self):
            print('GET')
            return self._shmee
    
        def set_shmee(self, shmee):
            print(f'SET: {shmee}')
            self._shmee = shmee
    
        def paintEvent(self, event):
            painter = QtGui.QPainter(self)
    
            width_full = painter.device().width()
            height_full = painter.device().height()
    
            brush = QtGui.QBrush()
    
            brush.setStyle(Qt.SolidPattern)
    
            if self.isChecked():
                brush.setColor(self.get_shmee())
            else:
                brush.setColor(QtGui.QColor(10, 10, 250))
    
            rect = QtCore.QRect(0, 0, width_full, height_full)
            painter.fillRect(rect, brush)
    
            painter.end()
    
        def mousePressEvent(self, event):
            self.setChecked(not self.isChecked())
    
        shmee = QtCore.Property(QtGui.QColor, get_shmee, set_shmee)
    
    
    if __name__ == '__main__':
        app = QtWidgets.QApplication(sys.argv)
    
        custom_style = CustomWidget()
    
        script_dir = os.path.dirname(__file__)
        stylesheet_path = 'style.css'
    
        with open(os.path.join(script_dir, stylesheet_path)) as sty:
            custom_style.setStyleSheet(sty.read())
    
        custom_style.show()
    
        rec = app.exec_()
        sys.exit(rec)
    

    style.css file :

    CustomWidget {
        qproperty-shmee: rgb(250, 10, 10);
    }
    
    1 Reply Last reply
    0
    • alomA Offline
      alomA Offline
      alom
      wrote on last edited by alom
      #2

      Ha!
      ... I was missing a comma in my stylesheet :P

      qproperty-shmee: rgb(20, 100, 100);
      
      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