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

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

        self.Tab1Widget1initUI()

        self.bridge = Tab1Widget2()

    def Tab1Widget1initUI(self):
        self.setLayout(QGridLayout())

        self.T1W1_checkbox = QCheckBox('checkbox1', self)
        self.layout().addWidget(self.T1W1_checkbox, 1, 0)

    def test(self):
        print ('123')


    def run(self):
        if self.T1W1_checkbox.isChecked() == True:
            self.test()
            if self.bridge.T1W2_checkbox.isChecked() == True:
                print (self.bridge.T1W2_le.text())

class Tab1Widget2(QWidget):

    def __init__(self, parent=None):
        super().__init__()
        self.setLayout(QGridLayout())

        self.T1W2_checkbox = QCheckBox('checkbox2', self)
        self.layout().addWidget(self.T1W2_checkbox, 0, 0)

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

class Tab1Layout(QWidget):
    def __init__(self, parent=None):
        super().__init__()
        self.setLayout(QGridLayout())

        self.group1 = Tab1Widget1(self)
        scroll = QScrollArea(self)
        scroll.setWidget(self.group1)
        scroll.setWidgetResizable(True)
        self.layout().addWidget(scroll, 0, 0)

        self.group2 = Tab1Widget2(self)
        self.layout().addWidget(self.group2, 1, 0)

        self.btnRun = QPushButton('Run', self)
        self.layout().addWidget(self.btnRun, 3, 0)
        self.btnRun.clicked.connect(self.group1.run)


class Page1(QTabWidget):
    def __init__(self, parent=None):
        super().__init__()
        self.tab1 = Tab1Layout()
        self.addTab(self.tab1, "Tab1")

        self.tab2 = QWidget()
        self.tab3 = QWidget()
        self.addTab(self.tab2, "Tab2")
        self.addTab(self.tab3, "Tab3")
        self.tab2_initUI()
        self.tab3_initUI()

    def tab2_initUI(self):
        grid = QGridLayout()
        self.tab2.setLayout(grid)

    def tab3_initUI(self):
        grid = QGridLayout()
        self.tab3.setLayout(grid)

class MainWindow(QMainWindow):
    def __init__(self, parent=None):
        super().__init__()
        self.setGeometry(450, 250, 800, 550)
        self.startPage1()

    def startPage1(self):
        x = Page1(self)
        self.setWindowTitle("Auto Benchmark")
        self.setCentralWidget(x)
        self.show()

def main():
    app = QApplication(sys.argv)
    main = MainWindow()
    main.show()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

If checkbox1 is checked and I press the run button, it will print 123. However, by pressing run button, I want checkbox2 to also print some texts entered in lineedit if the checkbox1 are also checked (i.e. it should print 123 first and then print 456).

I've looked up some similar types of questions, but none of that provides a proper answer. If anyone knows how to solve it, pls let me know thanks!!

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
ryan9025
  • 267
  • 6
  • 17

1 Answers1

1

The problem is that you are creating several Tab1Widget2, the first one you created in Tab1Layout, and that is the one you see, then you have created another one in Tab1Widget1, but not the time because you have not passed a parent, if you pass it to ** self ** as parent you will observe the following:

self.bridge = Tab1Widget2(self)

enter image description here

which is not what you want, so instead of creating a new you must pass the one that already exists, an option is to pass it through the constructor:

class Tab1Widget1(QWidget):
    def __init__(self, bridge, parent=None): # Modify here
        super().__init__(parent)
        self.Tab1Widget1initUI()
        self.bridge = bridge  #Modify here

    # ...

    def test(self): print ('123')

    def run(self):
        if self.T1W1_checkbox.isChecked():
            self.test()
            if self.bridge.T1W2_checkbox.isChecked():
                print (self.bridge.T1W2_le.text())

class Tab1Widget2(QWidget):
    # ...

class Tab1Layout(QWidget):
    def __init__(self, parent=None):
        super().__init__()
        self.setLayout(QGridLayout())

        self.group2 = Tab1Widget2(self) # Modify here
        self.group1 = Tab1Widget1(self.group2, self) # Modify here
        scroll = QScrollArea(self)
        scroll.setWidget(self.group1)
        scroll.setWidgetResizable(True)
        self.layout().addWidget(scroll, 0, 0)

        # self.group2 = Tab1Widget2(self) # Modify here
        self.layout().addWidget(self.group2, 1, 0)

        # ...
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • But when I do this, it still doesn't show `456` but only `123`when chefckbox1 & checkbox2 are checked? – ryan9025 Sep 30 '17 at 16:44
  • by `456` I meant the text entered in the LineEdit – ryan9025 Sep 30 '17 at 16:50
  • I do not understand you, explain yourself better. – eyllanesc Sep 30 '17 at 16:56
  • I mean if I enter some texts into the lineedit (eg. 456) when both checkbox1&2 are checked, it should print 123 (in checkbox1) & 456 (entered in lineedit). But when I modify the codes as you suggest, it still can't work. – ryan9025 Sep 30 '17 at 17:00
  • Is it weird, has everything changed with a comment? – eyllanesc Sep 30 '17 at 17:02
  • What do you mean? – ryan9025 Sep 30 '17 at 17:04
  • In the code that I have proposed you must modify the lines that have the comment side: `# Modify here` – eyllanesc Sep 30 '17 at 17:05
  • Yeah I tried to modify that but it seems I missed some of your comments. Appreciate! – ryan9025 Sep 30 '17 at 17:11
  • Again really appreciate your help! But I still have a question that why do you put `parent` inside the `super().__init__(parent)` inside the __init__ method inside the `Tab1Widget1`? I tried to remove the `parent` and the codes can still work. Thus Im just wondering if I don't put it there, will there be any impact? – ryan9025 Sep 30 '17 at 18:37
  • The advantage of passing a parent to a widget is that if the parent is removed from memory, it will eliminate their children, that is a good practice implemented by Qt and therefore PyQt. – eyllanesc Sep 30 '17 at 18:45