I am working on a customers record programm,that will be used in beauty saloon.
.
So, I need to fill my table widget using this dialog window.
And each record need to be placed in exact time position. So,how can it be achieved?
Here is my Interface and Dialog code created by QtDesigner.
Table Widget
self.tableWidget = QtWidgets.QTableWidget(self.centralwidget)
self.tableWidget.setGeometry(QtCore.QRect(330, 0, 521, 781))
self.tableWidget.setObjectName("tableWidget")
self.tableWidget.setColumnCount(5)
self.tableWidget.setRowCount(25)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(0, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(1, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(2, item)
item = QtWidgets.QTableWidgetItem()
#and so on...
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "Главная"))
self.addclient.setText(_translate("MainWindow", "Записать клиента"))
self.uslugi.setText(_translate("MainWindow", "Список услуг"))
self.mastera.setText(_translate("MainWindow", "Список мастеров"))
item = self.tableWidget.horizontalHeaderItem(0)
item.setText(_translate("MainWindow", "Время"))
item = self.tableWidget.horizontalHeaderItem(1)
item.setText(_translate("MainWindow", "Мастер"))
item = self.tableWidget.horizontalHeaderItem(2)
item.setText(_translate("MainWindow", "Услуга"))
item = self.tableWidget.horizontalHeaderItem(3)
item.setText(_translate("MainWindow", "Имя "))
item = self.tableWidget.horizontalHeaderItem(4)
item.setText(_translate("MainWindow", "Номер"))
__sortingEnabled = self.tableWidget.isSortingEnabled()
self.tableWidget.setSortingEnabled(False)
item = self.tableWidget.item(0, 0)
item.setText(_translate("MainWindow", "8:00"))
item = self.tableWidget.item(1, 0)
item.setText(_translate("MainWindow", "8:30"))
item = self.tableWidget.item(2, 0)
#...
self.tableWidget.setSortingEnabled(__sortingEnabled)
And Dialog window
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(281, 176)
self.comboBox = QtWidgets.QComboBox(Dialog)
self.comboBox.setGeometry(QtCore.QRect(90, 10, 71, 22))
self.comboBox.setObjectName("comboBox")
self.comboBox.addItem("")
self.comboBox_2 = QtWidgets.QComboBox(Dialog)
self.comboBox_2.setGeometry(QtCore.QRect(10, 10, 71, 22))
self.comboBox_2.setObjectName("comboBox_2")
self.comboBox_2.addItem("")
self.dateTimeEdit = QtWidgets.QDateTimeEdit(Dialog)
self.dateTimeEdit.setGeometry(QtCore.QRect(10, 60, 194, 22))
self.dateTimeEdit.setObjectName("dateTimeEdit")
self.label_2 = QtWidgets.QLabel(Dialog)
self.label_2.setGeometry(QtCore.QRect(10, 40, 91, 16))
self.label_2.setObjectName("label_2")
self.lineEdit = QtWidgets.QLineEdit(Dialog)
self.lineEdit.setGeometry(QtCore.QRect(150, 110, 113, 20))
self.lineEdit.setObjectName("lineEdit")
self.label_3 = QtWidgets.QLabel(Dialog)
self.label_3.setGeometry(QtCore.QRect(150, 90, 121, 16))
self.label_3.setObjectName("label_3")
self.lineEdit_2 = QtWidgets.QLineEdit(Dialog)
self.lineEdit_2.setGeometry(QtCore.QRect(10, 110, 113, 20))
self.lineEdit_2.setObjectName("lineEdit_2")
self.label_4 = QtWidgets.QLabel(Dialog)
self.label_4.setGeometry(QtCore.QRect(10, 90, 111, 16))
self.label_4.setObjectName("label_4")
self.pushButton = QtWidgets.QPushButton(Dialog)
self.pushButton.setGeometry(QtCore.QRect(150, 140, 111, 23))
self.pushButton.setObjectName("pushButton")
self.pushButton_2 = QtWidgets.QPushButton(Dialog)
self.pushButton_2.setGeometry(QtCore.QRect(10, 140, 111, 23))
self.pushButton_2.setObjectName("pushButton_2")
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Новый клиент"))
self.comboBox.setItemText(0, _translate("Dialog", "Услуга"))
self.comboBox_2.setItemText(0, _translate("Dialog", "Мастер"))
self.label_2.setText(_translate("Dialog", "Дата и время:"))
self.label_3.setText(_translate("Dialog", "Контактный номер:"))
self.label_4.setText(_translate("Dialog", "Имя клиента"))
self.pushButton.setText(_translate("Dialog", "Добавить клиента"))
self.pushButton_2.setText(_translate("Dialog", "Отмена записи"))
So i need inftomation in Dialog's input fields to be placed in exact time in a table widget. I'll appreciate any help! Sorry for QtDesigner abuse :D
This is a Record client button function that opens the dialog window
class MyDialog(QtWidgets.QDialog):
def __init__(self, parent=None):
super(MyDialog, self).__init__(parent)
self.ui = Ui_Dialog()
self.ui.setupUi(self)
class MyWin(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MyWin, self).__init__(parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.addclient.clicked.connect(self.addclientfunction)
def addclientfunction(self):
self.dialog = MyDialog()
self.dialog.show()
Entire MyWidnow Class
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(860, 803)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.calendarWidget = QtWidgets.QCalendarWidget(self.centralwidget)
self.calendarWidget.setGeometry(QtCore.QRect(0, 0, 331, 191))
self.calendarWidget.setObjectName("calendarWidget")
self.addclient = QtWidgets.QPushButton(self.centralwidget)
self.addclient.setGeometry(QtCore.QRect(0, 350, 331, 81))
font = QtGui.QFont()
font.setFamily("Verdana")
font.setPointSize(10)
self.addclient.setFont(font)
self.addclient.setObjectName("addclient")
self.uslugi = QtWidgets.QPushButton(self.centralwidget)
self.uslugi.setEnabled(True)
self.uslugi.setGeometry(QtCore.QRect(0, 270, 331, 81))
font = QtGui.QFont()
font.setFamily("Verdana")
font.setPointSize(10)
self.uslugi.setFont(font)
self.uslugi.setObjectName("uslugi")
self.mastera = QtWidgets.QPushButton(self.centralwidget)
self.mastera.setGeometry(QtCore.QRect(0, 190, 331, 81))
font = QtGui.QFont()
font.setFamily("Verdana")
font.setPointSize(10)
self.mastera.setFont(font)
self.mastera.setObjectName("mastera")
self.tableWidget = QtWidgets.QTableWidget(self.centralwidget)
self.tableWidget.setGeometry(QtCore.QRect(330, 0, 521, 781))
self.tableWidget.setObjectName("tableWidget")
self.tableWidget.setColumnCount(5)
self.tableWidget.setRowCount(25)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(0, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(1, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(2, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(3, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(4, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(5, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(6, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(7, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(8, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(9, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(10, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(11, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(12, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(13, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(14, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(15, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(16, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(17, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(18, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(19, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(20, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(21, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(22, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(23, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(24, item)
item = QtWidgets.QTableWidgetItem()
item.setTextAlignment(QtCore.Qt.AlignCenter)
font = QtGui.QFont()
font.setStyleStrategy(QtGui.QFont.PreferAntialias)
item.setFont(font)
self.tableWidget.setHorizontalHeaderItem(0, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(1, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(2, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(3, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(4, item)
item = QtWidgets.QTableWidgetItem()
item.setTextAlignment(QtCore.Qt.AlignCenter)
self.tableWidget.setItem(0, 0, item)
item = QtWidgets.QTableWidgetItem()
item.setTextAlignment(QtCore.Qt.AlignCenter)
self.tableWidget.setItem(1, 0, item)
item = QtWidgets.QTableWidgetItem()
item.setTextAlignment(QtCore.Qt.AlignCenter)
self.tableWidget.setItem(2, 0, item)
item = QtWidgets.QTableWidgetItem()
item.setTextAlignment(QtCore.Qt.AlignCenter)
self.tableWidget.setItem(3, 0, item)
item = QtWidgets.QTableWidgetItem()
item.setTextAlignment(QtCore.Qt.AlignCenter)
self.tableWidget.setItem(4, 0, item)
item = QtWidgets.QTableWidgetItem()
item.setTextAlignment(QtCore.Qt.AlignCenter)
self.tableWidget.setItem(5, 0, item)
item = QtWidgets.QTableWidgetItem()
item.setTextAlignment(QtCore.Qt.AlignCenter)
self.tableWidget.setItem(6, 0, item)
item = QtWidgets.QTableWidgetItem()
item.setTextAlignment(QtCore.Qt.AlignCenter)
self.tableWidget.setItem(7, 0, item)
item = QtWidgets.QTableWidgetItem()
item.setTextAlignment(QtCore.Qt.AlignCenter)
self.tableWidget.setItem(8, 0, item)
item = QtWidgets.QTableWidgetItem()
item.setTextAlignment(QtCore.Qt.AlignCenter)
self.tableWidget.setItem(9, 0, item)
item = QtWidgets.QTableWidgetItem()
item.setTextAlignment(QtCore.Qt.AlignCenter)
self.tableWidget.setItem(10, 0, item)
item = QtWidgets.QTableWidgetItem()
item.setTextAlignment(QtCore.Qt.AlignCenter)
self.tableWidget.setItem(11, 0, item)
item = QtWidgets.QTableWidgetItem()
item.setTextAlignment(QtCore.Qt.AlignCenter)
self.tableWidget.setItem(12, 0, item)
item = QtWidgets.QTableWidgetItem()
item.setTextAlignment(QtCore.Qt.AlignCenter)
self.tableWidget.setItem(13, 0, item)
item = QtWidgets.QTableWidgetItem()
item.setTextAlignment(QtCore.Qt.AlignCenter)
self.tableWidget.setItem(14, 0, item)
item = QtWidgets.QTableWidgetItem()
item.setTextAlignment(QtCore.Qt.AlignCenter)
self.tableWidget.setItem(15, 0, item)
item = QtWidgets.QTableWidgetItem()
item.setTextAlignment(QtCore.Qt.AlignCenter)
self.tableWidget.setItem(16, 0, item)
item = QtWidgets.QTableWidgetItem()
item.setTextAlignment(QtCore.Qt.AlignCenter)
self.tableWidget.setItem(17, 0, item)
item = QtWidgets.QTableWidgetItem()
item.setTextAlignment(QtCore.Qt.AlignCenter)
self.tableWidget.setItem(18, 0, item)
item = QtWidgets.QTableWidgetItem()
item.setTextAlignment(QtCore.Qt.AlignCenter)
self.tableWidget.setItem(19, 0, item)
item = QtWidgets.QTableWidgetItem()
item.setTextAlignment(QtCore.Qt.AlignCenter)
self.tableWidget.setItem(20, 0, item)
item = QtWidgets.QTableWidgetItem()
item.setTextAlignment(QtCore.Qt.AlignCenter)
self.tableWidget.setItem(21, 0, item)
item = QtWidgets.QTableWidgetItem()
item.setTextAlignment(QtCore.Qt.AlignCenter)
self.tableWidget.setItem(22, 0, item)
item = QtWidgets.QTableWidgetItem()
item.setTextAlignment(QtCore.Qt.AlignCenter)
self.tableWidget.setItem(23, 0, item)
item = QtWidgets.QTableWidgetItem()
item.setTextAlignment(QtCore.Qt.AlignCenter)
self.tableWidget.setItem(24, 0, item)
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "Главная"))
self.addclient.setText(_translate("MainWindow", "Записать клиента"))
self.uslugi.setText(_translate("MainWindow", "Список услуг"))
self.mastera.setText(_translate("MainWindow", "Список мастеров"))
item = self.tableWidget.horizontalHeaderItem(0)
item.setText(_translate("MainWindow", "Время"))
item = self.tableWidget.horizontalHeaderItem(1)
item.setText(_translate("MainWindow", "Мастер"))
item = self.tableWidget.horizontalHeaderItem(2)
item.setText(_translate("MainWindow", "Услуга"))
item = self.tableWidget.horizontalHeaderItem(3)
item.setText(_translate("MainWindow", "Имя "))
item = self.tableWidget.horizontalHeaderItem(4)
item.setText(_translate("MainWindow", "Номер"))
__sortingEnabled = self.tableWidget.isSortingEnabled()
self.tableWidget.setSortingEnabled(False)
item = self.tableWidget.item(0, 0)
item.setText(_translate("MainWindow", "8:00"))
item = self.tableWidget.item(1, 0)
item.setText(_translate("MainWindow", "8:30"))
item = self.tableWidget.item(2, 0)
item.setText(_translate("MainWindow", "9:00"))
item = self.tableWidget.item(3, 0)
item.setText(_translate("MainWindow", "9:30"))
item = self.tableWidget.item(4, 0)
item.setText(_translate("MainWindow", "10:00"))
item = self.tableWidget.item(5, 0)
item.setText(_translate("MainWindow", "10:30"))
item = self.tableWidget.item(6, 0)
item.setText(_translate("MainWindow", "11:00"))
item = self.tableWidget.item(7, 0)
item.setText(_translate("MainWindow", "11:30"))
item = self.tableWidget.item(8, 0)
item.setText(_translate("MainWindow", "12:00"))
item = self.tableWidget.item(9, 0)
item.setText(_translate("MainWindow", "12:30"))
item = self.tableWidget.item(10, 0)
item.setText(_translate("MainWindow", "13:00"))
item = self.tableWidget.item(11, 0)
item.setText(_translate("MainWindow", "13:30"))
item = self.tableWidget.item(12, 0)
item.setText(_translate("MainWindow", "14:00"))
item = self.tableWidget.item(13, 0)
item.setText(_translate("MainWindow", "14:30"))
item = self.tableWidget.item(14, 0)
item.setText(_translate("MainWindow", "15:00"))
item = self.tableWidget.item(15, 0)
item.setText(_translate("MainWindow", "15:30"))
item = self.tableWidget.item(16, 0)
item.setText(_translate("MainWindow", "16:00"))
item = self.tableWidget.item(17, 0)
item.setText(_translate("MainWindow", "16:30"))
item = self.tableWidget.item(18, 0)
item.setText(_translate("MainWindow", "17:00"))
item = self.tableWidget.item(19, 0)
item.setText(_translate("MainWindow", "17:30"))
item = self.tableWidget.item(20, 0)
item.setText(_translate("MainWindow", "18:00"))
item = self.tableWidget.item(21, 0)
item.setText(_translate("MainWindow", "18:30"))
item = self.tableWidget.item(22, 0)
item.setText(_translate("MainWindow", "19:00"))
item = self.tableWidget.item(23, 0)
item.setText(_translate("MainWindow", "19:30"))
item = self.tableWidget.item(24, 0)
item.setText(_translate("MainWindow", "20:00"))
self.tableWidget.setSortingEnabled(__sortingEnabled)
Modifying the code of the design is not recommended if you are a beginner, so it is best to create new classes that implement the logic and use the design, these will be Dialog and MainWindow.
Dialog:
To handle some tasks easily we will connect the addClient and cancel buttons to the accept and reject slots so that we can use the dialog in a non-modal way and use exec_() that returns one code if it is accepted and another if it is canceled. a method must be created that collects input information.
MainWindow:
the clicked Record Client signal is connected to a slot where the dialog is displayed and the data of the dialog is collected. a logic is created to get the correct row and add the elements.
class Dialog(QDialog, Ui_Dialog):
def __init__(self, parent=None):
QDialog.__init__(self, parent)
self.setupUi(self)
# cancel
self.pushButton_2.clicked.connect(self.reject)
# accept
self.pushButton.clicked.connect(self.accept)
self.dateTimeEdit.setDisplayFormat("M/d/yy hh:mm")
def data(self):
master = self.comboBox_2.currentText()
service = self.comboBox.currentText()
name = self.lineEdit_2.text()
contact_number = self.lineEdit.text()
time = self.dateTimeEdit.time()
return master, service, name, contact_number, time
class MainWindow(QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
QMainWindow.__init__(self, parent)
self.setupUi(self)
self.addclient.clicked.connect(self.onAddClient)
def onAddClient(self):
dialog = Dialog(self)
if dialog.exec_() == QDialog.Accepted:
master, service, name, contact_number, time = dialog.data()
row = int(QTime(8, 0).secsTo(time)/(30*60))
if row >= 0:
for k, text in enumerate([master, service, name, contact_number]):
self.tableWidget.setItem(row, k+1, QTableWidgetItem(text))
if __name__ == '__main__':
app = QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
Output:
Related
I want to create the basic look of my GUI with Qt-Designer, and add the functions via coding afterwards. Maybe there is already the Problem, but let's go on further.
My GUI that i created via Qt desinger consists of a 4x4 Table and a button.
When the Button is pressed, i want to write something in the cells of the Table.
(Sure, this in particular would be possible without using Qt desinger. But for the future there iwant to creat some more difficult GUIs. And therefore the Qt designer does save quite some time for a newbie like me :) )
The GUI code is:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(686, 539)
self.widget = QtWidgets.QWidget(Form)
self.widget.setGeometry(QtCore.QRect(10, 60, 258, 223))
self.widget.setObjectName("widget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.widget)
self.verticalLayout.setContentsMargins(0, 0, 0, 0)
self.verticalLayout.setObjectName("verticalLayout")
self.tableWidget = QtWidgets.QTableWidget(self.widget)
self.tableWidget.setObjectName("tableWidget")
self.tableWidget.setColumnCount(4)
self.tableWidget.setRowCount(4)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(0, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(1, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(2, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(3, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(0, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(1, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(2, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(3, item)
self.verticalLayout.addWidget(self.tableWidget)
self.pushButton = QtWidgets.QPushButton(self.widget)
self.pushButton.setObjectName("pushButton")
self.verticalLayout.addWidget(self.pushButton)
self.pushButton.clicked.connect(say_hello)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
item = self.tableWidget.verticalHeaderItem(0)
item.setText(_translate("Form", "1"))
item = self.tableWidget.verticalHeaderItem(1)
item.setText(_translate("Form", "2"))
item = self.tableWidget.verticalHeaderItem(2)
item.setText(_translate("Form", "3"))
item = self.tableWidget.verticalHeaderItem(3)
item.setText(_translate("Form", "4"))
item = self.tableWidget.horizontalHeaderItem(0)
item.setText(_translate("Form", "A"))
item = self.tableWidget.horizontalHeaderItem(1)
item.setText(_translate("Form", "B"))
item = self.tableWidget.horizontalHeaderItem(2)
item.setText(_translate("Form", "C"))
item = self.tableWidget.horizontalHeaderItem(3)
item.setText(_translate("Form", "D"))
testitem=QtWidgets.QTableWidgetItem()
self.tableWidget.setItem(1, 0, testitem)
self.pushButton.setText(_translate("Form", "PushButton"))
self.pushButton.clicked.connect(say_hello(testitem))
def say_hello(testitem):
print("Hello you!")
testitem.setText("Hi")
the GUI gets called from another file like that:
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication
import sys
import GUI_PyQtn
class ExampleApp(QtWidgets.QWidget, GUI_PyQtn.Ui_Form):
def __init__(self, parent=None):
super(ExampleApp, self).__init__(parent)
self.setupUi(self)
def main():
app = QApplication(sys.argv)
form = ExampleApp()
form.show()
app.exec_()
if __name__ == '__main__':
main()
My thoughts on my approach were, creating the Cell object and pass it to the function "say_hello". If the function would be inside the Ui_Form Class, it acutally called it not even once.
I believe you can get what you're looking for with the following tweaks.
Declare testitem with self in class Ui_Form
Declare say_hello in class Ui_Form
Pass testitem in say_hello by lambda
No use click.connect() in retranslateUi
that's it
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(686, 539)
self.widget = QtWidgets.QWidget(Form)
self.widget.setGeometry(QtCore.QRect(10, 60, 258, 223))
self.widget.setObjectName("widget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.widget)
self.verticalLayout.setContentsMargins(0, 0, 0, 0)
self.verticalLayout.setObjectName("verticalLayout")
self.tableWidget = QtWidgets.QTableWidget(self.widget)
self.tableWidget.setObjectName("tableWidget")
self.tableWidget.setColumnCount(4)
self.tableWidget.setRowCount(4)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(0, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(1, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(2, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(3, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(0, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(1, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(2, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(3, item)
self.verticalLayout.addWidget(self.tableWidget)
self.pushButton = QtWidgets.QPushButton(self.widget)
self.pushButton.setObjectName("pushButton")
self.verticalLayout.addWidget(self.pushButton)
# declare testitem with self ih here
self.testitem=QtWidgets.QTableWidgetItem()
# pass testitem in say_hello
self.pushButton.clicked.connect(lambda: self.say_hello(self.testitem))
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
item = self.tableWidget.verticalHeaderItem(0)
item.setText(_translate("Form", "1"))
item = self.tableWidget.verticalHeaderItem(1)
item.setText(_translate("Form", "2"))
item = self.tableWidget.verticalHeaderItem(2)
item.setText(_translate("Form", "3"))
item = self.tableWidget.verticalHeaderItem(3)
item.setText(_translate("Form", "4"))
item = self.tableWidget.horizontalHeaderItem(0)
item.setText(_translate("Form", "A"))
item = self.tableWidget.horizontalHeaderItem(1)
item.setText(_translate("Form", "B"))
item = self.tableWidget.horizontalHeaderItem(2)
item.setText(_translate("Form", "C"))
item = self.tableWidget.horizontalHeaderItem(3)
item.setText(_translate("Form", "D"))
#self.testitem=QtWidgets.QTableWidgetItem()
self.tableWidget.setItem(1, 0, self.testitem)
#self.pushButton.setText(_translate("Form", "PushButton"))
#self.pushButton.clicked.connect(self.say_hello(testitem))
# say_hello in class
def say_hello(self, testitem):
testitem.setText('Hi')
I'm setting a filtering tool in Python 3.7 and I'm using pyqt5. I've created a qtablewidget to store the filters and their complementaries chosen by the user. I'd like to allow the user to combine filters over a data so I added grouped checkboxes in each row to select wanted filters.
Which commands should I use to loop over my qtable to get which checkbox is selected please?
def bindApply(self):
checked_list = []
for i in range(self.tableWidget.rowCount()):
#print(self.tableWidget.rowCount())
if self.tableWidget.item(i, 1).checkState() == QtCore.Qt.Checked:
checked_list.append([i,1])
elif self.tableWidget.item(i, 2).checkState() == QtCore.Qt.Checked:
checked_list.append([i,2])
else:
pass
return(checked_list)
I expect a list containing the indexes of selected rows and columns but my function returns nothing.
Here is a reproducible example:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(550, 350)
MainWindow.setMinimumSize(QtCore.QSize(550, 350))
MainWindow.setMaximumSize(QtCore.QSize(550, 350))
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.tableWidget = QtWidgets.QTableWidget(self.centralwidget)
self.tableWidget.setGeometry(QtCore.QRect(20, 20, 500, 200))
self.tableWidget.setObjectName("tableWidget")
self.tableWidget.setColumnCount(3)
self.tableWidget.setRowCount(0)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(0, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(1, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(2, item)
self.pushButton_Save = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_Save.setGeometry(QtCore.QRect(120, 250, 100, 50))
self.pushButton_Save.setMinimumSize(QtCore.QSize(100, 50))
self.pushButton_Save.setMaximumSize(QtCore.QSize(100, 50))
self.pushButton_Save.setObjectName("pushButton_Save")
self.pushButton_Apply = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_Apply.setGeometry(QtCore.QRect(260, 250, 100, 50))
self.pushButton_Apply.setMinimumSize(QtCore.QSize(100, 50))
self.pushButton_Apply.setMaximumSize(QtCore.QSize(100, 50))
self.pushButton_Apply.setObjectName("pushButton_Apply")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 550, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
item = self.tableWidget.horizontalHeaderItem(0)
item.setText(_translate("MainWindow", "Name"))
item = self.tableWidget.horizontalHeaderItem(1)
item.setText(_translate("MainWindow", "Filter"))
item = self.tableWidget.horizontalHeaderItem(2)
item.setText(_translate("MainWindow", "complementary"))
self.pushButton_Save.setText(_translate("MainWindow", "Save"))
self.pushButton_Apply.setText(_translate("MainWindow", "Apply"))
#============================ BINDING ===============================
self.pushButton_Save.clicked.connect(self.bindSave)
self.pushButton_Apply.clicked.connect(self.bindApply)
def bindSave(self):
numRows = self.tableWidget.rowCount()
self.tableWidget.insertRow(numRows)
groupButton = QtWidgets.QButtonGroup(self.tableWidget)
groupButton.setExclusive(True)
it1 = QtWidgets.QTableWidgetItem("filter "+str(numRows))
self.tableWidget.setItem(numRows, 0, it1)
ch_bx1 = QtWidgets.QCheckBox()
groupButton.addButton(ch_bx1)
self.tableWidget.setCellWidget(numRows, 1, ch_bx1)
ch_bx2 = QtWidgets.QCheckBox()
groupButton.addButton(ch_bx2)
self.tableWidget.setCellWidget(numRows, 2, ch_bx2)
def bindApply(self):
checked_list = []
for i in range(self.tableWidget.rowCount()):
#print(self.tableWidget.rowCount())
if self.tableWidget.item(i, 1).checkState() == QtCore.Qt.Checked:
checked_list.append([i,1])
elif self.tableWidget.item(i, 2).checkState() == QtCore.Qt.Checked:
checked_list.append([i,2])
else:
pass
print(checked_list)
if __name__ == "__main__":
import sys
if not QtWidgets.QApplication.instance():
app = QtWidgets.QApplication(sys.argv)
else:
app = QtWidgets.QApplication.instance()
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
You should check the state of the check boxes assigned to the cells, not the state of the item associated with the cell, i.e.
def bindApply(self):
checked_list = []
for i in range(self.tableWidget.rowCount()):
#print(self.tableWidget.rowCount())
if self.tableWidget.cellWidget(i, 1).isChecked():
checked_list.append([i,1])
elif self.tableWidget.cellWidget(i, 2).isChecked():
checked_list.append([i,2])
else:
pass
print(checked_list)
I've got some code that checks which checkboxes a user clicks. Those are then put into an array (aylm). Once the user clicks the book button, a small confirmation window pops up which says are you sure you want to book these rooms. In this confirmation window, I would like the label to say are you sure you want to book (aylm)? so that all the rooms and times selected can be seen by the user. I am not sure how to go about this since I'm using one main window which connects all the smaller windows together, which you can see below in the code.
class Ui_DialogBook(QtWidgets.QDialog, Ui_DialogBook):
def __init__(self, parent=None):
super(Ui_DialogBook, self).__init__(parent)
self.setupUi(self)
class Ui_ViewAllRooms(QtWidgets.QMainWindow, Ui_ViewAllRooms):
def __init__(self, parent=None):
super(Ui_ViewAllRooms, self).__init__(parent)
self.setupUi(self)
self.dialogBook = Ui_DialogBook()
self.book_Button.clicked.connect(self.dialogBook.show)
self.book_Button.clicked.connect(self.book_clicked)
#QtCore.pyqtSlot()
def book_clicked(self):
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:
items.append(item)
for it in items:
r = it.row()
c = it.column()
v, h = self.tableWidget.horizontalHeaderItem(c).text(), self.tableWidget.verticalHeaderItem(r).text()
self._checked_items.append([h, v])
aylm = (self.checked_items())
aylm_len = len(aylm)
for nm in range (0, aylm_len):
print (aylm[nm])
def checked_items(self):
return self._checked_items
The auto generated code from PyQt designer for the Ui_DialogBook is below:
class Ui_DialogBook(object):
def setupUi(self, DialogBook):
DialogBook.setObjectName("DialogBook")
DialogBook.setMaximumSize(QtCore.QSize(200, 150))
self.invalidLogin_label = QtWidgets.QLabel(DialogBook)
self.invalidLogin_label.setGeometry(QtCore.QRect(10, 10, 181, 71))
self.invalidLogin_label.setAlignment(QtCore.Qt.AlignCenter)
self.invalidLogin_label.setObjectName("invalidLogin_label")
self.no_button = QtWidgets.QPushButton(DialogBook)
self.no_button.setGeometry(QtCore.QRect(110, 100, 75, 23))
self.no_button.setObjectName("no_button")
self.yes_button = QtWidgets.QPushButton(DialogBook)
self.yes_button.setGeometry(QtCore.QRect(20, 100, 75, 23))
self.yes_button.setObjectName("yes_button")
self.retranslateUi(DialogBook)
QtCore.QMetaObject.connectSlotsByName(DialogBook)
def retranslateUi(self, DialogBook):
_translate = QtCore.QCoreApplication.translate
DialogBook.setWindowTitle(_translate("DialogBook", "Dialog"))
self.invalidLogin_label.setText(_translate("DialogBook", "Are you sure you would like \n to book:\n"))
self.no_button.setText(_translate("DialogBook", "No"))
self.yes_button.setText(_translate("DialogBook", "Yes"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
DialogBook = QtWidgets.QDialog()
ui = Ui_DialogBook()
ui.setupUi(DialogBook)
DialogBook.show()
sys.exit(app.exec_())
Ui_ViewAllRooms
from PyQt5.QtCore import QDate, QTime, QDateTime, Qt
class Ui_ViewAllRooms(object):
def setupUi(self, ViewAllRooms):
ViewAllRooms.setObjectName("ViewAllRooms")
ViewAllRooms.resize(671, 347)
ViewAllRooms.setMinimumSize(QtCore.QSize(671, 347))
ViewAllRooms.setMaximumSize(QtCore.QSize(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.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.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(10, 290, 231, 41))
self.label.setObjectName("label")
self.book_Button = QtWidgets.QPushButton(self.centralwidget)
self.book_Button.setGeometry(QtCore.QRect(300, 290, 75, 23))
self.book_Button.setObjectName("book_Button")
self.backButton = QtWidgets.QPushButton(self.centralwidget)
self.backButton.setGeometry(QtCore.QRect(590, 10, 75, 23))
self.backButton.setObjectName("backButton")
self.login_title = QtWidgets.QLabel(self.centralwidget)
self.login_title.setGeometry(QtCore.QRect(220, 10, 231, 38))
font = QtGui.QFont()
font.setFamily("Gloucester MT Extra Condensed")
font.setPointSize(25)
font.setBold(False)
font.setItalic(False)
font.setUnderline(True)
font.setWeight(50)
self.login_title.setFont(font)
self.login_title.setLayoutDirection(QtCore.Qt.LeftToRight)
self.login_title.setAutoFillBackground(False)
self.login_title.setAlignment(QtCore.Qt.AlignCenter)
self.login_title.setObjectName("login_title")
self.date_label = QtWidgets.QLabel(self.centralwidget)
self.date_label.setGeometry(QtCore.QRect(240, 70, 191, 21))
self.date_label.setAlignment(QtCore.Qt.AlignCenter)
self.date_label.setObjectName("date_label")
self.nextDayButton = QtWidgets.QPushButton(self.centralwidget)
self.nextDayButton.setGeometry(QtCore.QRect(590, 70, 75, 23))
self.nextDayButton.setObjectName("nextDayButton")
ViewAllRooms.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(ViewAllRooms)
self.statusbar.setObjectName("statusbar")
ViewAllRooms.setStatusBar(self.statusbar)
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.setToolTip("Hall")
item = self.tableWidget.verticalHeaderItem(1)
item.setText(_translate("ViewAllRooms", "C214"))
item.setToolTip("Study Space")
item = self.tableWidget.verticalHeaderItem(2)
item.setText(_translate("ViewAllRooms", "C320"))
item.setToolTip("Computer")
item = self.tableWidget.verticalHeaderItem(3)
item.setText(_translate("ViewAllRooms", "F04"))
item.setToolTip("Mac Room")
item = self.tableWidget.verticalHeaderItem(4)
item.setText(_translate("ViewAllRooms", "E201"))
item.setToolTip("Computer")
item = self.tableWidget.horizontalHeaderItem(0)
item.setText(_translate("ViewAllRooms", "P1"))
item.setToolTip("8:55")
item = self.tableWidget.horizontalHeaderItem(1)
item.setText(_translate("ViewAllRooms", "P2"))
item.setToolTip("9:55")
item = self.tableWidget.horizontalHeaderItem(2)
item.setText(_translate("ViewAllRooms", "P3"))
item.setToolTip("11:15")
item = self.tableWidget.horizontalHeaderItem(3)
item.setText(_translate("ViewAllRooms", "P4"))
item.setToolTip("12:15")
item = self.tableWidget.horizontalHeaderItem(4)
item.setText(_translate("ViewAllRooms", "P5"))
item.setToolTip("2:00")
item = self.tableWidget.horizontalHeaderItem(5)
item.setText(_translate("ViewAllRooms", "P6"))
item.setToolTip("3:15")
__sortingEnabled = self.tableWidget.isSortingEnabled()
self.tableWidget.setSortingEnabled(False)
self.tableWidget.setSortingEnabled(__sortingEnabled)
self.book_Button.setText(_translate("ViewAllRooms", "Book"))
self.backButton.setText(_translate("ViewAllRooms", "Back"))
self.login_title.setText(_translate("ViewAllRooms", "VIEW ALL ROOMS"))
self.date_label.setText(_translate("ViewAllRooms", "Date:14 January 2019"))
self.label.setText(_translate("ViewAllRooms", "Hover over a room number or period for\n"
"extra information"))
self.nextDayButton.setText(_translate("ViewAllRooms", "Next Day"))
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_())
View All Rooms GUI
Try it :
from PyQt5 import QtWidgets, QtGui, QtCore
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class Ui_DialogBook(object):
def setupUi(self, DialogBook):
DialogBook.setObjectName("DialogBook")
DialogBook.setMaximumSize(QtCore.QSize(200, 150))
self.invalidLogin_label = QtWidgets.QLabel(DialogBook)
self.invalidLogin_label.setGeometry(QtCore.QRect(10, 10, 181, 71))
self.invalidLogin_label.setAlignment(QtCore.Qt.AlignCenter)
self.invalidLogin_label.setObjectName("invalidLogin_label")
self.no_button = QtWidgets.QPushButton(DialogBook)
self.no_button.setGeometry(QtCore.QRect(110, 100, 75, 23))
self.no_button.setObjectName("no_button")
self.yes_button = QtWidgets.QPushButton(DialogBook)
self.yes_button.setGeometry(QtCore.QRect(20, 100, 75, 23))
self.yes_button.setObjectName("yes_button")
self.retranslateUi(DialogBook)
QtCore.QMetaObject.connectSlotsByName(DialogBook)
def retranslateUi(self, DialogBook):
_translate = QtCore.QCoreApplication.translate
DialogBook.setWindowTitle(_translate("DialogBook", "Dialog"))
self.invalidLogin_label.setText(_translate("DialogBook", "Are you sure you would like \n to book:\n"))
self.no_button.setText(_translate("DialogBook", "No"))
self.yes_button.setText(_translate("DialogBook", "Yes"))
class DialogBook(QtWidgets.QDialog, Ui_DialogBook):
def __init__(self, parent=None):
super(DialogBook, self).__init__(parent)
self.setupUi(self)
self.listWidget = QListWidget()
self.grid = QGridLayout(self)
self.grid.addWidget(self.invalidLogin_label, 0, 0, 1, 2)
self.grid.addWidget(self.listWidget, 1, 0, 1, 2)
self.grid.addWidget(self.yes_button, 2, 0)
self.grid.addWidget(self.no_button, 2, 1)
class Ui_ViewAllRooms(object):
def setupUi(self, ViewAllRooms):
ViewAllRooms.setObjectName("ViewAllRooms")
ViewAllRooms.resize(671, 347)
ViewAllRooms.setMinimumSize(QtCore.QSize(671, 347))
ViewAllRooms.setMaximumSize(QtCore.QSize(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.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.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(10, 290, 231, 41))
self.label.setObjectName("label")
self.book_Button = QtWidgets.QPushButton(self.centralwidget)
self.book_Button.setGeometry(QtCore.QRect(300, 290, 75, 23))
self.book_Button.setObjectName("book_Button")
self.backButton = QtWidgets.QPushButton(self.centralwidget)
self.backButton.setGeometry(QtCore.QRect(590, 10, 75, 23))
self.backButton.setObjectName("backButton")
self.login_title = QtWidgets.QLabel(self.centralwidget)
self.login_title.setGeometry(QtCore.QRect(220, 10, 231, 38))
font = QtGui.QFont()
font.setFamily("Gloucester MT Extra Condensed")
font.setPointSize(25)
font.setBold(False)
font.setItalic(False)
font.setUnderline(True)
font.setWeight(50)
self.login_title.setFont(font)
self.login_title.setLayoutDirection(QtCore.Qt.LeftToRight)
self.login_title.setAutoFillBackground(False)
self.login_title.setAlignment(QtCore.Qt.AlignCenter)
self.login_title.setObjectName("login_title")
self.date_label = QtWidgets.QLabel(self.centralwidget)
self.date_label.setGeometry(QtCore.QRect(240, 70, 191, 21))
self.date_label.setAlignment(QtCore.Qt.AlignCenter)
self.date_label.setObjectName("date_label")
self.nextDayButton = QtWidgets.QPushButton(self.centralwidget)
self.nextDayButton.setGeometry(QtCore.QRect(590, 70, 75, 23))
self.nextDayButton.setObjectName("nextDayButton")
ViewAllRooms.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(ViewAllRooms)
self.statusbar.setObjectName("statusbar")
ViewAllRooms.setStatusBar(self.statusbar)
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.setToolTip("Hall")
item = self.tableWidget.verticalHeaderItem(1)
item.setText(_translate("ViewAllRooms", "C214"))
item.setToolTip("Study Space")
item = self.tableWidget.verticalHeaderItem(2)
item.setText(_translate("ViewAllRooms", "C320"))
item.setToolTip("Computer")
item = self.tableWidget.verticalHeaderItem(3)
item.setText(_translate("ViewAllRooms", "F04"))
item.setToolTip("Mac Room")
item = self.tableWidget.verticalHeaderItem(4)
item.setText(_translate("ViewAllRooms", "E201"))
item.setToolTip("Computer")
item = self.tableWidget.horizontalHeaderItem(0)
item.setText(_translate("ViewAllRooms", "P1"))
item.setToolTip("8:55")
item = self.tableWidget.horizontalHeaderItem(1)
item.setText(_translate("ViewAllRooms", "P2"))
item.setToolTip("9:55")
item = self.tableWidget.horizontalHeaderItem(2)
item.setText(_translate("ViewAllRooms", "P3"))
item.setToolTip("11:15")
item = self.tableWidget.horizontalHeaderItem(3)
item.setText(_translate("ViewAllRooms", "P4"))
item.setToolTip("12:15")
item = self.tableWidget.horizontalHeaderItem(4)
item.setText(_translate("ViewAllRooms", "P5"))
item.setToolTip("2:00")
item = self.tableWidget.horizontalHeaderItem(5)
item.setText(_translate("ViewAllRooms", "P6"))
item.setToolTip("3:15")
__sortingEnabled = self.tableWidget.isSortingEnabled()
self.tableWidget.setSortingEnabled(False)
self.tableWidget.setSortingEnabled(__sortingEnabled)
self.book_Button.setText(_translate("ViewAllRooms", "Book"))
self.backButton.setText(_translate("ViewAllRooms", "Back"))
self.login_title.setText(_translate("ViewAllRooms", "VIEW ALL ROOMS"))
self.date_label.setText(_translate("ViewAllRooms", "Date:14 January 2019"))
self.label.setText(_translate("ViewAllRooms", "Hover over a room number or period for\n"
"extra information"))
self.nextDayButton.setText(_translate("ViewAllRooms", "Next Day"))
class ViewAllRooms(QtWidgets.QMainWindow, Ui_ViewAllRooms):
def __init__(self, parent=None):
super(ViewAllRooms, self).__init__(parent)
self.setupUi(self)
self.dialogBook = DialogBook() #Ui_DialogBook() #
# self.book_Button.clicked.connect(self.dialogBook.show)
self.book_Button.clicked.connect(self.book_clicked)
#QtCore.pyqtSlot()
def book_clicked(self):
self._checked_items = []
# 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:
# items.append(item)
# r = it.row()
# c = it.column()
v = self.tableWidget.horizontalHeaderItem(j).text()
h = self.tableWidget.verticalHeaderItem(i).text()
self._checked_items.append([h, v, self.tableWidget.item(i, j).text()])
self.dialogBook.listWidget.clear()
self.dialogBook.listWidget.addItems([ "{} {} {}".format(h, v, it) for h, v, it in self._checked_items])
self.dialogBook.show()
"""
print("items->", items)
for it in items:
r = it.row()
c = it.column()
v, h = self.tableWidget.horizontalHeaderItem(c).text(), self.tableWidget.verticalHeaderItem(r).text()
print("item---", self.tableWidget.item(r, c).text())
self._checked_items.append([h, v])
aylm = (self.checked_items())
aylm_len = len(aylm)
for nm in range (0, aylm_len):
print (aylm[nm])
def checked_items(self):
return self._checked_items
"""
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
DialogBook = ViewAllRooms()
# DialogBook = QtWidgets.QDialog()
# ui = DialogBook()
# ui.setupUi(DialogBook)
DialogBook.show()
sys.exit(app.exec_())
How can I get value from dialog button to mainwindow?
How can if the button on my dialog is pressed, give value to table widget on mainwindow?
Here my mainwindow code and dialog code, and code what I want to impact each other.
mainwindow Code
from PyQt5 import QtCore, QtGui, QtWidgets
from ayam_ui import Ui_Dialog
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(330, 0, 121, 41))
font = QtGui.QFont()
font.setFamily("Verdana")
font.setPointSize(20)
self.label.setFont(font)
self.label.setTextFormat(QtCore.Qt.AutoText)
self.label.setObjectName("label")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(30, 100, 75, 23))
self.pushButton.setObjectName("pushButton")
self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_2.setGeometry(QtCore.QRect(120, 100, 75, 23))
self.pushButton_2.setObjectName("pushButton_2")
self.pushButton_3 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_3.setGeometry(QtCore.QRect(210, 100, 75, 23))
self.pushButton_3.setObjectName("pushButton_3")
self.pushButton_4 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_4.setGeometry(QtCore.QRect(300, 100, 75, 23))
self.pushButton_4.setObjectName("pushButton_4")
self.pushButton_5 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_5.setGeometry(QtCore.QRect(390, 100, 75, 23))
self.pushButton_5.setObjectName("pushButton_5")
self.tableWidget = QtWidgets.QTableWidget(self.centralwidget)
self.tableWidget.setGeometry(QtCore.QRect(550, 100, 231, 341))
self.tableWidget.setObjectName("tableWidget")
self.tableWidget.setColumnCount(2)
self.tableWidget.setRowCount(10)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(0, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(1, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(2, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(3, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(4, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(5, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(6, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(7, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(8, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(9, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(0, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(1, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setItem(0, 0, item)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
self.menubar.setObjectName("menubar")
self.menumenu = QtWidgets.QMenu(self.menubar)
self.menumenu.setObjectName("menumenu")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.menubar.addAction(self.menumenu.menuAction())
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
self.pushButton.clicked.connect(self.ayam)
def pesanayam(self):
item = self.tableWidget.item(0, 0)
item.setText("ayam")
def ayam(self):
self.Dialog = QtWidgets.QDialog()
self.ui = Ui_Dialog()
self.ui.setupUi(self.Dialog)
self.Dialog.show()
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "Kantin"))
self.label.setText(_translate("MainWindow", "Makanan"))
self.pushButton.setText(_translate("MainWindow", "Ayam"))
self.pushButton_2.setText(_translate("MainWindow", "Mie"))
self.pushButton_3.setText(_translate("MainWindow", "Sayur"))
self.pushButton_4.setText(_translate("MainWindow", "Daging"))
self.pushButton_5.setText(_translate("MainWindow", "Ikan"))
item = self.tableWidget.verticalHeaderItem(0)
item.setText (_translate("mainwindows", "1"))
item = self.tableWidget.horizontalHeaderItem(0)
item.setText(_translate("MainWindow", "Makanan"))
item = self.tableWidget.horizontalHeaderItem(1)
item.setText(_translate("MainWindow", "jumlah"))
__sortingEnabled = self.tableWidget.isSortingEnabled()
self.tableWidget.setSortingEnabled(False)
self.tableWidget.setSortingEnabled(__sortingEnabled)
self.menumenu.setTitle(_translate("MainWindow", "menu"))
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_())
and this the dialog
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(348, 496)
self.textBrowser = QtWidgets.QTextBrowser(Dialog)
self.textBrowser.setGeometry(QtCore.QRect(0, 280, 201, 201))
self.textBrowser.setObjectName("textBrowser")
self.pesan = QtWidgets.QPushButton(Dialog)
self.pesan.setGeometry(QtCore.QRect(210, 300, 75, 23))
self.pesan.setObjectName("pesan")
self.spinBox = QtWidgets.QSpinBox(Dialog)
self.spinBox.setGeometry(QtCore.QRect(300, 300, 42, 22))
self.spinBox.setObjectName("spinBox")
self.pushButton_2 = QtWidgets.QPushButton(Dialog)
self.pushButton_2.setGeometry(QtCore.QRect(210, 350, 75, 23))
self.pushButton_2.setObjectName("pushButton_2")
self.label = QtWidgets.QLabel(Dialog)
self.label.setGeometry(QtCore.QRect(0, -10, 1351, 291))
self.label.setText("")
self.label.setPixmap(QtGui.QPixmap("E:/python/resto/ayy.jpg"))
self.label.setObjectName("label")
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
self.pushButton_2.clicked.connect(Dialog.close)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
self.textBrowser.setHtml(_translate("Dialog", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'MS Shell Dlg 2\'; font-size:8.25pt; font-weight:400; font-style:normal;\">\n"
"<p align=\"center\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:12pt; font-weight:600; text-decoration: underline;\">AYAM</span></p>\n"
"<p align=\"center\" style=\"-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-size:12pt; font-weight:600; text-decoration: underline;\"><br /></p>\n"
"<p align=\"center\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:12pt; font-weight:600; text-decoration: underline;\">ini adalah ayam yang di goreng dengan minyak</span></p>\n"
"<p align=\"center\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:12pt; font-weight:600; text-decoration: underline;\">enak gurih dan renyah</span></p></body></html>"))
self.pesan.setText(_translate("Dialog", "pesan"))
self.pushButton_2.setText(_translate("Dialog", "kembali"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Dialog = QtWidgets.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())
How can this button on the dialog
self.pesan = QtWidgets.QPushButton(Dialog)
self.pesan.setGeometry(QtCore.QRect(210, 300, 75, 23))
self.pesan.setObjectName("pesan")
impact to this defintion on my main window?
def pesanayam(self):
item = self.tableWidget.item(0, 0)
item.setText("ayam")
Try it:
from PyQt5 import QtCore, QtGui, QtWidgets
from ayam_ui import Ui_Dialog
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(330, 0, 121, 41))
font = QtGui.QFont()
font.setFamily("Verdana")
font.setPointSize(20)
self.label.setFont(font)
self.label.setTextFormat(QtCore.Qt.AutoText)
self.label.setObjectName("label")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(30, 100, 75, 23))
self.pushButton.setObjectName("pushButton")
self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_2.setGeometry(QtCore.QRect(120, 100, 75, 23))
self.pushButton_2.setObjectName("pushButton_2")
self.pushButton_3 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_3.setGeometry(QtCore.QRect(210, 100, 75, 23))
self.pushButton_3.setObjectName("pushButton_3")
self.pushButton_4 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_4.setGeometry(QtCore.QRect(300, 100, 75, 23))
self.pushButton_4.setObjectName("pushButton_4")
self.pushButton_5 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_5.setGeometry(QtCore.QRect(390, 100, 75, 23))
self.pushButton_5.setObjectName("pushButton_5")
self.tableWidget = QtWidgets.QTableWidget(self.centralwidget)
self.tableWidget.setGeometry(QtCore.QRect(550, 100, 231, 341))
self.tableWidget.setObjectName("tableWidget")
self.tableWidget.setColumnCount(2)
self.tableWidget.setRowCount(10)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(0, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(1, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(2, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(3, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(4, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(5, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(6, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(7, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(8, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(9, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(0, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(1, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setItem(0, 0, item)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
self.menubar.setObjectName("menubar")
self.menumenu = QtWidgets.QMenu(self.menubar)
self.menumenu.setObjectName("menumenu")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.menubar.addAction(self.menumenu.menuAction())
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
self.pushButton.clicked.connect(self.ayam)
def pesanayam(self):
item = self.tableWidget.item(0, 0)
item.setText(str(self.ui.spinBox.value())) #("ayam") # +++
self.Dialog.hide() # +++
def ayam(self):
self.Dialog = QtWidgets.QDialog()
self.ui = Ui_Dialog()
self.ui.setupUi(self.Dialog)
self.ui.pesan.clicked.connect(self.pesanayam) # +++
self.Dialog.show()
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "Kantin"))
self.label.setText(_translate("MainWindow", "Makanan"))
self.pushButton.setText(_translate("MainWindow", "Ayam"))
self.pushButton_2.setText(_translate("MainWindow", "Mie"))
self.pushButton_3.setText(_translate("MainWindow", "Sayur"))
self.pushButton_4.setText(_translate("MainWindow", "Daging"))
self.pushButton_5.setText(_translate("MainWindow", "Ikan"))
item = self.tableWidget.verticalHeaderItem(0)
item.setText (_translate("mainwindows", "1"))
item = self.tableWidget.horizontalHeaderItem(0)
item.setText(_translate("MainWindow", "Makanan"))
item = self.tableWidget.horizontalHeaderItem(1)
item.setText(_translate("MainWindow", "jumlah"))
__sortingEnabled = self.tableWidget.isSortingEnabled()
self.tableWidget.setSortingEnabled(False)
self.tableWidget.setSortingEnabled(__sortingEnabled)
self.menumenu.setTitle(_translate("MainWindow", "menu"))
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_())
I'm newbie in PyQt,recently I want to use pyqt to do a data management system for athletes data.I create 50*3 blank Item.But when I delete data and then insert data, the data is not completely displayed on the interface.And when I add data,there is a mistake
a=self.tableWidget.horizontalHeaderItem(col).text()
AttributeError: 'NoneType' object has no attribute 'text'.
I use Qt designer to write UI program
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(841, 690)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.tabWidget = QtGui.QTabWidget(self.centralwidget)
self.tabWidget.setGeometry(QtCore.QRect(90, 60, 661, 401))
self.tabWidget.setObjectName(_fromUtf8("tabWidget"))
self.tab = QtGui.QWidget()
self.tab.setObjectName(_fromUtf8("tab"))
self.lineEdit = QtGui.QLineEdit(self.tab)
self.lineEdit.setGeometry(QtCore.QRect(80, 240, 113, 20))
self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
self.lineEdit_2 = QtGui.QLineEdit(self.tab)
self.lineEdit_2.setGeometry(QtCore.QRect(80, 280, 113, 20))
self.lineEdit_2.setObjectName(_fromUtf8("lineEdit_2"))
self.lineEdit_3 = QtGui.QLineEdit(self.tab)
self.lineEdit_3.setGeometry(QtCore.QRect(80, 320, 113, 20))
self.lineEdit_3.setObjectName(_fromUtf8("lineEdit_3"))
self.label = QtGui.QLabel(self.tab)
self.label.setGeometry(QtCore.QRect(10, 240, 54, 16))
self.label.setObjectName(_fromUtf8("label"))
self.label_2 = QtGui.QLabel(self.tab)
self.label_2.setGeometry(QtCore.QRect(10, 280, 51, 20))
self.label_2.setObjectName(_fromUtf8("label_2"))
self.label_3 = QtGui.QLabel(self.tab)
self.label_3.setGeometry(QtCore.QRect(10, 320, 54, 16))
self.label_3.setObjectName(_fromUtf8("label_3"))
self.pushButton = QtGui.QPushButton(self.tab)
self.pushButton.setGeometry(QtCore.QRect(550, 240, 75, 23))
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.pushButton_2 = QtGui.QPushButton(self.tab)
self.pushButton_2.setGeometry(QtCore.QRect(550, 280, 75, 23))
self.pushButton_2.setObjectName(_fromUtf8("pushButton_2"))
self.pushButton_3 = QtGui.QPushButton(self.tab)
self.pushButton_3.setGeometry(QtCore.QRect(550, 320, 75, 23))
self.pushButton_3.setObjectName(_fromUtf8("pushButton_3"))
self.tableWidget = QtGui.QTableWidget(self.tab)
self.tableWidget.setGeometry(QtCore.QRect(20, 10, 611, 192))
self.tableWidget.setObjectName(_fromUtf8("tableWidget"))
self.tableWidget.setColumnCount(3)
self.tableWidget.setRowCount(50)
item = QtGui.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(0, item)
item = QtGui.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(1, item)
item = QtGui.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(2, item)
item = QtGui.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(0, item)
item = QtGui.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(1, item)
item = QtGui.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(2, item)
item = QtGui.QTableWidgetItem()
self.tableWidget.setItem(0, 0, item)
self.tabWidget.addTab(self.tab, _fromUtf8(""))
self.tab_2 = QtGui.QWidget()
self.tab_2.setObjectName(_fromUtf8("tab_2"))
self.tabWidget.addTab(self.tab_2, _fromUtf8(""))
MainWindow.setCentralWidget(self.centralwidget)
self.QYolk = QtGui.QMenuBar(MainWindow)
self.QYolk.setGeometry(QtCore.QRect(0, 0, 841, 23))
self.QYolk.setObjectName(_fromUtf8("QYolk"))
MainWindow.setMenuBar(self.QYolk)
self.label.setBuddy(self.lineEdit)
self.label_2.setBuddy(self.lineEdit_2)
self.label_3.setBuddy(self.lineEdit_3)
self.retranslateUi(MainWindow)
self.tabWidget.setCurrentIndex(0)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
self.label.setText(_translate("MainWindow", " name0", None))
self.label_2.setText(_translate("MainWindow", "name1", None))
self.label_3.setText(_translate("MainWindow", "name2", None))
self.pushButton.setText(_translate("MainWindow", "add", None))
self.pushButton_2.setText(_translate("MainWindow", "save", None))
self.pushButton_3.setText(_translate("MainWindow", "delete", None))
item = self.tableWidget.verticalHeaderItem(0)
item.setText(_translate("MainWindow", "1", None))
item = self.tableWidget.verticalHeaderItem(1)
item.setText(_translate("MainWindow", "2", None))
item = self.tableWidget.verticalHeaderItem(2)
item.setText(_translate("MainWindow", "3", None))
item = self.tableWidget.horizontalHeaderItem(0)
item.setText(_translate("MainWindow", "name0", None))
item = self.tableWidget.horizontalHeaderItem(1)
item.setText(_translate("MainWindow", "name1", None))
item = self.tableWidget.horizontalHeaderItem(2)
item.setText(_translate("MainWindow", "name2", None))
__sortingEnabled = self.tableWidget.isSortingEnabled()
self.tableWidget.setSortingEnabled(False)
self.tableWidget.setSortingEnabled(__sortingEnabled)
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab), _translate("MainWindow", "player1", None))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_2), _translate("MainWindow", "player2", None))
Here is my back-end program.I use cellchange to get the changed data.Is it a good idea?
from PyQt4 import QtCore, QtGui,QtSql
import sys
class StartQt4(QtGui.QMainWindow,Ui_MainWindow):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.setupUi(self)
self.db = QtSql.QSqlDatabase.addDatabase("QMYSQL")
self.db.setHostName("localhost")
self.db.setDatabaseName("test")
self.db.setUserName("root")
self.db.setPassword("")
self.q=QtSql.QSqlQuery()
if (self.db.open()):
self.showdata()
else:
print "failed"
QtCore.QObject.connect(self.tableWidget, QtCore.SIGNAL("cellChanged(int, int)"), self.changedata)
QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL("clicked()"), self.adddata)
QtCore.QObject.connect(self.pushButton_3, QtCore.SIGNAL("clicked()"), self.deldata)
QtCore.QObject.connect(self.pushButton_2, QtCore.SIGNAL("clicked()"), self.savedata)
def adddata(self):
name0 = str(self.lineEdit.text())
name1 = str(self.lineEdit_2.text())
name2 = str(self.lineEdit_3.text())
self.q.exec_("insert into player values ('%s','%s','%s')" %(name0,name1,name2))
#lastrow=self.tableWidget.rowCount()
#self.tableWidget.insertRow(lastrow)
self.showdata()
def changedata(self,row,col):
co=self.tableWidget.columnCount()
row =self.tableWidget.currentRow()
a= self.tableWidget.selectedItems()
col=self.tableWidget.currentColumn()
a=self.tableWidget.horizontalHeaderItem(col).text()
if (col < (co-1)):
b=self.tableWidget.horizontalHeaderItem(col+1).text()
t=self.tableWidget.item(row, col+1).text()
elif (col == (co-1)):
b=self.tableWidget.horizontalHeaderItem(col-1).text()
t=self.tableWidget.item(row, col-1).text()
dt=self.tableWidget.currentItem().text()
self.q.exec_("update qiu set %s = '%s' where %s = %s " %(a,dt,b,t))
def savedata(self):
self.q.exec_("insert into player1 select distinct * from player")
def deldata(self):
#self.tableWidget.clear()
row=self.tableWidget.currentRow()
print row
a=self.tableWidget.horizontalHeaderItem(0).text()
x=self.tableWidget.item(row,0).text()
self.q.exec_("delete from qiu where %s=%s" %(a,x))
self.showdata()
def showdata(self):
self.q.exec_("select * from qiu")
col=self.tableWidget.columnCount()
for i in range(0,self.tableWidget.rowCount()):
self.q.next()
for j in range(col):
x=self.q.value(j).toString()
self.tableWidget.setItem(i, j, QtGui.QTableWidgetItem(x))
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp = StartQt4()
myapp.show()
sys.exit(app.exec_())
Appreciate very much