如何在PyQt5中单击按钮时从表格中删除复选框?

时间:2019-04-14 15:32:03

标签: python pyqt5

我正在创建一个房间预订程序,为此程序我使用一个表格,以便用户可以选中一个复选框并单击“ book”,然后我的程序会将详细信息添加到数据库中。但是,为了确保不会发生超额预订一个房间+时间段的情况,我希望某种迹象表明房间+时间段已被占用-因此我认为一旦将其添加到复选框中,我便可以取消选中该复选框数据库。有没有办法做到这一点,还是我应该创建一个错误窗口,因为房间和时间的组合已经存在于数据库中了?

有人对我如何表明房间和时隙已经被预订有其他建议吗?

主窗口

class Ui_ViewAllRooms(QtWidgets.QMainWindow, Ui_ViewAllRooms):
    def __init__(self, username, parent=None):
        super(Ui_ViewAllRooms, self).__init__(parent)
        self.username = username
        self.setupUi(self)
        self.dialogBook = Ui_DialogBook()
        self.book_Button.clicked.connect(self.book_clicked)

    @QtCore.pyqtSlot()
    def book_clicked(self):
        self._checked_items = []
        for i in range(self.tableWidget.rowCount()):
            for j in range(self.tableWidget.columnCount()):
                item = self.tableWidget.item(i, j)
                if item.checkState() == QtCore.Qt.Checked:
                    self.v = self.tableWidget.horizontalHeaderItem(j).text()
                    self.h = self.tableWidget.verticalHeaderItem(i).text()
                    self._checked_items.append([self.h, self.v, self.tableWidget.item(i, j).text()])

        self.dialogBook.listWidget.clear()
        self.dialogBook.listWidget.addItems([ "{} {} {}".format(self.h, self.v, it) for self.h, self.v, it in self._checked_items])
        self.dialogBook.show()
        self.dialogBook.yes_button.clicked.connect(self.addBooking)

    def addBooking(self):
        now = QDate.currentDate()
        now1 = now.toString(Qt.DefaultLocaleLongDate)
        room,lesson,irr = zip(*self._checked_items)
        connection = sqlite3.connect("roombooking.db")
        c = connection.cursor()

        lastBookingId = []
        for item in lesson:
            c.execute("INSERT INTO Booking (lessonBooked, dateBooked, username) VALUES (?,?,?)",(item,now1,self.username))
            connection.commit()
            lastBookingId.append (c.lastrowid)

        for i in range (len (lastBookingId)):
            roomb = room[i]
            lastBookingId1 = lastBookingId[i]
            c.execute ("INSERT INTO BookedRoom (roomNO, bookingID) VALUES (?, ?)",(roomb,lastBookingId1))
            connection.commit()
##### I would like the checkboxes selected to be removed somewhere here #####
        self.dialogBook.hide()

Ui_ViewAllRooms

class Ui_ViewAllRooms(object):
    def setupUi(self, ViewAllRooms):

        ViewAllRooms.setObjectName("ViewAllRooms")
        ViewAllRooms.resize(671, 347)

        self.centralwidget = QtWidgets.QWidget(ViewAllRooms)
        self.centralwidget.setObjectName("centralwidget")
        self.tableWidget = QtWidgets.QTableWidget(self.centralwidget)
        self.tableWidget.setGeometry(QtCore.QRect(10, 100, 651, 181))

        self.tableWidget.setObjectName("tableWidget")
        self.tableWidget.setColumnCount(6)
        self.tableWidget.setRowCount(5)
        self.tableWidget.setEditTriggers(QAbstractItemView.NoEditTriggers)

        self.tableWidget.setHorizontalHeaderLabels("P1 P2 P3 P4 P5 P6".split())
        self.tableWidget.setVerticalHeaderLabels("C101 C214 C320 F04 E201".split())

        for i in range(self.tableWidget.rowCount()):
            for j in range(self.tableWidget.columnCount()):
                item = QtWidgets.QTableWidgetItem()
                item.setFlags(item.flags() | QtCore.Qt.ItemIsUserCheckable)
                item.setCheckState(QtCore.Qt.Unchecked)
                self.tableWidget.setItem(i, j, item)
                self._checked_items = []

        self.book_Button = QtWidgets.QPushButton(self.centralwidget)
        self.book_Button.setGeometry(QtCore.QRect(300, 290, 75, 23))
        self.book_Button.setObjectName("book_Button")

        ViewAllRooms.setCentralWidget(self.centralwidget)
        self.retranslateUi(ViewAllRooms)
        QtCore.QMetaObject.connectSlotsByName(ViewAllRooms)

    def retranslateUi(self, ViewAllRooms):
        _translate = QtCore.QCoreApplication.translate
        ViewAllRooms.setWindowTitle(_translate("ViewAllRooms", "MainWindow"))
        item = self.tableWidget.verticalHeaderItem(0)
        item.setText(_translate("ViewAllRooms", "C101"))
        item = self.tableWidget.verticalHeaderItem(1)
        item.setText(_translate("ViewAllRooms", "C214"))
        item = self.tableWidget.verticalHeaderItem(2)
        item.setText(_translate("ViewAllRooms", "C320"))
        item = self.tableWidget.verticalHeaderItem(3)
        item.setText(_translate("ViewAllRooms", "F04"))
        item = self.tableWidget.verticalHeaderItem(4)
        item.setText(_translate("ViewAllRooms", "E201"))
        item = self.tableWidget.horizontalHeaderItem(0)
        item.setText(_translate("ViewAllRooms", "P1"))
        item = self.tableWidget.horizontalHeaderItem(1)
        item.setText(_translate("ViewAllRooms", "P2"))
        item = self.tableWidget.horizontalHeaderItem(2)
        item.setText(_translate("ViewAllRooms", "P3"))
        item = self.tableWidget.horizontalHeaderItem(3)
        item.setText(_translate("ViewAllRooms", "P4"))
        item = self.tableWidget.horizontalHeaderItem(4)
        item.setText(_translate("ViewAllRooms", "P5"))
        item = self.tableWidget.horizontalHeaderItem(5)
        item.setText(_translate("ViewAllRooms", "P6"))
        __sortingEnabled = self.tableWidget.isSortingEnabled()
        self.tableWidget.setSortingEnabled(False)
        self.tableWidget.setSortingEnabled(__sortingEnabled)
        self.book_Button.setText(_translate("ViewAllRooms", "Book"))

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    ViewAllRooms = QtWidgets.QMainWindow()
    ui = Ui_ViewAllRooms()
    ui.setupUi(ViewAllRooms)
    ViewAllRooms.show()

    sys.exit(app.exec_())

2 个答案:

答案 0 :(得分:1)

您只需要获取商品的位置并使用takeItem()即可删除

for i in range(self.tableWidget.rowCount()):
    for j in range(self.tableWidget.columnCount()):
        item = self.tableWidget.item(i, j)
        if item.checkState() == QtCore.Qt.Checked:
            it = self.tableWidget.takeItem(i, j)
            del it

答案 1 :(得分:0)

试试 setData()QVariant()

from PyQt5 import QtCore
from PyQt5.QtCore import QVariant 

for i in range(self.tableWidget.rowCount()):
    for j in range(self.tableWidget.columnCount()):
        item = self.tableWidget.item(i, j)
        if item.checkState() == QtCore.Qt.Checked:
             item.setData(QtCore.Qt.CheckStateRole, QVariant())