【发布时间】:2021-01-12 12:20:34
【问题描述】:
在我的项目中,我从数据库中为每个产品名称创建了三个项目:QCombobox、QLabel、QCheckbox(将自身名称作为文本提供给 QLabel)。之后,我在 QTableWidget 中列出这些项目。我想做的事情是到达选定的 QCheckbox 的行的 QLabel 的文本和同一行的 QCombobox 的当前索引。
我可以到达 QTableWidget 的选定 QCheckbox,但我不知道如何到达 QCheckbox 行的其他项目。
函数 current_index 应该返回 QLabel 的文本和 QCheckbox 的文本。
import sys
from PyQt5 import QtWidgets,QtGui,QtCore
from PyQt5.QtWidgets import QLabel,QMainWindow,QApplication,QHeaderView,QTableWidgetItem,QMessageBox,QWidget
from uretim_sayfasi import Ui_MainWindow
import mysql.connector
global curs
global conn
conn=mysql.connector.connect(
host="*****",
user="****",
password="*****",
database="****"
)
curs=conn.cursor()
class my_app(QMainWindow):
def __init__(self):
super(my_app, self).__init__()
self.ui=Ui_MainWindow()
self.ui.setupUi(self)
self.list_products()
self.ui.pushButton.clicked.connect(self.current_index)
def list_products(self):
curs.execute("SELECT productName,productId FROM products_list")
products=curs.fetchall()
curs.execute("SELECT shipmentTime FROM shipmentTime ORDER BY id")
shipmentTime=curs.fetchall()
c=0
for i in products:
self.ui.productlist.setRowCount(int(c + 1))
self.ui.productlist.setColumnCount(3)
e=QtWidgets.QLabel()
e.setText(i[0])
self.combo=QtWidgets.QComboBox()
self.combo.addItems(j[0] for j in shipmentTime)
self.checkbox=QtWidgets.QCheckBox(i[1])
self.checkbox.setObjectName(i[1])
self.ui.productlist.setCellWidget(c,2,self.combo)
self.ui.productlist.setCellWidget(c,0,self.checkbox)
self.ui.productlist.setCellWidget(c,1,e)
c+=1
def current_index(self):
items=self.ui.productlist.findChildren(QtWidgets.QCheckBox)
for i in items:
if i.isChecked():
n=i.objectName()
print(n)
def app():
app=QApplication(sys.argv)
win=my_app()
win.show()
sys.exit(app.exec_())
app()
the result table 在我的场景中,当按钮单击时,它应该返回选定的 QCheckbox(ARG.YI.2002 和 ARG.YI.2021)QLabel 的文本**(ARG.YI.2002 的“KREMA”和 ARG.YI 的“CEVİZİÇİEXTRA”。 2021) ** 和 QCombobox 的当前文本 (ARG.YI.2002 为“5 GÜN”,ARG.YI.2021 为“1 GÜN”)。
【问题讨论】:
-
你能更好地解释一下你想从
current_index得到什么吗?考虑到“当前索引”的概念不是被检查项的索引,并且通过您的函数,您还将获得 any 已检查的 QCheckBox。 -
其实我想根据勾选的QCheckbox的行获取QLabel的文字和QCombobox的文字。
标签: python-3.x pyqt5 qtablewidget