0

I set the printInWidget clicked method for the btnprint button in widget1. But I want to replace that command in MainWindow with command printInMain by code: self.wid.btnprint.clicked.connect(self.printInMain). I don't know why it does both commands in MainWindow. Please help me make the command printInWidget not execute when running MainWidow. Sorry everyone (English is not my native language and I only approached pyqt5 for a few months by teaching myself).

enter image description here

when I click button "printText" enter image description here

my code: mainwindow.py


from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QMainWindow, QApplication, QPushButton, QTextEdit, QGridLayout, QVBoxLayout, QFileDialog,QHBoxLayout,QSpacerItem,QSizePolicy
from PyQt5 import uic, QtCore

import sys
from widget1 import *


class UI(QMainWindow):
    def __init__(self):
        super(UI, self).__init__()
        uic.loadUi("MainWindow.ui", self)
        self.wid = widget1()
        self.verticalLayout.addWidget(self.wid)
        self.wid.btnprint.clicked.connect(self.printInMain)
    # I want to override method of button printText
    def printInMain(self):
        self.wid.labelB.setText('New text (method in Main)')
if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = UI()
    window.show()
    app.exec_()

widget1.py

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QMainWindow
from PyQt5.QtGui import QIcon
from PyQt5 import uic


class widget1(QWidget):
    def __init__(self):
        super().__init__()
        uic.loadUi('widget1.ui',self)
        self.btnprint.clicked.connect(self.printInwidget)

    def printInwidget(self):
        self.labelA.setText('Hello World (Method in Widget)')

if __name__ == '__main__':
    app = QApplication(sys.argv)
    Main = QMainWindow()
    widget = widget1()
    widget.show()
    sys.exit(app.exec_())
  • The code for `widget1` cannot work due to missing dependencies. Also missing UI file. Please provide full code. – rbaleksandar Jan 02 '21 at 08:42
  • @rbaleksandar the provided code, while not fully reproducible due to the missing ui, is clearly enough to understand the question and answer (as you actually did, as a matter of fact, and you could even have used the provided code as basis for the explanation). Also, what missing dependencies? – musicamante Jan 02 '21 at 10:30

1 Answers1

1

You need to disconnect. If you want to use the initial behaviour again you need to connect the signal to the slot again. I would suggest creating a reconnect() function that will do that for you if you decide to do the whole thing many times.

Here is an example.

More info can be found in the official QObject documentation (look for bool QObject::disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *method)).

Also try to provide full code or at least a minimal working example. What you have posted right now cannot work due to missing dependencies and a UI file.

rbaleksandar
  • 8,713
  • 7
  • 76
  • 161