如何隐藏Q复选框 当Qcombox框选择一个项目?


from PyQt5 import QtCore, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(762, 590)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.checkBox = QtWidgets.QCheckBox('box', self.centralwidget)
self.checkBox.setGeometry(QtCore.QRect(150, 75, 181, 20))
self.checkBox.setObjectName("checkBox")

self.comboBox = QtWidgets.QComboBox(self.centralwidget)
self.comboBox.setGeometry(QtCore.QRect(150,160,100,20))
self.comboBox.addItem("Yes")
self.comboBox.addItem("No")
self.comboBox.setObjectName("comboBox")

MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "11"))
MainWindow.show()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())

我已经创建了一个带有"是"和"否"的组合框,当我在组合框中选择"否"时,我想隐藏复选框,有人可以帮忙吗?

我试图创建一个函数,当self.comboBox.currentText =="是"时,运行self.checkBox.hide((,但它不起作用...

您必须使用currentTextChanged信号来通知您,如果QComboBox选择已更改,则向您发送新文本,然后您应该仅将其与文本进行比较,并与满足您要求的setVisible()方法一起进行比较。

self.comboBox.currentTextChanged.connect(self.handle_current_text_changed)
def handle_current_text_changed(self, text):
self.checkBox.setVisible(text == "Yes")

使用信号和插槽来执行此操作。 使用插槽隐藏复选框捕获组合框的编辑文本更改信号。

comboBox.currentTextChanged.connect(func)

在函数中,func只需在文本为"否"时将可见性设置为 false,当文本为"是"时设置为 true。

如果要在comboBox状态为HIDE时隐藏复选框,并在组合框状态为UNHIDE时取消隐藏checkBox,请使用IF构造来捕获组合框的状态。根据状态,将一个或另一个值应用于复选框:


class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(762, 590)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.checkBox = QtWidgets.QCheckBox('box', self.centralwidget)
self.checkBox.setGeometry(QtCore.QRect(150, 75, 181, 20))
self.checkBox.setObjectName("checkBox")

self.comboBox = QtWidgets.QComboBox(self.centralwidget)
self.comboBox.setGeometry(QtCore.QRect(150,160,100,20))
self.comboBox.addItem("UNHIDE")
self.comboBox.addItem("HIDE")
self.comboBox.setObjectName("comboBox")

MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
self.comboBox.currentTextChanged.connect(self.hidefunction) # code for connect to function below
def hidefunction(self):
text = str(self.comboBox.currentText()) 
# this is the state of the current text in combobox. Below simple IF block.
if text == "UNHIDE":
self.checkBox.setHidden(False) 
# its HIDE - your checkBox when comboBox changed his state 
else:
self.checkBox.setHidden(True) 
# its HIDE your checkBox when comboBox changed his state 


def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "11"))
MainWindow.show()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())

相关内容

  • 没有找到相关文章

最新更新