On Tuesday 26 October 2010 10:29:08 Abdullah Abouzekry wrote:
> Hi List,
> 
> I am running PySide 0.42 on windows with Python 2.6, i am currently
> migrating my application which is using PyQt4 to PySide and found the
> following incompatibility between PyQt4 and PySide which appears to be
> unreasonable, methods that accept class enums can't accept integers of the
> same value of enums?!, however when checking the type of these enum values
> using the Python Type checking recommended mechanism "isinstance(obj,
> type)" it yields an integer!!, while at the same time passing an integer
> to there respective methods i.e: QFrame.setFrameShape(), it complains for
> parameters being of incompatible types, kindly find below an illustration
> application of the issue.

Enums are implemented as types inherited from int type.

The code:

label.setFrameShape(1)

should not work, suppose you have the following enum and function in C++:

enum Foo {
 A = 1,
 B = 2,
 C = 3
};

void foobar(Foo f);

So we create python bindings for it and the python programmer calls:

fooBar(6);

6 is a valid integer, but not a valid enum value, so what to do?
We could do a reinterpret_cast pass it to the C++ function and watching it 
going mad or just throw a Python exception about invalid value.

Throw an exception still not being the better idea, first because we'll need 
to check if the integer is in a group of valid integers every time a function 
gets a integer and it's very time consuming.

Second because is a very good practice to avoid magic numbers in your code.

1 means nothing to me, but QtGui.QFrame.Box certainly do.

Regards;

> #label.setFrameShape(QtGui.QFrame.Box)
> #not working

 
> comment/uncomment the lines of : label.setFrameShape(QtGui.QFrame.Box) and
> label.setFrameShape(1) to see that the one of the enum only works,....
> 
> can any body help with this?
> 
> ##START OF CODE##
> import sys, types
> from PySide import QtCore, QtGui
> 
> app = QtGui.QApplication(sys.argv)
> 
> widget = QtGui.QWidget()
> widget.resize(250, 150)
> widget.setWindowTitle('testApp')
> 
> label = QtGui.QLabel(widget)
> label.setText("Test Label")
> 
> if isinstance(QtGui.QFrame.Box, QtGui.QFrame.Shape) :
>     print("Shape Detected, this is fine!")
> 
> if isinstance(QtGui.QFrame.Box, int) :
>     print("Integer Detected, this shouldnt be an integer!")
> 
> # will not work, even QtGui.QFrame.Box == 1 and
> isinstance(QtGui.QFrame.Box, int) returns True!!!!
> # so, either QtGui.QFrame.Box shouldn't be an instance of int -> wrong
> # or setFrameShape and other methods accepting enums should accept enums as
> well
> 
> #working
> #label.setFrameShape(QtGui.QFrame.Box)
> #not working
> label.setFrameShape(1)
> 
> label.show()
> widget.show()
> 
> sys.exit(app.exec_())
> 
> ##END OF CODE##
> 
> Best
> Abdullah Abouzekry

-- 
Hugo Parente Lima
INdT - Instituto Nokia de Tecnologia

Attachment: signature.asc
Description: This is a digitally signed message part.

_______________________________________________
PySide mailing list
[email protected]
http://lists.openbossa.org/listinfo/pyside

Reply via email to