简体   繁体   中英

Is there a way to call a function everytime a letter is entered in a QInputDialog() in PyQt5?

So I'm making a really simple notepad GUI using PyQt5 , and I have this Find feature, which will search the occurrence of the keyword inside the QTextEdit() entry. I want my app to update the highlighted text everytime a new character is entered. Is this possible in PyQt5?

Here's how my functions look

 def find(self):
    self.find_text = QInputDialog()
    self.find_text.getText(self, "Search Text", "Find:")
    self.find_text.textValueChanged.connect(self.select)


def select(self):
    print('sd')
    print(self.find_text.textValue())

You have to use the textValueChanged signal of QInputDialog.

from PyQt5.QtWidgets import (
    QApplication,
    QInputDialog,
    QPushButton,
    QTextEdit,
    QVBoxLayout,
    QWidget,
)


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

        button = QPushButton("Find")
        self.text_edit = QTextEdit()

        lay = QVBoxLayout(self)
        lay.addWidget(button)
        lay.addWidget(self.text_edit)

        self.input_dialog = QInputDialog()
        self.input_dialog.setWindowTitle("Search Text")
        self.input_dialog.setLabelText("Find:")

        self.input_dialog.textValueChanged.connect(self.search)
        button.clicked.connect(self.input_dialog.show)

    def search(self, text):
        print("search: ", text)


def main():
    app = QApplication([])
    w = Widget()
    w.show()
    app.exec_()


if __name__ == "__main__":
    main()

Note: don't use static methods like getText().

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