I'm having some issues when trying to execute a string/file from within a QPlainTextEdit, it appears to be some sort of scoping issues. What happens is that when the code EXECUTABLE_STRING is run from the global scope, it works fine. However, when it is run from a local scope, such as through the AbstractPythonCodeWidget, it either can't find the object to do inheritance TypeError: super(type, obj): obj must be an instance or subtype of type or runs into a name error NameError: name 'Test' is not defined. Which oddly changes based on whether or not the exec(EXECUTABLE_STRING) line is commented/uncommented when run. Any help would be greatly appreciated.
import sys
from PyQt5.QtWidgets import QApplication, QPlainTextEdit
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QCursor
app = QApplication(sys.argv)
EXECUTABLE_STRING = """
from PyQt5.QtWidgets import QLabel, QApplication
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QCursor
class Test(QLabel):
def __init__(self, parent=None):
super(Test, self).__init__(parent)
self.setText("Test")
a = Test()
a.show()
a.move(QCursor.pos())
"""
class AbstractPythonCodeWidget(QPlainTextEdit):
def __init__(self, parent=None):
super(AbstractPythonCodeWidget, self).__init__(parent)
self.setPlainText(EXECUTABLE_STRING)
def keyPressEvent(self, event):
if event.modifiers() == Qt.ControlModifier:
if event.key() == Qt.Key_Return:
# this does not work
#exec(compile(self.toPlainText(), "script", "exec"), globals(), locals())
exec(self.toPlainText())
return QPlainTextEdit.keyPressEvent(self, event)
w = AbstractPythonCodeWidget()
w.show()
w.move(QCursor.pos())
w.resize(512, 512)
# this works when run here, but not when run on the keypress event
# exec(EXECUTABLE_STRING)
sys.exit(app.exec_())
First of all, running exec based on user input can be a security issue, but most importantly usually leads to fatal crash unless lots of precautions are taken, since you're using the same interpreter for both your program and the user code: basically, if the code of the user fails, your program fails, but that's not the only problem.
The reason for which your code doesn't run properly is actually a bit complex, and it's related to the scope of the class name, which becomes a bit complex when running exec along with super().[1]
An interesting aspect is that if you remove the arguments of super (and you should, since Python 3), the program won't raise any error.
But that won't be enough: a is a local variable, and it will be garbage collected as soon as exec is finished, and since the label is assigned to that variable, it will be destroyed along with it.
A possible solution would be to make the reference persistent, for example by assigning it to self (since self exists in the scope of the executed script). This is a working example of the EXECUTABLE_STRING:
from PyQt5.QtWidgets import QLabel
from PyQt5.QtGui import QCursor
class Test(QLabel):
def __init__(self, parent=None):
super().__init__(parent)
self.setText("Test")
self.a = Test()
self.a.show()
self.a.move(QCursor.pos())
As you can see, we don't need to import everything again anymore, we only import QLabel, since it wasn't imported in the main script, but everything else already exists in the scope of the script, including the current QApplication instance.
That said, all the above is only for knowledge purposes, as you should NOT use exec to run user code.
For instance, try to paste the following in the text edit, and run it:
self.document().setHtml('This should <b>NOT</b> happen!!!<br/><br/>Bye!')
self.setReadOnly(True)
from PyQt5.QtCore import QTimer
from PyQt5.QtWidgets import QApplication
QTimer.singleShot(2000, QApplication.quit)
As you can see, not only the above code is able to change the input, but also take complete control of the whole application.
You could prevent that by calling a function that would basically limit the scope of the execution:
def runCode(code):
try:
exec(code)
except Exception as e:
return e
class AbstractPythonCodeWidget(QPlainTextEdit):
# ...
def keyPressEvent(self, event):
if event.modifiers() == Qt.ControlModifier:
if event.key() == Qt.Key_Return:
error = runCode(self.toPlainText())
if error:
QMessageBox.critical(self, 'Script crash!', str(error))
return QPlainTextEdit.keyPressEvent(self, event)
But that's just because no self is involved: you could still use w.a = Test() with the example above.
So, if you want to run user made scripts in your program, exec is probably not an acceptable solution, unless you take enough precautions.
If you don't need direct interaction between your program and the user script, a possibility could be to use the subprocess module, and run the script with another python interpreter instance.
[1] If anybody has a valid resource/answer that might shed some light on the topic, please comment.
Found a similar issue that goes into more depth about how the scope works with Globals/Locals in exec here:
globals and locals in python exec()
Don't want to copy/paste the entire thread, but the answer that worked for me in this post was:
d = dict(locals(), **globals())
exec (code, d, d)
Related
I'm working on terminal based PyQt application using Windows OCX api.
And I'm having trouble over implement(calling exec) QEventLoop in QAxWidget instance.
If I call exec() in main QApplication main loop, everthing is fine.
But, calling exec() in instance of QAxWidget which is instantiated in main loop, it does not work as expected.
When exec() called(_on_event_connect in code), error message showed up like
"QEventLoop:exec: instance 0x18479d8 has already called exec()"
And on the moment of calling exit(), QEventLoop seems not running.
And the application is hang after this.
The below is simplified example of my code, It tries call QAxWidget method "dynamicCall" as soon as log-in process finished.
It consists of pyQt signal/slot mechanism, get event from API server.
Please understand that you can not execute this code because of it needs specific APIs installation and registered user-id.
But I hope you can see the points of problem.
main.py
import sys
from PyQt5.QtWidgets import QApplication
from api import OpenApi
class Main():
def __init__(self):
self.api = OpenApi()
self.api.comm_connect()
# self.api.req_basic_stock_info("035420") -> if I call here, works well
if __name__ == "__main__":
app = QApplication(sys.argv)
main = Main()
sys.exit(app.exec_())
api.py
import sys
from PyQt5.QtWidgets import *
from PyQt5.QAxContainer import *
from PyQt5.QtCore import *
class OpenApi(QAxWidget):
def __init__(self):
super().__init__()
self.setControl("KHOPENAPI.KHOpenAPICtrl.1")
self.OnEventConnect.connect(self._on_event_connect)
self.OnReceiveTrData.connect(self._on_receive_tr_data)
self._qloop = QEventLoop()
def loop(self):
if not self._qloop.isRunning():
logger.info(f"-->exec")
self._qloop.exec()
else:
logger.info("exec skipped")
def unloop(self):
if self._qloop.isRunning():
logger.info("-->exit")
self._qloop.exit()
else:
logger.info(f"exit skipped")
def comm_connect(self):
self.dynamicCall("CommConnect()")
self.loop()
def comm_rq_data(self, rqname, trcode, next, screen_no):
self.dynamicCall("CommRqData(QString, QString, int, QString)", rqname, trcode, next, screen_no)
self.loop()
def _on_event_connect(self, err_code):
if err_code == 0:
logger.info("logged in")
else:
logger.info("log-in failed")
self.unloop()
# If I call this function here, QEventLoop fails
self.req_basic_stock_info("000660")
def _on_receive_tr_data(self, screen_no, rqname, trcode, record_name, next,
unused1, unused2, unused3, unused4):
logger.info(f"OnReceivTrData: {rqname}")
self.unloop()
def req_basic_stock_info(self, code):
# I want to call this function inside instance on certain condition.
self.set_input_value("item", code)
scrno = "2000"
self.comm_rq_data("ITEM_INFO_REQ", "opt10080", "0", scrno)
And output is like below.
12:42:53,54 test INFO -->exec
12:42:58,961 test INFO logged in
12:42:58,962 test INFO -->exit
12:42:58,977 test INFO -->exec
QEventLoop::exec: instance 0x35c34a0 has already called exec()
12:42:59,33 test INFO OnReceivTrData:ITEM_INFO_REQ
12:42:59,35 test INFO exit skipped
As eyllanesc told me, I changed functions related assign/deassign QEventLoop like below. And it works.
def loop(self):
self._qloop = QEventLoop()
self._qloop.exec()
def unloop(self):
try:
self._qloop.exit()
except AttributeError as e:
logger.error(e)
update
I'm afraid this issue has not been solved yet. It was successful when req_basic_stock_info function is called on _on_event_connect event, But I call it at another event function, it hang again without any error message. There is no difference in calling mechanism.
Calling the exit() function does not imply that the QEventLoop can be reused, therefore the error message is signaled. In this case it is better to use a new QEventLoop:
def unloop(self):
if self._qloop.isRunning():
logger.info("-->exit")
self._qloop.exit()
self._qloop = QEventLoop()
else:
logger.info(f"exit skipped")
This is a follow-up question to a previous one (again posted by me): PyQt4 QProcess state always 0, various slots not working too
The code (modified):
Main application: qprocess_test.py
#!/usr/bin/python
import sys
from PyQt4 import QtGui, QtCore
from PyQt4.QtCore import QProcess
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.command = "./testCommand.py"
self.args = [""]
self.initUI()
def initUI(self):
hbox = QtGui.QHBoxLayout()
hbox.addStretch(1)
qbtn = QtGui.QPushButton('Start', self)
qbtn.clicked.connect(self.toggleProcess)
qbtn.resize(qbtn.sizeHint())
hbox.addWidget(qbtn)
# This button is for testing the responsiveness of the GUI after the QProcess has been started
qbtn2 = QtGui.QPushButton('Click me', self)
qbtn2.setCheckable(True)
qbtn2.toggled.connect(self.toggleButton)
qbtn2.resize(qbtn2.sizeHint())
hbox.addWidget(qbtn2)
self.setLayout(hbox)
self.setGeometry(300, 300, 250, 150)
self.setWindowTitle('QProcess controlled by a button')
self.show()
def toggleProcess(self):
res, pid = QProcess.startDetached(self.command, self.args)
print "Starting process\n", str(res), " | pid = ", str(pid)
def toggleButton(self, value):
if value == True:
print "Lalalala!"
else:
print "Didadida!"
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
For now all the Start button does is start the detached process. The idea is for it to be toggable and stop/start the detached process whenever the users interacts with it based on the PID.
The application used for the created process: testCommand.py
#!/usr/bin/env python
while True:
print "a"
After doing some reading and googling I found out that the startDetached() function returns 2 values in Python (in the C++ it's a boolean function, but it also sets the value of one of its arguments - the pointer pid):
bool: tells us if the starting of the detached process was successful or not
int: gives the PID of the started process if the operation was successful
Here are the sources I have used for this information:
http://doc.qt.io/qt-4.8/qprocess.html#startDetached
http://pyqt.sourceforge.net/Docs/PyQt4/qprocess.html (due to current failure to load sourceforge properly here is the cached version: http://webcache.googleusercontent.com/search?q=cache:lQ-ixJIbVQQJ:pyqt.sourceforge.net/Docs/PyQt4/qprocess.html+&cd=1&hl=en&ct=clnk&gl=de&client=ubuntu)
For some reason this is not working at all. Here are the two situations that I have tested:
Trying to get both the bool and int values from the function:
res, pid = self.myProcess.startDetached(self.command, self.args)
I get this error:
Traceback (most recent call last):
File "./qprocess_test.py", line 48, in toggleProcess
res, pid = self.myProcess.startDetached(self.command, self.args)
TypeError: 'bool' object is not iterable
This tells me that I can't iterate over the return value of startDetached() hence only a SINGLE value is actually returned and not two as I have also seen in multiple pieces of code again part of the PyQt4 documentation...
Checking what startDetached() actually returns: it seems it only returns an int, which I presume is the PID. However if it's the PID it is not the correct value (looking at the output of htop):
val = QProcess.startDetached(self.command, self.args)
# val = 0 (always!)
I have learned that startDetached() is a static function hence any future interaction with the created process can be done via it's pid but things like calling its state (QProcess.state()) etc. are not possible since there is no object to interact with. The way this (broken) thing works right now is indeed start a detached process but since I don't have any PID that identifies it, the only way of interaction I have is finding manually the PID of the testCommand.py and then executing the appropriate signal via kill.
Does anyone know what I'm doing wrong here? I can go to the basics and start using C/C++ with the various system calls but I would really like to actually learn how to do this with PyQt4 and Python.
Thanks!
EDIT
It seems that startDetached() indeed returns True|False. The PID however is nowhere to be found...Not what one reads in the official documentation.
The function startDetached is overloaded in C++, it has several signatures. Depending on the arguments provided, it does not necesseraly return the same thing.
Even if overloading does not exist in Python, you have something similar:
import sys
from PyQt4 import QtGui, QtCore
from PyQt4.QtCore import QProcess
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
command = "./testCommand.py"
args = [""]
#"regular" startDetached : give two arguments, get a boolean
process=QProcess()
re=process.startDetached("ls",["."])
print(re)
print(type(re))
#"overload" startDetached : give three arguments, get a tuple(boolean,PID)
process2=QProcess()
re,pid=process2.startDetached("ls",["."],".")
print(re,pid)
print(type(re),type(pid))
How can I create one single instance of QApplication?
Background:
I am testing a couple of widgets implementing QWidget in unit tests. For this, I have to create an instance of QApplication. The second call to the constructor of QApplication results in an exception.
It works with following drawbacks:
The widget and the QApplication are created in setUpClass(cls), marked as #classmethod. For creation and maintenance of the tests, this is a pain cause every test has to deal with the same instance of the widget.
As soon as I have to execute multiple test cases, multiple QApplication instances are created and I'm facing a RuntimeError again...
My first working idea was to surround every call to QApplication() by a try except. But I am not happy with that...
I tried calling app.quit(), setting self.app = None and gc.collect(). None of them worked.
Technology facts:
Python 3.4
PySide
module unittest
Execution in PyCharm and console / script
Use the same QApplication instance for all unit tests in your test script.
To use the same QApplication instance, instantiate QApplication in the global scope of the unit test script.
Use a unique QWidget for each unit test.
To use a unique QWidget instance, instantiate QWidget in unittest.TestCase.setUp()
Here is a complete test script example to run from a console.
My environment is similar to yours except I am using:
PyQt5 instead of PySide
Jupyter QtConsole instead of PyCharm
#! /usr/bin/env python
import sys
import unittest
from PyQt5.QtWidgets import QApplication
"""All tests use the same single global instance of QApplication."""
from PyQt5.QtWidgets import QWidget
"""The tests individually instantiate the top-level window as a QWidget."""
# Create an application global accessible from all tests.
app= QApplication( sys.argv )
# Define setUp() code used in all tests.
class PyQt_TestFixture( unittest.TestCase ):
def create_application_window( self ):
w= QWidget()
return w
def setUp( self ):
self.window= self.create_application_window()
class TestPyQt( PyQt_TestFixture ):
"""Suite of tests. See test fixture for setUp()."""
def test_QWidget_setWindowTitle( self ):
"""Test that PyQt setWindowTitle() sets the title of the window."""
# Operate.
self.window.setWindowTitle( 'Test setWindowTitle' )
# Check.
self.assertEqual( self.window.windowTitle(), 'Test setWindowTitle' )
def test_eachTestUsesUniqueQWidgets( self ):
"""Test that the other test of PyQt setWindowTitle() is not affecting this test."""
# Check.
self.assertNotEqual( self.window.windowTitle(), 'Test setWindowTitle' )
"""The windowTitle() is an empty string '' if setWindowTitle() is not called."""
def test_QWidget_resize( self ):
"""Another example: test that PyQt resize() resizes the window."""
# Operate.
self.window.resize( 123, 456 )
# Check.
from PyQt5.QtCore import QSize
size= QSize( 123, 456 )
self.assertEqual( self.window.size(), size )
if __name__ == '__main__':
unittest.main()
I'm having troubles using PyQt4 slots/signals.
I'm using PyLIRC and I'm listening for button presses on a remote. This part I have gotten to work outside of Qt. My problem comes when emitting the signal from the button listening thread and attempting to call a slot in the main thread.
My button listener is a QObject initialized like so:
buttonPressed = pyqtSignal(int)
def __init__(self):
super(ButtonEvent, self).__init__()
self.buttonPressed.connect(self.onButtonPressed)
def run(self):
print 'running'
while(self._isListening):
s = pylirc.nextcode()
if (s):
print 'emitting'
self.buttonPressed.emit(int(s[0]))
The onButtonPressed slot is internal to the button listener for testing purposes.
To move the button listener to another thread to do the work, I use the following:
event = ButtonEvent()
eventThread = QThread()
event.moveToThread(eventThread)
eventThread.started.connect(event.run)
Then in the main thread, I have my VideoTableController class that contains the slot in the main thread that doesn't get called. Inside of __init__ I have this:
class VideoTableController(QObject):
def __init__(self, buttonEvent):
buttonEvent.buttonPressed.connect(self.onButtonPressed)
Where onButtonPressed in this case is:
#pyqtSlot(int)
def onButtonPressed(self, bid):
print 'handling button press'
if bid not in listenButtons: return
{ ButtonEnum.KEY_LEFT : self.handleBack,
#...
So when I start the event thread, it starts listening properly. When I press a button on the remote, the onButtonPressed slot internal to the ButtonEvent class is properly called, but the slot within VideoTableController, which resides in the main thread, is not called. I started my listening thread after connecting the slot to the signal, and I tested doing it the other way around, but to no avail.
I have looked around, but I haven't been able to find anything. I changed over to using QObject after reading You're doing it wrong. Any help with this is greatly appreciated. Let me know if you need anything else.
EDIT: Thanks for the responses! Here is a big chunk of code for you guys:
ButtonEvent (This class uses singleton pattern, excuse the poor coding because I'm somewhat new to this territory of Python also):
import pylirc
from PyQt4.QtCore import QObject, pyqtSignal, QThread, pyqtSlot
from PyQt4 import QtCore
class ButtonEvent(QObject):
"""
A class used for firing button events
"""
_instance = None
_blocking = 0
_isListening = False
buttonPressed = pyqtSignal(int)
def __new__(cls, configFileName="~/.lircrc", blocking=0, *args, **kwargs):
if not cls._instance:
cls._instance = super(ButtonEvent, cls).__new__(cls, args, kwargs)
cls._blocking = blocking
if not pylirc.init("irexec", configFileName, blocking):
raise RuntimeError("Problem initilizing PyLIRC")
cls._isListening = True
return cls._instance
def __init__(self):
"""
Creates an instance of the ButtonEvent class
"""
super(ButtonEvent, self).__init__()
self.buttonPressed.connect(self.button)
### init
def run(self):
print 'running'
while(self._isListening):
s = pylirc.nextcode()
if (s):
print 'emitting'
self.buttonPressed.emit(int(s[0]))
def stopListening(self):
print 'stopping'
self._isListening = False
#pyqtSlot(int)
def button(self, bid):
print 'Got ' + str(bid)
def setupAndConnectButtonEvent(configFileName="~/.lircrc", blocking=0):
"""
Initializes the ButtonEvent and puts it on a QThread.
Returns the QThread it is running on.
Does not start the thread
"""
event = ButtonEvent().__new__(ButtonEvent, configFileName, blocking)
eventThread = QThread()
event.moveToThread(eventThread)
eventThread.started.connect(event.run)
return eventThread
Here is the VideoTableController:
from ControllerBase import ControllerBase
from ButtonEnum import ButtonEnum
from ButtonEvent import ButtonEvent
from PyQt4.QtCore import pyqtSlot
from PyQt4 import QtCore
class VideoTableController(ControllerBase):
listenButtons = [ ButtonEnum.KEY_LEFT,
ButtonEnum.KEY_UP,
ButtonEnum.KEY_OK,
ButtonEnum.KEY_RIGHT,
ButtonEnum.KEY_DOWN,
ButtonEnum.KEY_BACK ]
def __init__(self, model, view, parent=None):
super(VideoTableController, self).__init__(model, view, parent)
self._currentRow = 0
buttonEvent = ButtonEvent()
buttonEvent.buttonPressed.connect(self.onButtonPressed)
self.selectRow(self._currentRow)
#pyqtSlot(int)
def onButtonPressed(self, bid):
print 'handling button press'
if bid not in listenButtons: return
{ ButtonEnum.KEY_LEFT : self.handleBack,
ButtonEnum.KEY_UP : self.handleUp,
ButtonEnum.KEY_OK : self.handleOk,
ButtonEnum.KEY_RIGHT : self.handleRight,
ButtonEnum.KEY_DOWN : self.handleDown,
ButtonEnum.KEY_BACK : self.handleBack,
}.get(bid, None)()
And here is my startup script:
import sys
from PyQt4 import QtCore, QtGui
from ui_main import Ui_MainWindow
from VideoTableModel import VideoTableModel
from VideoTableController import VideoTableController
from ButtonEvent import *
class Main(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.buttonEvent = ButtonEvent()
self.bEventThread = setupAndConnectButtonEvent()
model = VideoTableModel("/home/user/Videos")
self.ui.videoView.setModel(model)
controller = VideoTableController(model, self.ui.videoView)
self.bEventThread.start()
def closeEvent(self, event):
self.buttonEvent.stopListening()
self.bEventThread.quit()
event.accept()
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
buttonEvent = ButtonEvent()
myapp = Main()
myapp.show()
sys.exit(app.exec_())
It turns out I was just making a foolish Python mistake. The signal was being emitted correctly, and the event loop was running properly in all threads. My problem was that in my Main.__init__ function I made a VideoTableController object, but I did not keep a copy in Main, so my controller did not persist, meaning the slot also left. When changing it to
self.controller = VideoTableController(model, self.ui.videoView)
Everything stayed around and the slots were called properly.
Moral of the story: it's not always a misuse of the library, it may be a misuse of the language.
It seems that the quickest workaround would be change your ButtonEvent code here:
...
def run(self):
print 'running'
while(self._isListening):
s = pylirc.nextcode()
if (s):
print 'emitting'
self.buttonPressed.emit(int(s[0]))
...
to this:
#pyqtSlot()
def run(self):
print 'running'
while(self._isListening):
s = pylirc.nextcode()
if (s):
print 'emitting'
self.buttonPressed.emit(int(s[0]))
The short explanation to this issue is that PyQt uses a proxy internally, and this way you can make sure to avoid that. After all, your method is supposed to be a slot based on the connect statement.
Right... Now, I would encourage you to give some consideration for your current software design though. It seems that you are using a class in a dedicated thread for handling Qt button events. It may be good idea, I am not sure, but I have not seen this before at least.
I think you could get rid of that class altogether in the future with a better approach where you connect from the push button signals directly to your handler slot. That would not be the run "slot" in your dedicated thread, however, but the cannonical handler.
It is not a good design practice to introduce more complexity, especially in multi-threaded applications, than needed. Hope this helps.
I haven't actually tested this (because I don't have access to your compiled UI file), but I'm fairly certain I'm right.
Your run method of your ButtonEvent (which is supposed to be running in a thread) is likely running in the mainthread (you can test this by importing the python threading module and adding the line print threading.current_thread().name. To solve this, decorate your run method with #pyqtSlot()
If that doesn't solve it, add the above print statement to various places until you find something running in the main thread that shouldn't be. The lined SO answer below will likely contain the answer to fix it.
For more details, see this answer: https://stackoverflow.com/a/20818401/1994235
Using python, pyside, I get this error:
self.setCheckState(value)
TypeError: could not convert 'BooleanEditor' to 'QCheckBox'
Beside that many of Google's result only show "TypeError: can not convert" instead of "could", I still have no idea how to fix this.
Code snippet:
class Editor(QGraphicsLayoutItem):
def __init__(self, name):
QGraphicsLayoutItem.__init__(self)
:
:
def update_value(self, value):
pass
class BooleanEditor(Editor, QCheckBox):
def __init__(self, parent, name, value, min, max):
Editor.__init__(self, name)
QCheckBox.__init__(self)
self.update_value(value)
def update_value(self, value):
self.old_value = value
self.setCheckState(value) # Error occurs here.
"value" that setCheckState receives will be Qt.CheckState. Upon running, the "value" is Qt.Unchecked (== 0) as expected, according to debug printing.
Notice that BooleanEditor employs multiple inheritance. I'm porting wxWidget app (that someone else made) to Qt, and for now I don't want to change this design because this comes from original (meaning mult inheritance itself here shouldn't be the cause since the original app works fine).
Environment) pyside 1.1.0, python 2.7.3, Ubuntu 12.04
Update-1) As #Luke Woodward suggests, I tried to swap the order of super classes as BooleanEditor(QCheckBox, Editor), then get a different error at different part.
class PaneGroup(GroupView, QFrame):
def __init__(self, parent, group, config, top = None):
GroupView.__init__(self, group, top)
QFrame.__init__(self)
:
sizer = QGraphicsGridLayout()
:
for param_descr in self.params:
name = param_descr['name']
type, val, min, max, description = param_descr['type'], config[name], param_descr['min'], param_descr['max'], param_descr['description']
try:
enum = eval(param_descr['edit_method'])['enum']
editor = self.top.EnumEditor(self, name, val, enum)
except:
editor = self.top._editor_types[type](self, name, val, min, max)
self.top.editors[name] = editor
sizer.addItem(editor, row, 1) # Error occurs here
Error:
TypeError: could not convert 'BooleanEditor' to 'QGraphicsLayoutItem'
Looks like an issue about initialization in multiple inheritance to me..
My very bad, it turned out I was using PyQt4 instead of PySide. When I use PySide, error doesn't occur (as #phihag's gist shows) whereas using PyQt4 yields the same result. It's actually curious but I won't investigate further now.
Use the following code for reproduction.
#!/usr/bin/env python
import sys
from PySide.QtCore import Qt
from PySide.QtGui import QApplication, QCheckBox, QGraphicsLayoutItem
#from PyQt4.QtCore import Qt
#from PyQt4.QtGui import QApplication, QCheckBox, QGraphicsLayoutItem
class Editor(QGraphicsLayoutItem):
def __init__(self, name):
QGraphicsLayoutItem.__init__(self)
def update_value(self, value):
pass
class BooleanEditor(Editor, QCheckBox):
def __init__(self, value):
Editor.__init__(self, "foo")
QCheckBox.__init__(self)
self.update_value(value)
def update_value(self, value):
self.old_value = value
self.setCheckState(value) # Error occurs here
print("End of update_value")
if __name__ == "__main__":
qApp = QApplication(sys.argv)
BooleanEditor(Qt.Checked)
PS. Why I didn't know I was using PyQt? For which one I use, I'm depending on this framework I've been working with (called qt_gui in ROS, Robot Operating System), which doesn't explicitly tells me which one in use (it's open source prj and documentation work is ongoing)...After hacking into its codes, I figured out default is PyQt.