简体   繁体   中英

Python PyQt: I could not display the data in lineedit from external program

I am trying to display runtime data from EPICS (Package) in lineedit. I can receive and send my data through my program but when I can't display the data. The function pv.get() is giving the data from EPICS to Python. Please suggest me the changes because the SIGNAL function in connect is giving the error

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

class MyFrame(QWidget):
    def __init__(self, parent=None):
            QWidget.__init__(self)

            self.lineedit = QLineEdit(self)
            self.lineedit.setGeometry(QRect(250,450,75,28))

            pv=epics.PV('calc:sum.VAL')
            self.lineedit=pv.get()

            self.connect(self.lineedit, SIGNAL("textChanged()"), self.changedata)
            self.color = QColor(Qt.blue)

            self.show()

    def changedata (self):

            pv=epics.PV('calc:sum.VAL')
            self.lineedit=pv.get()
            text=pv.get()


            self.update()


app=QApplication(sys.argv)
f=MyFrame()
f.show()
app.exec_()

`

Credit to @ivica for the suggestion. I saw it was 11 hours with no answer so I thought I would contribute

You are replacing your QLineEdit widget with whatever data your pv.get() is returning. What you want to do is actually set the data onto the widget.

class MyFrame(QWidget):
    def __init__(self, parent=None):
        QWidget.__init__(self)

        self.lineedit = QLineEdit(self)
        self.lineedit.setGeometry(QRect(250,450,75,28))

        pv=epics.PV('calc:sum.VAL')
        self.lineedit.setText(pv.get())

        # new-style signal slot connections
        self.lineedit.textChanged.connect(self.changedata)
        self.color = QColor(Qt.blue)


    def changedata (self):

        pv=epics.PV('calc:sum.VAL')
        self.lineedit.setText(pv.get())

Update

Examining your code example more closely I see some more potential issues.

Though I am using the new style signal connections, in your old style you are missing the proper signature:

self.connect(self.lineedit, SIGNAL("textChanged()"), self.changedata)
# should be
self.connect(self.lineedit, SIGNAL("textChanged(QString)"), self.changedata)

Now, that being said... I don't understand why you would connect the changing of the text field to a slot that then changes the text field. It will cause a couple recursive triggers. If what you are trying to do is catch the editing of the text, and instead call your epics lib and set the text from that, then you should be using the textEdited signal which will not be emitted when you programmatically change the field.

# new style
self.lineedit. textEdited.connect(self.changedata)
# old style
self.connect(self.lineedit, SIGNAL("textEdited(QString)"), self.changedata)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM