Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从任意对象(Python和Qt)向文本框发送文本_Python_Qt - Fatal编程技术网

从任意对象(Python和Qt)向文本框发送文本

从任意对象(Python和Qt)向文本框发送文本,python,qt,Python,Qt,我需要能够从node.py中的类“node”发送文本: import datetime from PyQt4.QtCore import * from PyQt4.QtGui import * class node( QGraphicsItem ): def __init__( self, position, scene ): super( node, self ).__init__( None, scene ) self.setFlags( QGr

我需要能够从node.py中的类“node”发送文本:

import datetime

from PyQt4.QtCore import *
from PyQt4.QtGui import *

class node( QGraphicsItem ):
    def __init__( self, position, scene ):
        super( node, self ).__init__( None, scene )

        self.setFlags( QGraphicsItem.ItemIsSelectable | QGraphicsItem.ItemIsMovable )

        self.rect = QRectF( -30, -30, 120, 60 )
        self.setPos( position )
        scene.clearSelection()

    def sendFromNodeToBox( self, text ):
        # how do i send text from here to textBox?
        pass

    def boundingRect( self ):
        return self.rect

    def paint( self, painter, option, widget ):
        painter.setRenderHint( QPainter.Antialiasing )
        pen = QPen( Qt.SolidLine )
        pen.setColor( Qt.black )
        pen.setWidth( 3 )

        if option.state & QStyle.State_Selected:
            #####################
            self.sendFromNodeToBox( 'node selected' )
            #####################
            self.setZValue( 1 )
            pen.setWidth( 4 )
            pen.setColor( Qt.green )
        else:
            pen.setWidth( 3 )
            self.setZValue( 0 )
        painter.setPen( pen )
        painter.setBrush( QColor( 200, 0, 0 ) )
        painter.drawRoundedRect( self.rect, 10.0, 10.0 )
到由mainWindow.py加载的
mainWindow.ui
中的状态框

import os, sip, sys, subprocess, platform

from PyQt4.QtGui import *
from PyQt4.QtCore import *
from PyQt4.uic import *
from PyQt4.QtOpenGL import *

from src.node import *

app = None



class mainWindow( QMainWindow ):
    def __init__( self, parent = None ):
        super( mainWindow, self ).__init__( parent )


        self.currentPlatform = platform.system()

        if self.currentPlatform == "Windows":
            self.ui = loadUi( r'ui\mainWindow.ui', self )

        elif self.currentPlatform == "Darwin":
            self.ui = loadUi( r'ui/mainWindow.ui', self )

        else:
            print 'platform not supported'
            quit()

        # Scene view
        self.scene = SceneView()
        self.nodeDropGraphicsView.setViewport( QGLWidget( QGLFormat( QGL.SampleBuffers ) ) )
        self.nodeDropGraphicsView.setScene( self.scene )

        self.sendTextToBox( 'this text comes from mainWindow class, line 37 and 38.\n' )
        self.sendTextToBox( 'press right mouse button.\n' )


    def sendTextToBox( self, text ):
        cursorBox = self.statusBox.textCursor()
        cursorBox.movePosition(cursorBox.End)
        cursorBox.insertText( str( text ) )
        self.statusBox.ensureCursorVisible()


class SceneView( QGraphicsScene ):
    def __init__( self, parent=None ):
        super( SceneView, self ).__init__( parent )

        text = self.addText( 'title' )

    def mousePressEvent( self, event ):
        pos = event.scenePos()
        if event.button() == Qt.MidButton:
            pass

        elif event.button() == Qt.RightButton:
            newNode = node( pos, self )

        super( SceneView, self ).mousePressEvent( event )

    def mouseReleaseEvent( self, event ):
        print 'mouseReleaseEvent'

        self.line = None

        super( SceneView, self ).mouseReleaseEvent( event )

if __name__ == "__main__":

    app = QApplication( sys.argv )
    screenSize = QApplication.desktop().availableGeometry()
    window = mainWindow()
    window.resize( int( screenSize.width() ), int( screenSize.height() ) )
    window.show()
    app.exec_()
应用程序在win和osx上运行。Linux尚未测试

Python 2.7和Qt 4.8是必需的

有什么建议吗

完整资料来源如下:


非常感谢您的帮助。

一种方法是在
SceneView
类中定义自定义信号,然后图形项可以通过其场景发射文本:

class node( QGraphicsItem ):
    ...  
    def sendFromNodeToBox( self, text ):
        self.scene().textMessage.emit(text)

class SceneView( QGraphicsScene ):
    textMessage = pyqtSignal(str)

class mainWindow( QMainWindow ):
    def __init__( self, parent = None ):
        ...
        self.scene = SceneView()
        self.scene.textMessage.connect(self.sendTextToBox)

请内联源代码…你说的“发送文本”到底是什么意思?ekhumoro在这方面帮了我:)无论如何,谢谢