Waiting in for loop until QRadioButton get checked

2019-07-29 22:56发布

问题:

I have a situation where i need to get Pass/Fail from tester for every test step in PySide GUI. Now the data of testsuite i am running in for loop and trying to get current checked/unchecked state of QRadioButton in for loop based on which i will do further code processing. My code is :-

for i in range(self.ui.hlfDataset_sa_lst.count()):

    self.ui.pass_radio.setChecked(False)
    self.ui.fail_radio.setChecked(False)

    print "command ", str(self.ui.hlfDataset_sa_lst.item(i).text())
    print "Run  ", str(i)+" is here"
    ##
    self.telnetThread = TelnetThread.SocketTunnel("localhost",2000)
    returnCommand = self.telnetThread.communicateSock(str(self.ui.hlfDataset_sa_lst.item(i).text()))
    print "returnCommand ",returnCommand
    ##XML Data structure
    result = ET.SubElement(results,"result")
    testcasestepno = ET.SubElement(result,"testcasestepno")
    testerComment = ET.SubElement(result,"testerComment")
    testresult = ET.SubElement(result,"testresult")
    mguImage = ET.SubElement(result,"mguImage")

    if self.ui.pass_radio.isChecked():
        print "TC passed "
        testcasestepno.text = str(i+1)
        testresult.text = "PASS"
        mguImage.text = "NA"
        testerComment.text=str(self.ui.testercomment_txt.text())
    elif self.ui.fail_radio.isChecked():
        if not str(self.ui.testercomment_txt.text()):
            QtGui.QMessageBox.critical(self, 'Tester Comment ', 'Tester Comment is desired ', QtGui.QMessageBox.Ok)
            self.ui.pass_radio.setChecked(False)
            self.ui.fail_radio.setChecked(False)
        else:
            print "TC failed "
            testcasestepno.text = str(i+1)
            testresult.text = "FAIL"
            testerComment.text = str(self.ui.testercomment_txt.text())
            #Save Live Image when failed

I want for loop to wait until tester has provided the input and i don't want to put sleep or in anyway to use thread unless convenient way is shown. This code runs complete loop without waiting for input.

回答1:

If I understood you correctly, you want to wait until one of buttons (fail_radio or pass_radio) is checked before if self.ui.pass_radio.isChecked(): line.

In Qt, you can achieve this using QEventLoop like here: waiting for a signal, where signal you want to wait for is clicked. You need to connect both buttons' signals to quit slot before executing it. For signal/slot connecting in PyQt you can look here: http://pyqt.sourceforge.net/Docs/PyQt5/signals_slots.html

So you need to write something like:

loop = QtCore.QEventLoop()
self.ui.fail_radio.clicked.connect(loop.quit)
self.ui.pass_radio.clicked.connect(loop.quit)
loop._exec()