Search code examples
pythonpyqtpyside

What type conversions exist in PySide/PyQt?


Slot, Signal, Property, etc. take a type. For instance, the sample code shows:

class MyObject(QObject):                                                                            

    def __init__(self,startval=42):                                                                 
        QObject.__init__(self)                                                                      
        self.ppval = startval                                                                       
                                                                                                   
    def readPP(self):                                                                                   
        return self.ppval                                                                               
                                                                                                    
    def setPP(self,val):                                                                                
        self.ppval = val                                                                                
                                                           
    pp = Property(int, readPP, setPP)  # <--- Look at the first argument

The Property expects an 'int'.

What types are possible? I can find no documentation.

In my time working with Qt, I have gleaned the following through osmosis:

  • int
  • float
  • bool
  • str
  • list
  • 'QVariant'
  • 'QJsonObject`

Are these the only types available? Are they actually documented somewhere?


Solution

  • In general it supports any data type.

    The problem is that if QML supports it, and in that case there are only some basic types that PySide/PyQt can interpret and convert them to QML objects. Unfortunately there is no documentation about it (maybe reporting it as a bug would be good for them to implement it).

    Besides the ones you indicate you can also use:

    • "QVariantList"

    • QObject: this type must also be exported when you expose models or classes derived from QObject.

    • QColor

    • QUrl

    • QDateTime

    • QFont

    • QPoint, QPointF

    • QRect, QRectF

    • QSize, QSizeF

    In theory PySide/PyQt can support at most the types that Qt/C++ supports and in that case if there is documentation about it: Basic Qt Data Types.

    Any other type of python data that is not in that list will be encapsulated in a QVariant since it does support PyObject but it will be unusable since it cannot be manipulated in QML.