简体   繁体   中英

How to have signals and slots connect across classes?

I am trying to make a basic text editor with a toolbar, sidebar, and textbox. My program needs to use a QPushButton (which is in my 'toolbar' class) to make the text in my QTextEdit bold (in my TextEdit class). Here's an example snippet of code:

import sys
from PyQt5.QtWidgets import QPushButton, QTextEdit, QWidget, QMainWindow
from PyQt5.Qt import Qt, QApplication, QVBoxLayout, QHBoxLayout, QFrame, QFont


class ToolBar(QWidget):
    def __init__(self):
        super().__init__()
        layout = QHBoxLayout()
        self.btn = QPushButton(self, text="Bold")
        layout.addWidget(self.btn)

        self.italic = QPushButton(self, text="Italic")
        layout.addWidget(self.italic)
        t = TextEdit()

        # This is the line that isn't working
        self.btn.clicked.connect(lambda: t.set_bold())
        # I've tried this without the lambda, and also with 'TextEdit.set_bold' but they didn't work
        # It would also be nice to do the same for the italic buttons and other buttons in my toolbar 
        self.setLayout(layout)


class TextEdit(QWidget):
    def __init__(self):
        super().__init__()
        self.textEdit = QTextEdit(self)
        # self.set_bold() <-- If I were to run this, then the text box would become bold, so I know that it's not an error with the function

    def set_bold(self):
        # print("function activated") <-- This statement shows that the function is indeed executing
        self.font = QFont("Helvetica", 14)
        self.font.setBold(True)
        self.textEdit.setFont(self.font)


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        central_widget = QFrame()
        layout = QVBoxLayout()

        self.boldButton = ToolBar()
        layout.addWidget(self.boldButton)

        self.textBox = TextEdit()
        layout.addWidget(self.textBox)

        central_widget.setLayout(layout)

        self.setCentralWidget(central_widget)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec())

I tried putting a print statement to confirm that the function is executing, which it is. I don't get any error messages so I can't seem to figure out what to do.

The connection must occur where the objects have a common scope, in this case within the MainWindow class.

class ToolBar(QWidget):
    def __init__(self):
        super().__init__()

        self.bold_button = QPushButton(text="Bold")
        self.italic_button = QPushButton(text="Italic")

        layout = QHBoxLayout(self)
        layout.addWidget(self.bold_button)
        layout.addWidget(self.italic_button)


class TextEdit(QWidget):
    def __init__(self):
        super().__init__()
        self.textEdit = QTextEdit(self)

    def set_bold(self):
        font = QFont("Helvetica", 14)
        font.setBold(True)
        self.textEdit.setFont(font)


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        self.toolbar = ToolBar()
        self.textBox = TextEdit()

        central_widget = QFrame()
        layout = QVBoxLayout(central_widget)
        layout.addWidget(self.toolbar)
        layout.addWidget(self.textBox)
        self.setCentralWidget(central_widget)

        self.toolbar.bold_button.clicked.connect(self.textBox.set_bold)

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