Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Language Bindings
  4. [Solved] PyQt4 - Passing data between widget instances
Forum Updated to NodeBB v4.3 + New Features

[Solved] PyQt4 - Passing data between widget instances

Scheduled Pinned Locked Moved Language Bindings
2 Posts 1 Posters 7.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    pyPlease
    wrote on last edited by
    #1

    I can't seem to figure out how to do this. I'd like to create a mainWidget that handles instances of several other widgets where the user can interact with one instance and the other instances will recognize their interaction.

    The code below is an operational example where it prints the users interaction instead of passing the values back to either the mainWidget (that's where I'm stuck). mainWidget creates WidgetA and a pushButton. On click widgetB is created (lineEdit & PushBtn), on click of widgetB pushbutton I'd like data in line edit to replace text of existing instance WidgetA (self.editor).

    @import sys
    from PyQt4.QtCore import *
    from PyQt4.QtGui import *

    class mainWidget(QWidget):
    def init(self,parent):
    super(mainWidget, self).init(parent)

        '''Widget A'''
        self.editor = WidgetA(self)
        self.Btn = QPushButton('input button', self)
        
        mainLayout = QVBoxLayout()
        mainLayout.addWidget(self.Btn)
        mainLayout.addWidget(self.editor)
        self.setLayout(mainLayout)  
        
        #signals
        self.connect(self.Btn,
                     SIGNAL("clicked()"),self.BtnClick)
                     
    def BtnClick(self):
        #on self.Btn Click create:
        '''Widget B'''
        self.inputDlg = WidgetB(self)
        self.inputDlg.show()
        #Tried a setText function here that would change self.Editor Text by
        #referencing a global variable but that did not work either
    

    class WidgetA(QTextEdit):
    def init(self, parent):
    super(WidgetA, self).init(parent)
    self.setPlainText("Starting Text")

    class WidgetB(QDialog):
    def init(self, parent):
    super(WidgetB, self).init(parent)

        self.inputBox = QLineEdit(self)
        self.ok = QPushButton('input button', self)
    
        mainLayout = QVBoxLayout()
        mainLayout.addWidget(self.ok)
        mainLayout.addWidget(self.inputBox)
        self.setLayout(mainLayout)
        self.setLayout(mainLayout)  
        
        #signals
        self.connect(self.ok,
                     SIGNAL("clicked()"),self.changeWidgetAText)
    
    def changeWidgetAText(self):
        #Would like this signal to cahnge widgetA's text instance "editor
        print("Would like widget A instance self.editor to read '%s'" % (self.inputBox.text()))
        self.close()
    

    if name == "main":
    app = QApplication(sys.argv)
    form = mainWidget(None)
    form.show()
    app.exec_()@

    1 Reply Last reply
    0
    • P Offline
      P Offline
      pyPlease
      wrote on last edited by
      #2

      the solution is:

      @ def BtnClick(self):
      #on self.Btn Click create:
      '''Widget B'''
      inputDlg = WidgetB(self)
      inputDlg.show()
      if inputDlg.exec_():
      self.editor.acceptUserInput(inputDlg.userInput())@

      userInput being a getter in WidgetB
      acceptUserInput being a setter in WidgetA

      Other possible solution would be using global variable but you still need .exec_() to make it work.

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved