0

By clicking a button, I want to print some texts that is entered in QLineEdit when a checkbox is checked. My example codes are as below:

import sys
import PyQt4
from PyQt4 import QtGui, QtCore
from PyQt4.QtGui import *
from PyQt4.QtCore import *

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

        self.setLayout(layout)

        self.checkBox = QCheckBox()
        layout.addWidget(self.checkBox, 0, 0)


        self.le = QLineEdit()
        layout.addWidget(self.le, 0, 1)

        self.btn = QPushButton('Run')
        layout.addWidget(self.btn, 0, 3)


class Func ():
    def __init__(self):
        a = Widget(self)

    def someFunc(self):
        ##print ()


app = QApplication(sys.argv)
widget = Widget()
widget.show()
app.exec_()

As you can see above, I want the button in "Widget" class to connect to "someFunc" method in "Func" class. Thus when some texts are entered in "self.le" as well as "checkBox" is checked, I want "someFunc" to print the texts entered in "self.le" by clicking the button. If the "checkbox" is not checked, clicking the button should not cause anything to happen even when some texts are entered.

If anyone knows how to solve it, pls let me know thanks!!

ryan9025
  • 267
  • 6
  • 17

1 Answers1

0

You need to connect the button's clicked signal to the function that will handle it. Like this: button.clicked.connect(handler_function)

import sys
import PyQt5
from PyQt5.QtWidgets import *

class Func ():
    def __init__(self, widget):
        self.w = widget

    def someFunc(self):
        if self.w.checkBox.isChecked():
            print(self.w.le.text())

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

        self.setLayout(layout)

        self.checkBox = QCheckBox()
        layout.addWidget(self.checkBox, 0, 0)


        self.le = QLineEdit()
        layout.addWidget(self.le, 0, 1)

        self.btn = QPushButton('Run')
        layout.addWidget(self.btn, 0, 3)

        # connecting to a method in this class
        # self.btn.clicked.connect(self.some_func)

        #connecting to a method in another class
        self.handler_class = Func(self)
        self.btn.clicked.connect(self.handler_class.someFunc)

    def some_func(self):
        if self.checkBox.isChecked():
            print(self.le.text())


app = QApplication(sys.argv)
widget = Widget()
widget.show()
app.exec_()

Edit: Put simply: In the Func class self.w contains a reference to the widget from which a signal will be emitted when the button is clicked.

Why am I keeping a reference to that widget? So that I can access the widget's combobox and lineedit. Without a way to access them I can't see if the checkbox is checked or what the user typed in the textedit.

Anonta
  • 2,500
  • 2
  • 15
  • 25
  • Could you do an example when someFunc is in another class like Func? Because I actually have another much bigger project while this someFunc has to be in another class. Thanks! – ryan9025 Sep 30 '17 at 09:09
  • I edited the code and connected the signal to a method of `Func` class. Also, I'm passing an instance of the widget to the `Func` class during it's construction so that it can see if the `checkbox` is checked and get the string form the `lineedit` when a signal get emitted. – Anonta Sep 30 '17 at 09:27
  • Thanks! But I don't get it that why a instance variable "self.w" should be put in order to receive the signal? – ryan9025 Sep 30 '17 at 10:12
  • You don't need it to get the signal. You need it to access the checkbox and lineedit from the `Func` class. – Anonta Sep 30 '17 at 10:17
  • How does `widget` get anything to do with class `Widget`? How does it get passed through? – ryan9025 Sep 30 '17 at 10:36
  • I added some explanation at the end of the answer. If you're still confused let me know. – Anonta Sep 30 '17 at 10:40
  • Thanks but I'm still confused... I know that `self.w = widget` is for referring to the `checkBox` inside the class `Widget`. But how does `widget` inside the class `Func` get passed through the class `Widget`? Where does it inherent from? Because I don't see a direct line in the codes that `widget` equals to `Widget` – ryan9025 Sep 30 '17 at 10:51
  • In this line: `self.handler_class = Func(self)` I'm passing `self` a reference to this widget to the `Func` constructor. – Anonta Sep 30 '17 at 10:53
  • So after that in `__init__(self, widget)` it becomes to `__init__(self, self.handler_class)` ? – ryan9025 Sep 30 '17 at 10:58
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/155655/discussion-between-anonta-and-ryan9025). – Anonta Sep 30 '17 at 10:59