QPlainTextEdit达到一定长度时运行方法

时间:2018-09-19 21:50:59

标签: python python-3.x pyqt pyqt5

我有一个pyqt对话框,其中包含以退回结尾的数据列表(来自具有自动退回功能的条形码扫描仪)。

该列表的长度为5个项目,当输入5个项目时,我现在必须单击“添加数据”以运行populate_row方法。

我使用\n将字符串分成一个列表,并根据需要处理每个项目。

add data框中的行数或\n的数量达到5时,是否可以自动按下QPlainTextEdit按钮?

import sys

from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *

class Status(QDialog):
    def __init__(self, parent=None):
        super(Status, self).__init__(parent)

        self.label = QLabel()

        self.btn = QPushButton("Input Data")
        self.btn.clicked.connect(self.populate_row)

        self.layout = QVBoxLayout()

        self.layout.addWidget(self.btn)
        self.layout.addWidget(self.label)

        self.resize(660, 260)
        self.setLayout(self.layout)

    def populate_row(self, letter):
        self.dialog = QDialog()

        self.dialog.resize(660, 260)

        self.textBox = QPlainTextEdit(self.dialog)

        Rbtn = QPushButton("Add Data")
        Rbtn.clicked.connect(
            lambda: self.enter_data(self.textBox.toPlainText()))

        layout = QVBoxLayout(self.dialog)
        layout.addWidget(self.textBox)
        layout.addWidget(Rbtn)

        self.dialog.exec_()

    def enter_data(self, text):
        self.label.setText(text)

        lst = text.split("\n")
        try:
            for  x in lst:
                if x != "":
                    print(x)
                    self.do_something_with_x()
        except IndexError:
            pass

        self.update_data()

        self.dialog.close()

    def do_something_with_x(self):
        print('Something done with x..')

    def update_data(self):
        print('Data updated..')

if __name__ == "__main__":
    app = QApplication(sys.argv)
    ex = Status()
    ex.show()
    sys.exit(app.exec_())

2 个答案:

答案 0 :(得分:1)

  

无效QPlainTextEdit :: blockCountChanged(int newBlockCount)

     

每当块计数改变时,该信号就会发出。新的块计数在newBlockCount中传递。

public class MyEntity {
    public int Id { get; set; }
    public int MyLinkedEntityId { get; set; }
    public MyLinkedEntity MyLinkedEntity { get; set; }
    ...
}

public class MyLinkedEntity {
    public int Id { get; set; }
    public bool MyBoolean { get; set; }
    ...
}

public class MyManyToMany {
    public int MyLinkedEntityId { get; set; }
    public MyLinkedEntity MyLinkedEntity { get; set; }
    public int MyOtherLinkedEntity { get; set; }
    public MyOtherLinkedEntity MyOtherLinkedEntity { get; set; }
}

enter image description here

答案 1 :(得分:1)

要计算换行符,请使用blockCountChanged()信号,如果要单击按钮,则必须使用click()方法。同样要验证字符串是否为空,如果使用x,就足够了:因为字符串是可迭代的,如果为空,则if如果可迭代返回False(在其他情况下返回True)。

import sys
from PyQt5 import QtCore, QtGui, QtWidgets


class Status(QtWidgets.QDialog):
    def __init__(self, parent=None):
        super(Status, self).__init__(parent)
        self.label = QtWidgets.QLabel()
        self.btn = QtWidgets.QPushButton("Input Data")
        self.btn.clicked.connect(self.populate_row)
        layout = QtWidgets.QVBoxLayout(self)
        layout.addWidget(self.btn)
        layout.addWidget(self.label)
        self.resize(660, 260)


    def populate_row(self, letter):
        self.dialog = QtWidgets.QDialog()
        self.dialog.resize(660, 260)
        self.textBox = QtWidgets.QPlainTextEdit()
        self.textBox.blockCountChanged.connect(self.blockCount)
        self.Rbtn = QtWidgets.QPushButton("Add Data")
        self.Rbtn.clicked.connect(self.runcode)
        layout = QtWidgets.QVBoxLayout(self.dialog)
        layout.addWidget(self.textBox)
        layout.addWidget(self.Rbtn)
        self.dialog.exec_()

    @QtCore.pyqtSlot(int)
    def blockCount(self, num):
        if num > 5: self.Rbtn.click()

    def runcode(self):
        self.enter_data(self.textBox.toPlainText())

    def enter_data(self, text):
        self.label.setText(text)
        for  x in text.split("\n"):
            if x:
                print(x)
                self.do_something_with_x()
        self.update_data()
        self.dialog.close()

    def do_something_with_x(self):
        print('Something done with x..')

    def update_data(self):
        print('Data updated..')

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    ex = Status()
    ex.show()
    sys.exit(app.exec_())