[PyQt] connect and "TypeError: argument 1 of QObject.connect() has an invalid type"

Thierry Leurent thierry.leurent at asgardian.be
Mon Oct 19 22:17:47 BST 2009


Hello,

I'm working on small application that will display the map of a network.

The main window have a QListWidget and a QGraphicScene. I create a number of 
object (hosts) in each main widget. When  I select an hosts in the 
QGraphicScene, I would like select the host in  the QListWidget too.
I try different solution to implement signal but that don't work.

I have this kind of message :
  File "./ANetworkMapper.py", line 240, in addItem
    self.connect(item, QtCore.SIGNAL("hostSelected"),self.hostSelected)
TypeError: argument 1 of QObject.connect() has an invalid type


Thanks

Thierry

the code

#!/usr/bin/env python
# -*- coding: utf-8 -*-

# testdraw.py

import sys
from PyQt4 import QtCore, QtGui


class MainWindow(QtGui.QMainWindow):
    def __init__(self, parent=None):
        QtGui.QMainWindow.__init__(self, parent)
	self.setGeometry(300, 300, 1000, 550)
        self.setWindowTitle(self.tr("Asgardian.be's Network Mapper"))
        self.setupActions()
        self.setupMenus()

	self.setupFrame()
 
        self.statusbar = self.statusBar()
	self.connect(self, QtCore.SIGNAL("messageToStatusbar(QString)"), 
self.statusbar, QtCore.SLOT("showMessage(QString)"))
	self.emit(QtCore.SIGNAL("messageToStatusbar(QString)"), "On the road again")

        self.LoadNetworkMap()

    def setupFrame(self):

	frame = QtGui.QFrame()

        self.networkmap = NetworkMapScene(self)
        self.networkmap.setSceneRect(QtCore.QRectF(0, 0, 5000, 5000))
               
	self.hostsList = NodesList()

#QtCore.QObject.connect(a, QtCore.SIGNAL("QtSig()"), pyFunction)
#QtCore.QObject.connect(a, QtCore.SIGNAL("QtSig()"), pyClass.pyMethod)
#QtCore.QObject.connect(a, QtCore.SIGNAL("QtSig()"), QtCore.SLOT("QtSlot()"))
#QtCore.QObject.connect(a, QtCore.SIGNAL("PySig()"), QtCore.SLOT("QtSlot()"))
#QtCore.QObject.connect(a, QtCore.SIGNAL("PySig"), pyFunction)
	
	self.connect(self.hostsList, 
QtCore.SIGNAL("itemClicked(QListWidgetItem*)"),self.listItemClicked)
	self.connect(self.networkmap, 
QtCore.SIGNAL("hostSelected"),self.hostSelected)
	layout = QtGui.QHBoxLayout(frame)
	
	
	layout.addWidget(self.hostsList)
        
	self.view = QtGui.QGraphicsView(self.networkmap)
        self.view.setRenderHints(QtGui.QPainter.Antialiasing)
        layout.addWidget(self.view)


	self.setCentralWidget(frame)

    def about(self):
        QtGui.QMessageBox.about(self, ("About ANetworkMapper"),
                        ("The <b>Asgerdian.be's Network Mapper</b> is build to 
help you to make the map of your network."))

    def setupActions(self):
        aboutAction = QtGui.QAction("A&bout", self)
        aboutAction.setShortcut("Ctrl+B")
        self.connect(aboutAction, QtCore.SIGNAL("triggered()"),
                self.about)
        self.aboutAction = aboutAction

    def setupMenus(self):
	fileMenu = self.menuBar().addMenu(self.tr("&File"))
        exitAction = fileMenu.addAction(self.tr("E&xit"))
        exitAction.setShortcut(QtGui.QKeySequence(self.tr("Ctrl+Q")))
        self.connect(exitAction, QtCore.SIGNAL("triggered()"), QtGui.qApp, 
QtCore.SLOT("quit()"))
        self.aboutMenu = self.menuBar().addMenu("&Help")
        self.aboutMenu.addAction(self.aboutAction)


    def LoadNetworkMap(self):

	ServerOne=NetworkHost("ServerOne",["Dell", "PowerEdge1900", "Server", 
"Debian", "5.0"] ,200,150,None,self.networkmap)
	ServerOne.add2List(self.hostsList)
	self.networkmap.addItem(ServerOne)
	#self.networkmap(ServerOne, QtCore.SIGNAL("hostSelected"),self.hostSelected)
	#ServerOne.setPixmap(str("./images/debian.png"))

	ServerTwo=NetworkHost("ServerTwo",["Dell", "PowerEdge2600", "Server", 
"RedHat", "5.3"] ,50,100,None,self.networkmap)
	ServerTwo.add2List(self.hostsList)
	self.networkmap.addItem(ServerTwo) 	        
	
	ServerThree=NetworkHost("ServerThree",["Dell", "PowerEdge2600", "Server", 
"Debian", "Sid"] ,100,150,None,self.networkmap)
	ServerThree.add2List(self.hostsList)
	self.networkmap.addItem(ServerThree) 	        
	
	ServerFour=NetworkHost("ServerFour",["HP", "Proliant", "Server", "Windows", 
"2003"] ,50,200,None,self.networkmap)
	ServerFour.add2List(self.hostsList)
	self.networkmap.addItem(ServerFour) 	        

	ServerFive=NetworkHost("ServerFive",["HP", "Proliant", "Server", "Windows", 
"2003"] ,1500,2000,None,self.networkmap)
	ServerFive.add2List(self.hostsList)
	self.networkmap.addItem(ServerFive) 	        
	

	self.view.show()

	
	for networkthostitem in self.networkmap.items():
	  X=networkthostitem.x()
	  Y=networkthostitem.y()
	  print X, " ", Y, " ", networkthostitem.toolTip()
	  print "\n"

    def listItemClicked(self,listItem):
	viewHorizontalScrollBar=self.view.horizontalScrollBar()
	visibleLeft=viewHorizontalScrollBar.value()
	viewVerticalScrollBar=self.view.verticalScrollBar()
	visibleTop=viewVerticalScrollBar.value()
	viewViewport=self.view.viewport()
	viewViewportRect=viewViewport.rect()
	viewViewportRectBottomRight=viewViewportRect.bottomRight()	
	visibleLength=viewViewportRectBottomRight.x()
	visibleHeigth=viewViewportRectBottomRight.y()

	for networkthostitem in self.networkmap.items():
	  if (listItem.text() == networkthostitem.getName()):
	     print "   ", networkthostitem.getName(), "\n"
	     print "   ", networkthostitem.isVisible(), "\n"
	     networkthostitem.setFocus()
	     networkthostitem.setSelected(True)
	     if (networkthostitem.x() <= visibleLeft) or (networkthostitem.x() >= 
(visibleLeft + visibleLength)) or (networkthostitem.y() <= visibleTop) or 
(networkthostitem.y() >= (visibleTop + visibleHeigth)):
	      networkthostitem.ensureVisible(0,0,200,200)
	     
	  else:
	     networkthostitem.setSelected(False	)

    def hostSelected(self):
	print "hostslected\n"
class NodesList(QtGui.QListWidget):	
    def __init__(self, parent=None):
	QtGui.QListWidget.__init__(self, parent)
        self.setDragEnabled(True)
        self.setViewMode(QtGui.QListView.ListMode)
        self.setMovement(QtGui.QListView.Snap)
        self.setAcceptDrops(True)
        self.setDropIndicatorShown(True)

    def setCurrentItem(self,host):
	QtGui.QListWidget.setCurrentItem(self,host)
	
class NetworkHost(QtGui.QGraphicsPixmapItem):
    HostName=""
    HostProducer=""
    HostModel=""
    HostType=""
    HostOS=""
    HostOSVersion=""
    X=0
    Y=0

    PixmapItem=None
    ListWidgetItem=None
    HostsList=None

    def __init__(self, name, hostinformations,x, y, parent=None, scene=None):
	QtGui.QGraphicsPixmapItem.__init__(self, parent, scene)
	self.setFlag(QtGui.QGraphicsItem.ItemIsMovable, True)
	self.setFlag(QtGui.QGraphicsItem.ItemIsSelectable, True)
	self.setFlag(QtGui.QGraphicsItem.ItemIsFocusable, True)
	self.HostName=name
	self.HostProducer=hostinformations[0]
	self.HostModel=hostinformations[1]
	self.HostType=hostinformations[2]
	self.HostOS=hostinformations[3]
	self.HostOSVersion=hostinformations[4]

	self.setToolTip(self.HostName)
        self.X=x
	self.Y=y
	self.setPos(x,y)

	self.PixmapItem=QtGui.QPixmap()
	self.PixmapItem.load(self.choosePixmap(self.HostProducer, self.HostModel, 
self.HostType, self.HostOS, self.HostOSVersion))
        QtGui.QGraphicsPixmapItem.setPixmap(self,self.PixmapItem)
  	 
    def x(self):
	return self.X

    def y(self):
	return self.Y

    def setPixmap (self,PixmapFile):
	self.PixmapItem.load(str(PixmapFile))
	QtGui.QGraphicsPixmapItem.setPixmap(self,self.PixmapItem)	

    def choosePixmap(self, HostProducer, HostModel, HostType, HostOS, 
HostOSVersion):
	if HostOS=="Debian":
	   return(str("./images/debian.png"))
	elif HostOS=="RedHat":
	   return(str("./images/redhat.png"))
	elif HostOS=="Windows":
	   return(str("./images/windows.png"))
	else:
	   return(str("./images/qt-logo.png"))

    def add2List(self, hostslist):
	self.ListWidgetItem=QtGui.QListWidgetItem(self.HostName, hostslist)
    
    def setFocusInList(self, hostsList):
	QtGui.QListWidget.setCurrentItem(hostsList,self.ListWidgetItem)

    def mousePressEvent(self, mouseEvent):
	print "NetworkHost :mousePressEvent\n"
	print "--- ", self.getName(),"\n"
	self.scene.setListSelectedItem(self.getName())
	self.emit(QtCore.SIGNAL("hostSelected"))
	#self.emit(SIGNAL("hostSelected"))
	#self.emit(QtCore.SIGNAL("hostSelected(QGraphicsPixmapItem*)"), self)
        #if (mouseEvent.button() != QtCore.Qt.LeftButton):
        #    return
        QtGui.QGraphicsPixmapItem.mousePressEvent(self, mouseEvent)

    def getName(self):
	return self.HostName

class NetworkMapScene(QtGui.QGraphicsScene):
    InsertItem, InsertLine, InsertText, MoveItem  = range(4)

    def __init__(self, itemMenu, parent=None):
        QtGui.QGraphicsScene.__init__(self, parent)

        self.myItemMenu = itemMenu
        self.myMode = self.MoveItem

        self.line = None
        self.textItem = None
        self.myItemColor = QtCore.Qt.white
        self.myTextColor = QtCore.Qt.black
        self.myLineColor = QtCore.Qt.black
        self.myFont = QtGui.QFont()
    
    def addItem(self, item):
      print "---- ", item, "\n"
      print "---- ", item.getName(),"\n"
      self.connect(item, QtCore.SIGNAL("hostSelected"),self.hostSelected)
      QtGui.QGraphicsScene.addItem (self, item)
    def setListSelectedItem(self, itemName):
	print "setListSelectedItem ", itemName,"\n"
    def mousePressEvent(self, mouseEvent):
	print "NetworkMapScene :mousePressEvent\n"
	for networkthostitem in self.items():
	  print ">> ",networkthostitem.getName()," ",networkthostitem.isSelected()," 
",networkthostitem.hasFocus(),"\n"
	  #if (networkthostitem.isSelected() == True):
	    #print "-- ",networkthostitem.getName(),"\n"
	self.emit(QtCore.SIGNAL("hostSelected"))
        #if (mouseEvent.button() != QtCore.Qt.LeftButton):
        #    return
        QtGui.QGraphicsScene.mousePressEvent(self, mouseEvent)
    def hostSelected(self):
	print "NetworkMapScene :hostSelected\n"
if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())



More information about the PyQt mailing list