Jon4thanPP
Jon4thanPP

Reputation: 35

pyqt signal from a window not changing the main window widgets

I can pass values between two windows using signal, and I want the plain text change, while I press a button from another window. but the insertPlainText and even text.show() are not working

I've tried sending the signal to the init part of the Mainwindow, tried update, repaint, but none of them works.

appreciate for any of your help, thanks!


the search_send method in class SearchWindow to MainWindow method test_

class MainWindow(QtWidgets.QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.ui = Ui_Dialog()
        self.ui.setupUi(self)
        self.ui.tag_box.hide()

    def test_(self, i): # -------problem here------------------
        print(i)  // <- value here were right
        self.ui.tag_box.insertPlainText(i)  # -------Plain Text does not change-------
        self.ui.tag_box.show()# -------Plain Text does not show either--------------


class SearchWindow(QtWidgets.QMainWindow):
    
    signal=pyqtSignal(str)

    def __init__(self, endpoint=None, user=None, password=None, points_link=None):
        super(SearchWindow, self).__init__()
        self.ui = Ui_Search()
        self.ui.setupUi(self)
        self.ui.pushButton_2.clicked.connect(self.search_send)

    def search_send(self): # -------problem here------------------
        tag_list = [tag1,tag2, tag3]
        otherClass = MainWindow()
        self.signal.connect(otherClass.test_)
        for k in tag_list:
            self.signal.emit(k)

if __name__ == '__main__':
    app = QtWidgets.QApplication([])
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())

Upvotes: 0

Views: 1134

Answers (1)

jupiterbjy
jupiterbjy

Reputation: 3503

As we're unclear what Ui_Dialog contains, I can't tell what's wrong exactly, but can show you how Signal should be used.

This is example of Signal that's transferring text to Slot. Name may differ in PyQt5 - change accordingly.

from PySide2.QtWidgets import QWidget, QApplication, QPlainTextEdit, QTextEdit, QVBoxLayout
from PySide2.QtCore import Signal, Slot
import sys


class MainWindow(QWidget):
    sig = Signal(str)

    def __init__(self):
        super(MainWindow, self).__init__()
        self.setWindowTitle("Main")
        self.layout = QVBoxLayout()
        self.text = QTextEdit()
        self.layout.addWidget(self.text)
        self.setLayout(self.layout)

        self.text.textChanged.connect(self.onChange)

    def onChange(self):
        self.sig.emit(self.text.toPlainText())


class SubWindow(QWidget):

    def __init__(self, connect_target: MainWindow):
        super(SubWindow, self).__init__()
        self.setWindowTitle("Sub")
        self.layout = QVBoxLayout()
        self.text = QPlainTextEdit()
        self.layout.addWidget(self.text)
        self.setLayout(self.layout)

        connect_target.sig.connect(self.onSignal)

    @Slot(str)
    def onSignal(self, text):
        self.text.insertPlainText(text + '\r\n')


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    window_sub = SubWindow(window)
    window_sub.show()
    sys.exit(app.exec_())

Anything inside Main will be inserted to QPlainTextEdit as you wanted.

enter image description here

Upvotes: 1

Related Questions