I have a program that shows information about products and I wanted to present the date, but when the date appears, this is what it shows: PyQt5.QtCore.QDate(2018, 7, 7)
.
For those who need this code here:
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtSql import QSqlDatabase, QSqlQuery
from PyQt5.QtWidgets import QTableWidgetItem, QTableWidget
from PyQt5.QtCore import QDate, QTime, QDateTime, Qt
from adicionar_produtos import Ui_adicionar
class Ui_Produtos(object):
# Adicionar
def add(self):
self.window = QtWidgets.QMainWindow()
self.ui = Ui_adicionar()
self.ui.setupUi(self.window)
self.window.show()
#MainWindow
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.setMinimumSize(QtCore.QSize(0, 0))
#MainWindow.setMaximumSize(QtCore.QSize(1920,510))
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout_2 = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout_2.setObjectName("gridLayout_2")
self.gridLayout = QtWidgets.QGridLayout()
self.gridLayout.setObjectName("gridLayout")
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
self.btn_add = QtWidgets.QPushButton(self.centralwidget)
self.btn_add.setObjectName("btn_add")
self.horizontalLayout.addWidget(self.btn_add)
#-----------------------------
"""
self.btn_edit = QtWidgets.QPushButton(self.centralwidget)
self.btn_edit.setObjectName("btn_edit")
self.horizontalLayout.addWidget(self.btn_edit)
"""
#---------------------------------------
self.btn_del = QtWidgets.QPushButton(self.centralwidget)
self.btn_del.setObjectName("btn_del")
self.horizontalLayout.addWidget(self.btn_del)
spacerItem = QtWidgets.QSpacerItem(998, 20, QtWidgets.QSizePolicy.Maximum, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout.addItem(spacerItem)
"""
self.btn_stock = QtWidgets.QPushButton(self.centralwidget)
self.btn_stock.setObjectName("btn_stock")
self.horizontalLayout.addWidget(self.btn_stock)
spacerItem1 = QtWidgets.QSpacerItem(150, 20, QtWidgets.QSizePolicy.Maximum, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout.addItem(spacerItem1)
"""
#---------------------------------------------------
"""
self.btn_cat = QtWidgets.QPushButton(self.centralwidget)
self.btn_cat.setObjectName("btn_cat")
self.horizontalLayout.addWidget(self.btn_cat)
"""
#-----------------------------------------------------------
self.gridLayout.addLayout(self.horizontalLayout, 0, 0, 1, 1)
self.tablep = QTableWidget()
self.gridLayout.addWidget(self.tablep)
self.produtos()
header = self.tablep.horizontalHeader()
header.setSectionResizeMode(0, QtWidgets.QHeaderView.ResizeToContents)
header.setSectionResizeMode(1, QtWidgets.QHeaderView.ResizeToContents)
header.setSectionResizeMode(2, QtWidgets.QHeaderView.ResizeToContents)
header.setSectionResizeMode(3, QtWidgets.QHeaderView.ResizeToContents)
header.setSectionResizeMode(4, QtWidgets.QHeaderView.ResizeToContents)
header.setSectionResizeMode(5, QtWidgets.QHeaderView.ResizeToContents)
header.setSectionResizeMode(6, QtWidgets.QHeaderView.ResizeToContents)
header.setSectionResizeMode(7, QtWidgets.QHeaderView.ResizeToContents)
header.setSectionResizeMode(8, QtWidgets.QHeaderView.ResizeToContents)
#-------------------------------------------------------
self.gridLayout_2.addLayout(self.gridLayout, 0, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 783, 21))
self.menubar.setObjectName("menubar")
self.menuFicheirp = QtWidgets.QMenu(self.menubar)
self.menuFicheirp.setObjectName("menuFicheirp")
self.menuExibir = QtWidgets.QMenu(self.menubar)
self.menuExibir.setObjectName("menuExibir")
self.menuAjuda = QtWidgets.QMenu(self.menubar)
self.menuAjuda.setObjectName("menuAjuda")
self.menuSobre = QtWidgets.QMenu(self.menuAjuda)
self.menuSobre.setObjectName("menuSobre")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.actionExportar = QtWidgets.QAction(MainWindow)
self.actionExportar.setObjectName("actionExportar")
self.actionFechar_Janela = QtWidgets.QAction(MainWindow)
self.actionFechar_Janela.setObjectName("actionFechar_Janela")
self.actionSair = QtWidgets.QAction(MainWindow)
self.actionSair.setObjectName("actionSair")
self.actionComo_Utilizar = QtWidgets.QAction(MainWindow)
self.actionComo_Utilizar.setObjectName("actionComo_Utilizar")
self.actionCriador = QtWidgets.QAction(MainWindow)
self.actionCriador.setObjectName("actionCriador")
self.menuFicheirp.addAction(self.actionExportar)
self.menuFicheirp.addSeparator()
self.menuFicheirp.addAction(self.actionFechar_Janela)
self.menuFicheirp.addAction(self.actionSair)
self.menuSobre.addAction(self.actionCriador)
self.menuAjuda.addAction(self.actionComo_Utilizar)
self.menuAjuda.addSeparator()
self.menuAjuda.addAction(self.menuSobre.menuAction())
self.menubar.addAction(self.menuFicheirp.menuAction())
self.menubar.addAction(self.menuExibir.menuAction())
self.menubar.addAction(self.menuAjuda.menuAction())
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
self.tablep.itemChanged.connect(self.editar)
self.btn_add.clicked.connect(self.add)
self.btn_del.clicked.connect(self.eliminar)
#ler produtos
def produtos(self):
db = QSqlDatabase.addDatabase('QMYSQL')
db.setHostName("localhost")
db.setDatabaseName("stock")
db.setUserName("root")
db.setPassword("")
estado = db.open()
print(estado)
if estado == False:
msgBox = QtWidgets.QMessageBox()
msgBox.setIcon(QtWidgets.QMessageBox.Warning)
msgBox.setWindowTitle("Erro")
msgBox.setText(db.lastError().text())
msgBox.setStandardButtons(QtWidgets.QMessageBox.Ok)
msgBox.exec_()
else:
self.tablep.setColumnCount(9)
self.tablep.setHorizontalHeaderLabels(['ID', 'Nome', 'Descrição', 'Marca', 'Nível de Stock', 'Stock Fora?', 'Data de Reabastecimento', 'Preço', 'Validade'])
row = 0
sql = "SELECT ID, Nome, Descrição, marca, stock_nivel, stock_fora, dat_reabast, preco, validade FROM produtos"
query = QSqlQuery(sql)
while query.next():
self.tablep.insertRow(row)
ID = QTableWidgetItem(str(query.value(0)))
nome = QTableWidgetItem(str(query.value(1)))
Descricao = QTableWidgetItem(str(query.value(2)))
Marca = QTableWidgetItem(str(query.value(3)))
Stock_nivel = QTableWidgetItem(str(query.value(4)))
Stock_fora = QTableWidgetItem(str(query.value(5)))
dat_reabas = QTableWidgetItem(str(query.value(6)))
preco = QTableWidgetItem(str(query.value(7)))
Validade = QTableWidgetItem(str(query.value(8)))
self.tablep.setItem(row, 0, ID)
self.tablep.setItem(row, 1, nome)
self.tablep.setItem(row, 2, Descricao)
self.tablep.setItem(row, 3, Marca)
self.tablep.setItem(row, 4, Stock_nivel)
self.tablep.setItem(row, 5, Stock_fora)
self.tablep.setItem(row, 6, dat_reabas)
self.tablep.setItem(row, 7, preco)
self.tablep.setItem(row, 8, Validade)
row = row + 1
db.close()
def eliminar(self):
db = QSqlDatabase.addDatabase('QMYSQL')
db.setHostName("localhost")
db.setDatabaseName("stock")
db.setUserName("root")
db.setPassword("")
estado = db.open()
if estado == False:
msgBox = QtWidgets.QMessageBox()
msgBox.setIcon(QtWidgets.QMessageBox.Warning)
msgBox.setWindowTitle("Erro")
msgBox.setText(db.lastError().text())
msgBox.setStandardButtons(QtWidgets.QMessageBox.Ok)
msgBox.exec_()
else:
rows = self.tablep.selectionModel().selectedRows()
index = []
for i in rows:
index.append(i.row())
index.sort(reverse=True)
for i in index:
ID = self.tablep.item(i, 0).text()
self.tablep.removeRow(i)
sql = "DELETE FROM produtos WHERE ID=:id"
query = QSqlQuery()
query.prepare(sql)
query.bindValue(":id", ID)
estado = query.exec_()
if estado == False:
msgBox = QtWidgets.QMessageBox()
msgBox.setIcon(QtWidgets.QMessageBox.Warning)
msgBox.setWindowTitle("Erro")
msgBox.setText(db.lastError().text())
msgBox.setStandardButtons(QtWidgets.QMessageBox.Discard)
msgBox.exec_()
db.close()
def editar(self):
db = QSqlDatabase.addDatabase('QMYSQL')
db.setHostName("localhost")
db.setDatabaseName("stock")
db.setUserName("root")
db.setPassword("")
estado = db.open()
if estado == False:
msgBox = QtWidgets.QMessageBox()
msgBox.setIcon(QtWidgets.QMessageBox.Warning)
msgBox.setWindowTitle("Erro")
msgBox.setText(db.lastError().text())
msgBox.setStandardButtons(QtWidgets.QMessageBox.Ok)
msgBox.exec_()
else:
column = self.tablep.currentColumn()
row = self.tablep.currentRow()
ID = self.tablep.item(row, 0).text()
value = self.tablep.currentItem().text()
columns = ['ID', 'Nome', 'Descrição', 'Marca', 'Nível de Stock', 'Stock Fora?', 'Data de Reabastecimento', 'Preço', 'Validade']
query = QSqlQuery()
sql = "UPDATE produtos SET " + columns[column] + "=" + ":value WHERE ID=:id"
query.prepare(sql)
query.bindValue(":id", ID)
query.bindValue(":value", value)
estado = query.exec_()
if estado == False:
msgBox = QtWidgets.QMessageBox()
msgBox.setIcon(QtWidgets.QMessageBox.Warning)
msgBox.setWindowTitle("Erro")
msgBox.setText(db.lastError().text())
msgBox.setStandardButtons(QtWidgets.QMessageBox.Discard)
msgBox.exec_()
db.close()
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "Produtos"))
self.btn_add.setText(_translate("MainWindow", "Adicionar"))
#self.btn_edit.setText(_translate("MainWindow", "Editar"))
self.btn_del.setText(_translate("MainWindow", "Eliminar"))
#self.btn_stock.setText(_translate("MainWindow", "Stock"))
#self.btn_cat.setText(_translate("MainWindow", "Categorias"))
self.menuFicheirp.setTitle(_translate("MainWindow", "Ficheiro"))
self.menuExibir.setTitle(_translate("MainWindow", "Exibir"))
self.menuAjuda.setTitle(_translate("MainWindow", "Ajuda"))
self.menuSobre.setTitle(_translate("MainWindow", "Sobre"))
self.actionExportar.setText(_translate("MainWindow", "Exportar"))
self.actionFechar_Janela.setText(_translate("MainWindow", "Fechar Janela"))
self.actionSair.setText(_translate("MainWindow", "Sair"))
self.actionComo_Utilizar.setText(_translate("MainWindow", "Como Utilizar"))
self.actionCriador.setText(_translate("MainWindow", "Criador"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_Produtos()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())