2014-12-25 3 views
-2

мне нужно, чтобы иметь возможность отправить текст из класса «узла» в node.py:Отправить текст текстового поля от произвольного объекта (Python и Qt)

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) 

к statusBox в mainWindow.ui, который будучи нагруженной mainWindow.py

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 требуется.

Любые предложения?

Полный источник здесь:

https://www.dropbox.com/sh/lcetrurnemr2cla/AAD-Z6ijgTrG0qVU_cum5viua?dl=0

Помощь в настоящее время высоко ценится.

+1

Пожалуйста, инлайн исходный код ... – lpapp

+0

Что именно вы имеете в виду «отправить текст»? – lpapp

+0

ekhumoro помог мне на этом :) спасибо anyways – user2207196

ответ

0

Один из способов сделать это было бы определить пользовательский сигнал на 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) 
+0

это очень помогло! благодаря! – user2207196

Смежные вопросы