我查看了创建QTableWidgetItems的文档,并说它需要使用item()
方法。
"返回给定行和列的项(如果已设置);否则返回0。"
以下是我如何使用它:
self.table.item(item.row(),1)
这些值肯定有一个项目。
item.row()
是与我尝试返回的同一行中的项关联的行。关于该项目的一切工作正常,.text()
返回正确的信息。我用这个项目得到了
self.table.itemChanged.connect(self.log_change)
由于某种原因,我试图返回的这个项目并不起作用。当我去print(self.table.item(item.row(),1).text())
它什么都不打印,只是一个空字符串。它应该打印的值是int(0)
围绕着int()
并不起作用。
我是否可以通过某种方式选择或返回该行和列中的项目,并.text()
为其提供int(0)
作为结果?
当QTableWidget显示数字0时,为什么只是打印一个空字符串?
编辑:
self.cur.execute("""SELECT s.StudentID, s.FullName, m.PreviouslyMailed, m.nextMail, m.learnersDate, m.RestrictedDate, m.DefensiveDate FROM
StudentProfile s LEFT JOIN Mailouts m ON s.studentID=m.studentID""")
self.all_data = self.cur.fetchall()
self.search_results()
self.table.setRowCount(len(self.all_data))
self.tableFields = ["Check","REMOVE THIS","Full name","Previously mailed?","Next mail","learners date","Restricted date","Defensive driving date"]
self.columnList = ["StudentID","FullName","PreviouslyMailed","NextMail","learnersDate","RestrictedDate","DefensiveDate"]
self.table.setColumnCount(len(self.tableFields))
self.table.setHorizontalHeaderLabels(self.tableFields)
self.checkbox_list = []
for i, item in enumerate(self.all_data):
FullName = QtGui.QTableWidgetItem(str(item[1]))
PreviouslyMailed = QtGui.QComboBox()
PreviouslyMailed_combobox_list = ["No", "Yes"]
PreviouslyMailed.addItems(PreviouslyMailed_combobox_list)
LearnersDate = QtGui.QTableWidgetItem(str(item[4]))
RestrictedDate = QtGui.QTableWidgetItem(str(item[5]))
DefensiveDate = QtGui.QTableWidgetItem(str(item[6]))
NextMail = QtGui.QTableWidgetItem(str(item[3]))
StudentID = QtGui.QTableWidgetItem(item[0])
self.table.setItem(i, 1, StudentID)
self.table.setItem(i, 2, FullName)
self.table.setCellWidget(i, 3, PreviouslyMailed)
self.table.setItem(i, 4, LearnersDate)
self.table.setItem(i, 5, RestrictedDate)
self.table.setItem(i, 6, DefensiveDate)
self.table.setItem(i, 7, NextMail)
chkBoxItem = QtGui.QTableWidgetItem()
chkBoxItem.setFlags(QtCore.Qt.ItemIsUserCheckable | QtCore.Qt.ItemIsEnabled)
chkBoxItem.setCheckState(QtCore.Qt.Unchecked)
self.checkbox_list.append(chkBoxItem)
self.table.setItem(i, 0, self.checkbox_list[i])
FullName.setFlags(FullName.flags() & ~Qt.ItemIsEditable)
NextMail.setFlags(NextMail.flags() & ~Qt.ItemIsEditable)
self.table.blockSignals(False)
self.changed_items = []
self.table.itemChanged.connect(self.log_change)
def log_change(self, item):
self.table.blockSignals(False)
self.changed_items.append((self.table.itemAt(item.row(),1),item))
def click_btn_edit(self):
print("Updating")
IDList = []
for item in self.changed_items:
text, col, row = item[1].text(), item[1].column(), item[1].row()
new_data = self.all_data
IDvar = item[0].text()
#IDvar prints as an empty string.
IDList.append(IDvar)
第二次编辑:
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4 import *
import sqlite3
from datetime import datetime
import calendar
import sip
sip.setapi('QVariant', 2)
class WindowContainer():
def __init__(self):
self.windows = []
def _fromUtf8(t):
return t
class mainWindowTemplate(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setObjectName(_fromUtf8("mainWindowTemplate"))
self.resize(800, 600)
self.centralwidget = QtGui.QWidget(self)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.gridLayout = QtGui.QGridLayout(self.centralwidget)
self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
self.table = QtGui.QTableWidget(self.centralwidget)
self.table.setObjectName(_fromUtf8("table"))
self.table.setColumnCount(0)
self.table.setRowCount(0)
self.gridLayout.addWidget(self.table, 0, 0, 1, 1)
self.btn_edit = QtGui.QPushButton(self.centralwidget)
self.btn_edit.setObjectName(_fromUtf8("btn_edit"))
self.gridLayout.addWidget(self.btn_edit, 1, 0, 1, 1)
self.setCentralWidget(self.centralwidget)
self.btn_edit.clicked.connect(self.click_btn_edit)
self.menubar = QtGui.QMenuBar(self)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
self.menubar.setObjectName(_fromUtf8("menubar"))
self.setMenuBar(self.menubar)
"""self.statusbar = QtGui.QStatusBar(self)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
self.setStatusBar(self.statusbar)"""
self.setWindowTitle("MainWindow")
self.btn_edit.setText("Edit")
self.conn = sqlite3.connect("EDA_Database.db")
self.cur = self.conn.cursor()
QtCore.QMetaObject.connectSlotsByName(self)
self.show()
self.click_btn_mailingInfo()
def retranslateUi(self):
self.setWindowTitle(_translate("MainWindow", "MainWindow", None))
def click_btn_mailingInfo(self):
self.table.blockSignals(True)
self.screen_name = "mailingInfo"
self.cur.execute("""SELECT s.StudentID, s.FullName, m.PreviouslyMailed, m.nextMail, m.learnersDate, m.RestrictedDate, m.DefensiveDate FROM
StudentProfile s LEFT JOIN Mailouts m ON s.studentID=m.studentID""")
self.all_data = self.cur.fetchall()
self.table.setRowCount(len(self.all_data))
self.tableFields = ["Check","If you see this column, an error has occured.","Full name","Previously mailed?","Next mail","learners date","Restricted date","Defensive driving date"]
self.columnList = ["StudentID","FullName","PreviouslyMailed","NextMail","learnersDate","RestrictedDate","DefensiveDate"]
self.table.setColumnCount(len(self.tableFields))
self.table.setHorizontalHeaderLabels(self.tableFields)
self.checkbox_list = []
for i, item in enumerate(self.all_data):
FullName = QtGui.QTableWidgetItem(str(item[1]))
PreviouslyMailed = QtGui.QComboBox()
PreviouslyMailed_combobox_list = ["No", "Yes"]
PreviouslyMailed.addItems(PreviouslyMailed_combobox_list)
LearnersDate = QtGui.QTableWidgetItem(str(item[4]))
RestrictedDate = QtGui.QTableWidgetItem(str(item[5]))
DefensiveDate = QtGui.QTableWidgetItem(str(item[6]))
NextMail = QtGui.QTableWidgetItem(str(item[3]))
StudentID = QtGui.QTableWidgetItem(str(item[0]))
self.table.setItem(i, 1, StudentID)
self.table.setItem(i, 2, FullName)
self.table.setCellWidget(i, 3, PreviouslyMailed)
self.table.setItem(i, 4, LearnersDate)
self.table.setItem(i, 5, RestrictedDate)
self.table.setItem(i, 6, DefensiveDate)
self.table.setItem(i, 7, NextMail)
chkBoxItem = QtGui.QTableWidgetItem()
chkBoxItem.setFlags(QtCore.Qt.ItemIsUserCheckable | QtCore.Qt.ItemIsEnabled)
chkBoxItem.setCheckState(QtCore.Qt.Unchecked)
self.checkbox_list.append(chkBoxItem)
self.table.setItem(i, 0, self.checkbox_list[i])
FullName.setFlags(FullName.flags() & ~Qt.ItemIsEditable)
NextMail.setFlags(NextMail.flags() & ~Qt.ItemIsEditable)
self.table.setColumnHidden(1, True)
self.table.blockSignals(False)
self.changed_items = []
self.combobox_item_list = []
self.table.itemChanged.connect(self.log_change)
#PreviouslyMailed.currentIndexChanged.connect(self.comboBox_change(None,None,row,1,self.table.cellWidget(row,1).currentText()))
def log_change(self, item):
self.table.blockSignals(False)
self.changed_items.append((self.table.item(item.row(),1),item))
def click_btn_edit(self):
IDList = []
for item in self.changed_items:
text, col, row = item[1].text(), item[1].column(), item[1].row()
new_data = self.all_data
IDvar = item[0].text()
#no longer works
print(text)
#now works
print(IDvar)
IDList.append(IDvar)
if __name__ == "__main__":
container = WindowContainer()
app = QApplication(sys.argv)
container.windows.append(mainWindowTemplate())
sys.exit(app.exec_())
答案 0 :(得分:0)
如果查看QTableWidgetItem的qt文档,您将看到它只能从另一个项目,字符串,图标和字符串或表示项目类型ID的整数构造。您正在为构造函数直接提供学生ID,因此它正在使用最后一个构造函数,该构造函数创建一个空项。所以,使用这个:
StudentID = QtGui.QTableWidgetItem(str(item[0]))
修复后,您仍然遇到连接到click_btn_mailingInfo中的表itemChanged信号的问题。验证此函数仅被调用一次(特别是禁用行,以便我们按名称连接s以查看它是否有所不同)。