Text Indent doesn't works - qt

The program does not work changing the paragraph indentation in the text. I want the text to always be indented in TextEdit. If do not use other widgets than TextEdit, then everything works.I think that there are problems with layouts, but I can’t understand, because I don’t really understand Python, but this problem needs to be solved. What could be the problem?
PyQt5 import QtCore, QtGui, QtWidgets
import os
from PyQt5.QtWidgets import QTextEdit, QApplication
from PyQt5.QtGui import QTextDocument, QTextCursor, QTextBlockFormat
class TextEditor(QTextEdit):
def __init__(self,parent):
super().__init__(parent)
self.initUI()
def initUI(self):
cursor = QTextCursor(self.document())
cursor.select(QTextCursor.Document)
fmt = QTextBlockFormat()
fmt.setTextIndent(60)
cursor.mergeBlockFormat(fmt)
font= QtGui.QFont()
font.setFamily("Times New Roman")
font.setPointSize(14)
self.setFont(font)
self.show()
def insertFromMimeData(self, md):
cursor = self.textCursor()
fmt = QTextBlockFormat()
fmt.setTextIndent(cursor.blockFormat().textIndent())
cursor.mergeBlockFormat(fmt)
cursor.insertText(md.text())
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.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout.setObjectName("gridLayout")
self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.textEdit = TextEditor(self.centralwidget)
self.textEdit.setObjectName("textEdit")
self.horizontalLayout_2.addWidget(self.textEdit)
self.gridLayout.addLayout(self.horizontalLayout_2, 2, 0, 1, 1)
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
font = QtGui.QFont()
font.setFamily("Times New Roman")
font.setPointSize(10)
font.setBold(True)
font.setWeight(75)
self.pushButton.setFont(font)
self.pushButton.setObjectName("pushButton")
self.horizontalLayout.addWidget(self.pushButton)
self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
font = QtGui.QFont()
font.setFamily("Times New Roman")
font.setPointSize(10)
font.setBold(True)
font.setWeight(75)
self.pushButton_2.setFont(font)
self.pushButton_2.setObjectName("pushButton_2")
self.horizontalLayout.addWidget(self.pushButton_2)
self.gridLayout.addLayout(self.horizontalLayout, 0, 0, 1, 1)
self.label = QtWidgets.QLabel(self.centralwidget)
font = QtGui.QFont()
font.setPointSize(11)
self.label.setFont(font)
self.label.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
self.label.setObjectName("label")
self.gridLayout.addWidget(self.label, 1, 0, 1, 1)
mainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(mainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
self.menubar.setObjectName("menubar")
mainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(mainWindow)
self.statusbar.setObjectName("statusbar")
mainWindow.setStatusBar(self.statusbar)
self.pig()
self.retranslateUi(mainWindow)
QtCore.QMetaObject.connectSlotsByName(mainWindow)
self.butclick()
self.butclick2()
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())

Related

pyQT5 - missing field update when inserting row in a SQLite table using QdataWidget mapper

I have a problem using QDataWidgetMapper and QSQLTableModel to insert a row in a SQLlite table: I have a SQlite table with QSQLTableModel and QDataWidgetMapper set to such model.
I set-up a filter to the model to select only specific rows. The problem is when I insert a new row, only the value of few fields is updated in the SQLite table, while all others entered in the form are not present. Below is the code, along with the .ui file, the screenshot of the SQLite table structure, the screenshot of the input form with data input and the screenshot of the table record after the input. Thanks for any suggestion
import sys
from datetime import datetime
from PyQt5 import QtWidgets, QtCore
from PyQt5.QtCore import QSize, Qt, QModelIndex
from PyQt5.QtSql import QSqlDatabase, QSqlTableModel, QSqlRelation, QSqlQuery, QSqlQueryModel
from PyQt5.QtWidgets import QApplication, QTableView,QWidget,QMessageBox, QMainWindow, QDialog, QMenu, QInputDialog, QLineEdit, QDataWidgetMapper
from connect_SQLITE import Database
from gestione_fase_testUi import Ui_f_gestioneFaseTest
class Fase(QWidget, Ui_f_gestioneFaseTest):
def __init__(self, nome_prev=None, num_prev=None, rev_prev=None, fase=None, ie=None):
super().__init__()
self.dataIns=QtWidgets.QDateTimeEdit()
self.dataMod=QtWidgets.QDateTimeEdit()
self.num_prev='1'
self.rev_prev='1'
self.fase=fase
self.interna_esterna=ie
self.setupUi(self)
self.assegnazione_modello_dati()
self.assegnazione_mapper()
self.pbInsFase.setEnabled(False)
self.pbInsFase.setVisible(False)
self.pbCancFase.setEnabled(False)
self.pbModFase.setEnabled(False)
self.pbNuovaFase.setEnabled(True)
self.pbNuovaFase.clicked.connect(self.nuova_fase)
self.pbInsFase.clicked.connect(self.inserimento_fase)
self.pbModFase.clicked.connect(self.modifica_fase)
self.pbCancFase.clicked.connect(self.cancellazione_fase)
self.pbFirst.clicked.connect(self.mapperFasiDett.toFirst)
self.pbLast.clicked.connect(self.mapperFasiDett.toLast)
self.pbPrevious.clicked.connect(self.mapperFasiDett.toPrevious)
self.pbNext.clicked.connect(self.mapperFasiDett.toNext)
def assegnazione_modello_dati(self):
self.modelFasiDett = QSqlTableModel()
self.modelFasiDett.setTable("tb_FasiTest1")
filtro=""
if not self.num_prev:
self.modelFasiDett.setFilter("")
elif not self.fase:
filtro = "codicePreventivo = "+"'"+self.num_prev+"'" + " AND revisionePreventivo = " + "'" + self.rev_prev + "'"
self.modelFasiDett.setFilter(filtro)
else:
filtro = "codicePreventivo = "+"'"+self.num_prev+"'" + " AND revisionePreventivo = " + "'" + self.rev_prev + "'" + " AND nomeFase = " + "'" + self.fase + "'"
self.modelFasiDett.setFilter(filtro)
def assegnazione_mapper(self):
self.mapperFasiDett = QDataWidgetMapper()
self.mapperFasiDett.setSubmitPolicy(self.mapperFasiDett.ManualSubmit)
self.mapperFasiDett.setModel(self.modelFasiDett)
self.lbNomePrev.setVisible(False)
self.lE_nomePrev.setVisible(False)
self.mapperFasiDett.addMapping(self.lE_codPrev, 0)
self.mapperFasiDett.addMapping(self.lE_revPrev, 1)
self.mapperFasiDett.addMapping(self.lE_fase, 2)
self.mapperFasiDett.addMapping(self.SB_Qta, 3)
self.mapperFasiDett.addMapping(self.SB_barraU, 4)
self.mapperFasiDett.addMapping(self.SB_setupH, 5)
self.mapperFasiDett.addMapping(self.SB_setupM, 6)
self.mapperFasiDett.addMapping(self.SB_cicloS, 7)
self.mapperFasiDett.addMapping(self.SB_cicloM, 8)
self.mapperFasiDett.addMapping(self.SB_percScarto, 9)
self.mapperFasiDett.addMapping(self.SB_PercEff, 10)
self.mapperFasiDett.addMapping(self.dataIns, 11)
self.mapperFasiDett.addMapping(self.dataMod, 12)
self.mapperFasiDett.toFirst()
iModel=self.mapperFasiDett.currentIndex()
if not self.lE_codPrev.text():
self.lE_codPrev.setText(self.num_prev)
if not self.lE_revPrev.text():
self.lE_revPrev.setText(self.rev_prev)
def nuova_fase(self):
self.rows=self.modelFasiDett.rowCount()
self.pbInsFase.setEnabled(True)
self.pbInsFase.setVisible(True)
self.pbModFase.setEnabled(False)
self.pbCancFase.setEnabled(False)
def inserimento_fase(self):
self.dataIns.setDateTime(datetime.now())
self.dataMod.setDateTime(datetime.now())
rows=self.modelFasiDett.rowCount()
self.modelFasiDett.insertRows(rows, 1)
self.mapperFasiDett.toLast()
self.mapperFasiDett.submit()
def modifica_fase(self):
print("modifica")
def cancellazione_fase(self):
print("cancella")
app = QApplication(sys.argv)
window = Fase()
window.show()
sys.exit(app.exec_())
----- .ui file -----
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_f_gestioneFaseTest(object):
def setupUi(self, f_gestioneFaseTest):
f_gestioneFaseTest.setObjectName("f_gestioneFaseTest")
f_gestioneFaseTest.setWindowModality(QtCore.Qt.NonModal)
f_gestioneFaseTest.resize(557, 433)
self.gridLayout = QtWidgets.QGridLayout(f_gestioneFaseTest)
self.gridLayout.setObjectName("gridLayout")
self.formLayout = QtWidgets.QFormLayout()
self.formLayout.setObjectName("formLayout")
self.lbDescFase = QtWidgets.QLabel(f_gestioneFaseTest)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.lbDescFase.sizePolicy().hasHeightForWidth())
self.lbDescFase.setSizePolicy(sizePolicy)
self.lbDescFase.setObjectName("lbDescFase")
self.formLayout.setWidget(0, QtWidgets.QFormLayout.LabelRole, self.lbDescFase)
self.lE_fase = QtWidgets.QLineEdit(f_gestioneFaseTest)
self.lE_fase.setObjectName("lE_fase")
self.formLayout.setWidget(0, QtWidgets.QFormLayout.FieldRole, self.lE_fase)
self.lbQta = QtWidgets.QLabel(f_gestioneFaseTest)
self.lbQta.setObjectName("lbQta")
self.formLayout.setWidget(1, QtWidgets.QFormLayout.LabelRole, self.lbQta)
self.SB_Qta = QtWidgets.QSpinBox(f_gestioneFaseTest)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.SB_Qta.sizePolicy().hasHeightForWidth())
self.SB_Qta.setSizePolicy(sizePolicy)
self.SB_Qta.setMinimumSize(QtCore.QSize(50, 0))
self.SB_Qta.setMaximumSize(QtCore.QSize(100, 16777215))
self.SB_Qta.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTrailing|QtCore.Qt.AlignVCenter)
self.SB_Qta.setMaximum(999999)
self.SB_Qta.setObjectName("SB_Qta")
self.formLayout.setWidget(1, QtWidgets.QFormLayout.FieldRole, self.SB_Qta)
self.lbBarraU = QtWidgets.QLabel(f_gestioneFaseTest)
self.lbBarraU.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTrailing|QtCore.Qt.AlignVCenter)
self.lbBarraU.setObjectName("lbBarraU")
self.formLayout.setWidget(2, QtWidgets.QFormLayout.LabelRole, self.lbBarraU)
self.SB_barraU = QtWidgets.QSpinBox(f_gestioneFaseTest)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.SB_barraU.sizePolicy().hasHeightForWidth())
self.SB_barraU.setSizePolicy(sizePolicy)
self.SB_barraU.setMaximumSize(QtCore.QSize(100, 16777215))
self.SB_barraU.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTrailing|QtCore.Qt.AlignVCenter)
self.SB_barraU.setMaximum(99999)
self.SB_barraU.setObjectName("SB_barraU")
self.formLayout.setWidget(2, QtWidgets.QFormLayout.FieldRole, self.SB_barraU)
self.lbSetupH = QtWidgets.QLabel(f_gestioneFaseTest)
self.lbSetupH.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTrailing|QtCore.Qt.AlignVCenter)
self.lbSetupH.setObjectName("lbSetupH")
self.formLayout.setWidget(3, QtWidgets.QFormLayout.LabelRole, self.lbSetupH)
self.SB_setupH = QtWidgets.QDoubleSpinBox(f_gestioneFaseTest)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.MinimumExpanding, QtWidgets.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.SB_setupH.sizePolicy().hasHeightForWidth())
self.SB_setupH.setSizePolicy(sizePolicy)
self.SB_setupH.setMaximumSize(QtCore.QSize(100, 16777215))
self.SB_setupH.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTrailing|QtCore.Qt.AlignVCenter)
self.SB_setupH.setObjectName("SB_setupH")
self.formLayout.setWidget(3, QtWidgets.QFormLayout.FieldRole, self.SB_setupH)
self.lbSetupM = QtWidgets.QLabel(f_gestioneFaseTest)
self.lbSetupM.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTrailing|QtCore.Qt.AlignVCenter)
self.lbSetupM.setObjectName("lbSetupM")
self.formLayout.setWidget(4, QtWidgets.QFormLayout.LabelRole, self.lbSetupM)
self.SB_setupM = QtWidgets.QDoubleSpinBox(f_gestioneFaseTest)
self.SB_setupM.setMaximumSize(QtCore.QSize(100, 16777215))
self.SB_setupM.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTrailing|QtCore.Qt.AlignVCenter)
self.SB_setupM.setObjectName("SB_setupM")
self.formLayout.setWidget(4, QtWidgets.QFormLayout.FieldRole, self.SB_setupM)
self.lbCicloS = QtWidgets.QLabel(f_gestioneFaseTest)
self.lbCicloS.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTrailing|QtCore.Qt.AlignVCenter)
self.lbCicloS.setObjectName("lbCicloS")
self.formLayout.setWidget(5, QtWidgets.QFormLayout.LabelRole, self.lbCicloS)
self.SB_cicloS = QtWidgets.QSpinBox(f_gestioneFaseTest)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.SB_cicloS.sizePolicy().hasHeightForWidth())
self.SB_cicloS.setSizePolicy(sizePolicy)
self.SB_cicloS.setMaximumSize(QtCore.QSize(100, 16777215))
self.SB_cicloS.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTrailing|QtCore.Qt.AlignVCenter)
self.SB_cicloS.setMaximum(99999)
self.SB_cicloS.setObjectName("SB_cicloS")
self.formLayout.setWidget(5, QtWidgets.QFormLayout.FieldRole, self.SB_cicloS)
self.lbCicloM = QtWidgets.QLabel(f_gestioneFaseTest)
self.lbCicloM.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTrailing|QtCore.Qt.AlignVCenter)
self.lbCicloM.setObjectName("lbCicloM")
self.formLayout.setWidget(6, QtWidgets.QFormLayout.LabelRole, self.lbCicloM)
self.SB_cicloM = QtWidgets.QDoubleSpinBox(f_gestioneFaseTest)
self.SB_cicloM.setMaximumSize(QtCore.QSize(100, 16777215))
self.SB_cicloM.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTrailing|QtCore.Qt.AlignVCenter)
self.SB_cicloM.setObjectName("SB_cicloM")
self.formLayout.setWidget(6, QtWidgets.QFormLayout.FieldRole, self.SB_cicloM)
self.lbPerScarto = QtWidgets.QLabel(f_gestioneFaseTest)
self.lbPerScarto.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTrailing|QtCore.Qt.AlignVCenter)
self.lbPerScarto.setObjectName("lbPerScarto")
self.formLayout.setWidget(7, QtWidgets.QFormLayout.LabelRole, self.lbPerScarto)
self.SB_percScarto = QtWidgets.QDoubleSpinBox(f_gestioneFaseTest)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.SB_percScarto.sizePolicy().hasHeightForWidth())
self.SB_percScarto.setSizePolicy(sizePolicy)
self.SB_percScarto.setMaximumSize(QtCore.QSize(100, 16777215))
self.SB_percScarto.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTrailing|QtCore.Qt.AlignVCenter)
self.SB_percScarto.setDecimals(4)
self.SB_percScarto.setMaximum(99.9999)
self.SB_percScarto.setObjectName("SB_percScarto")
self.formLayout.setWidget(7, QtWidgets.QFormLayout.FieldRole, self.SB_percScarto)
self.lbPercEff = QtWidgets.QLabel(f_gestioneFaseTest)
self.lbPercEff.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTrailing|QtCore.Qt.AlignVCenter)
self.lbPercEff.setObjectName("lbPercEff")
self.formLayout.setWidget(8, QtWidgets.QFormLayout.LabelRole, self.lbPercEff)
self.SB_PercEff = QtWidgets.QDoubleSpinBox(f_gestioneFaseTest)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.SB_PercEff.sizePolicy().hasHeightForWidth())
self.SB_PercEff.setSizePolicy(sizePolicy)
self.SB_PercEff.setMaximumSize(QtCore.QSize(100, 16777215))
self.SB_PercEff.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTrailing|QtCore.Qt.AlignVCenter)
self.SB_PercEff.setDecimals(4)
self.SB_PercEff.setMaximum(99.9999)
self.SB_PercEff.setSingleStep(0.1)
self.SB_PercEff.setObjectName("SB_PercEff")
self.formLayout.setWidget(8, QtWidgets.QFormLayout.FieldRole, self.SB_PercEff)
self.gridLayout.addLayout(self.formLayout, 0, 0, 2, 1)
self.verticalLayout_2 = QtWidgets.QVBoxLayout()
self.verticalLayout_2.setObjectName("verticalLayout_2")
self.lbNomePrev = QtWidgets.QLabel(f_gestioneFaseTest)
self.lbNomePrev.setAlignment(QtCore.Qt.AlignCenter)
self.lbNomePrev.setObjectName("lbNomePrev")
self.verticalLayout_2.addWidget(self.lbNomePrev)
self.lE_nomePrev = QtWidgets.QLineEdit(f_gestioneFaseTest)
font = QtGui.QFont()
font.setBold(True)
self.lE_nomePrev.setFont(font)
self.lE_nomePrev.setStyleSheet("background-color: rgb(118, 214, 255);\n"
"color: rgb(255, 38, 0);\n"
"")
self.lE_nomePrev.setReadOnly(True)
self.lE_nomePrev.setObjectName("lE_nomePrev")
self.verticalLayout_2.addWidget(self.lE_nomePrev)
self.label_3 = QtWidgets.QLabel(f_gestioneFaseTest)
self.label_3.setAlignment(QtCore.Qt.AlignCenter)
self.label_3.setObjectName("label_3")
self.verticalLayout_2.addWidget(self.label_3)
self.lE_codPrev = QtWidgets.QLineEdit(f_gestioneFaseTest)
font = QtGui.QFont()
font.setBold(True)
self.lE_codPrev.setFont(font)
self.lE_codPrev.setStyleSheet("background-color: rgb(118, 214, 255);\n"
"color: rgb(255, 38, 0);\n"
"")
self.lE_codPrev.setAlignment(QtCore.Qt.AlignLeading|QtCore.Qt.AlignLeft|QtCore.Qt.AlignVCenter)
self.lE_codPrev.setReadOnly(True)
self.lE_codPrev.setObjectName("lE_codPrev")
self.verticalLayout_2.addWidget(self.lE_codPrev)
self.label = QtWidgets.QLabel(f_gestioneFaseTest)
self.label.setAlignment(QtCore.Qt.AlignCenter)
self.label.setObjectName("label")
self.verticalLayout_2.addWidget(self.label)
self.lE_revPrev = QtWidgets.QLineEdit(f_gestioneFaseTest)
font = QtGui.QFont()
font.setBold(True)
self.lE_revPrev.setFont(font)
self.lE_revPrev.setStyleSheet("background-color: rgb(118, 214, 255);\n"
"color: rgb(255, 38, 0);")
self.lE_revPrev.setReadOnly(True)
self.lE_revPrev.setObjectName("lE_revPrev")
self.verticalLayout_2.addWidget(self.lE_revPrev)
self.gridLayout.addLayout(self.verticalLayout_2, 0, 3, 1, 1)
self.verticalLayout = QtWidgets.QVBoxLayout()
self.verticalLayout.setObjectName("verticalLayout")
self.pbNuovaFase = QtWidgets.QPushButton(f_gestioneFaseTest)
self.pbNuovaFase.setStyleSheet("background-color: rgb(0, 150, 255);\n"
"color: rgb(255, 255, 255);")
self.pbNuovaFase.setObjectName("pbNuovaFase")
self.verticalLayout.addWidget(self.pbNuovaFase)
self.pbInsFase = QtWidgets.QPushButton(f_gestioneFaseTest)
self.pbInsFase.setStyleSheet("background-color: rgb(0, 144, 81);\n"
"color: rgb(255, 255, 255);")
self.pbInsFase.setObjectName("pbInsFase")
self.verticalLayout.addWidget(self.pbInsFase)
self.pbModFase = QtWidgets.QPushButton(f_gestioneFaseTest)
self.pbModFase.setStyleSheet("background-color: rgb(215, 131, 255);\n"
"color: rgb(255, 255, 255);")
self.pbModFase.setObjectName("pbModFase")
self.verticalLayout.addWidget(self.pbModFase)
self.pbCancFase = QtWidgets.QPushButton(f_gestioneFaseTest)
self.pbCancFase.setStyleSheet("background-color: rgb(255, 38, 0);\n"
"color: rgb(255, 255, 255);\n"
"")
self.pbCancFase.setObjectName("pbCancFase")
self.verticalLayout.addWidget(self.pbCancFase)
self.gridLayout.addLayout(self.verticalLayout, 1, 3, 1, 1)
self.horizontalLayout_8 = QtWidgets.QHBoxLayout()
self.horizontalLayout_8.setObjectName("horizontalLayout_8")
self.pbFirst = QtWidgets.QPushButton(f_gestioneFaseTest)
self.pbFirst.setMaximumSize(QtCore.QSize(200, 16777215))
self.pbFirst.setObjectName("pbFirst")
self.horizontalLayout_8.addWidget(self.pbFirst)
self.pbLast = QtWidgets.QPushButton(f_gestioneFaseTest)
self.pbLast.setObjectName("pbLast")
self.horizontalLayout_8.addWidget(self.pbLast)
self.pbPrevious = QtWidgets.QPushButton(f_gestioneFaseTest)
self.pbPrevious.setMaximumSize(QtCore.QSize(200, 16777215))
self.pbPrevious.setObjectName("pbPrevious")
self.horizontalLayout_8.addWidget(self.pbPrevious)
self.pbNext = QtWidgets.QPushButton(f_gestioneFaseTest)
self.pbNext.setMaximumSize(QtCore.QSize(200, 16777215))
self.pbNext.setObjectName("pbNext")
self.horizontalLayout_8.addWidget(self.pbNext)
self.gridLayout.addLayout(self.horizontalLayout_8, 2, 0, 1, 1)
self.lbNomeRisorsa = QtWidgets.QLabel(f_gestioneFaseTest)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Maximum, QtWidgets.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.lbNomeRisorsa.sizePolicy().hasHeightForWidth())
self.lbNomeRisorsa.setSizePolicy(sizePolicy)
self.lbNomeRisorsa.setMinimumSize(QtCore.QSize(0, 0))
self.lbNomeRisorsa.setMaximumSize(QtCore.QSize(16777215, 16777215))
self.lbNomeRisorsa.setText("")
self.lbNomeRisorsa.setObjectName("lbNomeRisorsa")
self.gridLayout.addWidget(self.lbNomeRisorsa, 2, 1, 1, 1)
self.retranslateUi(f_gestioneFaseTest)
QtCore.QMetaObject.connectSlotsByName(f_gestioneFaseTest)
f_gestioneFaseTest.setTabOrder(self.SB_Qta, self.SB_barraU)
f_gestioneFaseTest.setTabOrder(self.SB_barraU, self.SB_setupH)
f_gestioneFaseTest.setTabOrder(self.SB_setupH, self.SB_setupM)
f_gestioneFaseTest.setTabOrder(self.SB_setupM, self.SB_cicloS)
f_gestioneFaseTest.setTabOrder(self.SB_cicloS, self.SB_cicloM)
f_gestioneFaseTest.setTabOrder(self.SB_cicloM, self.SB_percScarto)
f_gestioneFaseTest.setTabOrder(self.SB_percScarto, self.SB_PercEff)
f_gestioneFaseTest.setTabOrder(self.SB_PercEff, self.pbFirst)
f_gestioneFaseTest.setTabOrder(self.pbFirst, self.pbPrevious)
f_gestioneFaseTest.setTabOrder(self.pbPrevious, self.pbNext)
def retranslateUi(self, f_gestioneFaseTest):
_translate = QtCore.QCoreApplication.translate
f_gestioneFaseTest.setWindowTitle(_translate("f_gestioneFaseTest", "Inserimento Riga Preventivo"))
self.lbDescFase.setText(_translate("f_gestioneFaseTest", "descrizione Fase"))
self.lbQta.setText(_translate("f_gestioneFaseTest", "Quantità"))
self.lbBarraU.setText(_translate("f_gestioneFaseTest", "Barra utile"))
self.lbSetupH.setText(_translate("f_gestioneFaseTest", "Setup (h)"))
self.lbSetupM.setText(_translate("f_gestioneFaseTest", "Setup (min)"))
self.lbCicloS.setText(_translate("f_gestioneFaseTest", "Ciclo (s)"))
self.lbCicloM.setText(_translate("f_gestioneFaseTest", "Ciclo (min)"))
self.lbPerScarto.setText(_translate("f_gestioneFaseTest", "% Scarto"))
self.lbPercEff.setText(_translate("f_gestioneFaseTest", "% Efficienza"))
self.lbNomePrev.setText(_translate("f_gestioneFaseTest", "nome preventivo"))
self.label_3.setText(_translate("f_gestioneFaseTest", "codice preventivo"))
self.label.setText(_translate("f_gestioneFaseTest", "revisione"))
self.pbNuovaFase.setText(_translate("f_gestioneFaseTest", "Nuova Fase"))
self.pbInsFase.setText(_translate("f_gestioneFaseTest", "Inserimento Fase"))
self.pbModFase.setText(_translate("f_gestioneFaseTest", "Modifica"))
self.pbCancFase.setText(_translate("f_gestioneFaseTest", "Cancellazione"))
self.pbFirst.setText(_translate("f_gestioneFaseTest", "Primo"))
self.pbLast.setText(_translate("f_gestioneFaseTest", "Ultimo"))
self.pbPrevious.setText(_translate("f_gestioneFaseTest", "Precedente"))
self.pbNext.setText(_translate("f_gestioneFaseTest", "Successivo"))

add different widgets to each wxnotebook tab permanently in a txt file or sqlite

Please how can I add permanently different widgets in the tabs and save them permanently
<html>
<pre>
import wx
tabs = []
with open('test.txt','r') as file:
for element in file.readlines():
tabs.append(element)
class TabPanel(wx.Panel):
def __init__(self, parent, pageNum):
self.parent = parent
self.pageNum = pageNum
wx.Panel.__init__(self, parent=parent)
class DemoFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, "Notebook", size=(600,400))
panel = wx.Panel(self)
self.tab_num = len(tabs)
self.notebook = wx.Notebook(panel)
for tab in tabs:
name = "Page " + str(tab)
tab = TabPanel(self.notebook, 1)
self.notebook.AddPage(tab, name)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.notebook, 1, wx.ALL|wx.EXPAND, 5)
btn = wx.Button(panel, label="Add Page")
btn.Bind(wx.EVT_BUTTON, self.addPage)
sizer.Add(btn)
panel.SetSizer(sizer)
self.Layout()
self.Show()
def addPage(self, event):
self.tab_num += 1
new_tab = TabPanel(self.notebook, self.tab_num)
self.notebook.AddPage(new_tab, "Page %s" % self.tab_num)
tabs.append(self.tab_num)
print()
with open('test.txt','a+') as file:
file.write(str(self.tab_num))
file.write('\n')
if __name__ == "__main__":
app = wx.App(False)
frame = DemoFrame()
app.MainLoop()
</pre>
</html>
Something like this should get you started (sorry not readdly familiar with python):
class DemoFrame(wx.Frame):
def __init__(self):
self.tab_num = 1
wx.Frame.__init__(self, None, wx.ID_ANY, "Notebook", size=(600,400))
panel = wx.Panel(self)
with open( "test.txt", "r" as file:
self.tab_num = file.read()
self.notebook = wx.Notebook(panel)
for tab in [1..self.tab_num]:
name = "Page " + str(tab)
tab = TabPanel(self.notebook, 1)
self.notebook.AddPage(tab, name)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.notebook, 1, wx.ALL|wx.EXPAND, 5)
btn = wx.Button(panel, label="Add Page")
btn.Bind(wx.EVT_BUTTON, self.addPage)
sizer.Add(btn)
panel.SetSizer(sizer)
self.Layout()
self.Show()
def addPage(self, event):
self.tab_num += 1
new_tab = TabPanel(self.notebook, self.tab_num)
self.notebook.AddPage(new_tab, "Page %s" % self.tab_num)
tabs.append(self.tab_num)
print()
with open('test.txt','a+') as file:
file.write(str(self.tab_num))
file.write('\n')
if __name__ == "__main__":
app = wx.App(False)
frame = DemoFrame()
app.MainLoop()

QGraphicsItem, how to paint element above the childs?

from PyQt5.QtWidgets import QGraphicsItem
from PyQt5.QtSvg import QGraphicsSvgItem, QSvgRenderer
from PyQt5.QtCore import Qt, QRectF
class MyItem(QGraphicsItem):
def __init__(self, parent ):
super().__init__( parent = parent )
self.textRect = QRectF( 0,0,100,100 )
self.SvgGItem = QGraphicsSvgItem(self)
self.Svg_renderer = QSvgRenderer("./item.svg")
self.SvgGItem.setSharedRenderer ( self.Svg_renderer )
self.SvgGItem.setElementId( "1" )
def paint(self, painter, option, widget):
alignFlags = Qt.AlignLeft | Qt.AlignTop
painter.drawText( self.textRect, alignFlags , "text" )
I want the text being above the svg item. This code is for example, the real code is pretty difficult, but the sence is the same. setZValue() does not work in this case and it is clear. There is some way or other decision?
Try it:
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtSvg import QGraphicsSvgItem, QSvgRenderer
class MyItem(QGraphicsItem):
def __init__(self):
super().__init__()
self.x = 0
self.y = 0
self.w = 100
self.h = 100
self.width = 8
self.text = "None"
self.setFlags(MyItem.ItemIsSelectable | MyItem.ItemIsMovable)
def boundingRect(self):
return QRectF(self.x, self.y, self.w, self.h)
def paint(self, painter, option, widget):
painter.setPen(QPen(Qt.blue, self.width))
rec = self.boundingRect().adjusted(self.width / 2,
self.width / 2,
-self.width / 2,
-self.width / 2)
painter.setBrush(Qt.yellow)
painter.drawRect(rec)
painter.setFont(QFont("Arial", 16))
self.textRect = QRectF( 0, 0, 100, 100 )
alignFlags = Qt.AlignCenter
painter.drawText( self.textRect, alignFlags , "! text !" )
class MySvg(QGraphicsSvgItem):
def __init__(self):
super(MySvg, self).__init__('blank.svg')
self.setFlags(QGraphicsItem.ItemIsSelectable|
QGraphicsItem.ItemIsMovable)
class GraphicsScene(QGraphicsScene):
def __init__(self):
super().__init__()
self.setSceneRect(-200, -200, 400, 400)
self.fillScene()
def fillScene(self):
test = MySvg()
self.addItem(test)
item = MyItem()
self.addItem(item)
def drawBackground(self, painter, rect):
painter.setPen(QPen(Qt.green, 3))
painter.drawRect(self.sceneRect())
painter.setPen(QPen(Qt.red, 3))
painter.drawText(0, 0, '0')
painter.drawText(-200, -200, '-200')
painter.drawText(200, 200, '200')
class Main(QWidget):
def __init__(self):
super(Main, self).__init__()
self.scene = GraphicsScene()
self.view = QGraphicsView(self)
self.view.setScene(self.scene)
hbox = QHBoxLayout()
hbox.addWidget(self.view)
self.setLayout(hbox)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Main()
ex.show()
sys.exit(app.exec_())

Custom Widget PyQt

I am trying to create a custom widget and adding it to a listwidget following the example here but used a widget as here(Answer) instead of the progress bar. The custom widget works fine independently. But when it is added to the list widget, the horizontal scrolling works perfect but the vertical scroll covers only a portion of the image. Can somebody suggest some improvement?
The code is
import sys
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 MainWidget(QtGui.QWidget):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.scene = QtGui.QGraphicsScene()
self.view = QtGui.QGraphicsView(self.scene)
layout = QtGui.QVBoxLayout()
layout.addWidget(self.view)
self.setLayout(layout)
self.pixmap_item = QtGui.QGraphicsPixmapItem(QtGui.QPixmap('image.png'), None, self.scene)
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(800, 600)
sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Preferred, QtGui.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(MainWindow.sizePolicy().hasHeightForWidth())
MainWindow.setSizePolicy(sizePolicy)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.listWidget = QtGui.QListWidget(self.centralwidget)
self.listWidget.setGeometry(QtCore.QRect(170, 20, 400, 400))
sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.listWidget.sizePolicy().hasHeightForWidth())
self.listWidget.setSizePolicy(sizePolicy)
self.listWidget.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAsNeeded)
self.listWidget.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAsNeeded)
self.listWidget.setObjectName(_fromUtf8("listWidget"))
self.pushButton = QtGui.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(40, 200, 113, 32))
self.pushButton.setObjectName(_fromUtf8("pushButton"))
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 22))
self.menubar.setObjectName(_fromUtf8("menubar"))
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
self.pushButton.setText(_translate("MainWindow", "PushButton", None))
class MyForm(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.pushButton.clicked.connect(self.progress)
def progress(self):
item = QtGui.QListWidgetItem(self.ui.listWidget)
item_widget = MainWidget()
item.setSizeHint(item_widget.sizeHint())
self.ui.listWidget.addItem(item)
self.ui.listWidget.setItemWidget(item,item_widget)
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp = MyForm()
myapp.show()
sys.exit(app.exec_())
and here is the test image:

PyQt draw a vertical label

I want to rotate a QLabel so that it is oriented vertically. A similar question was asked here: PyQT: Rotate a QLabel so that it's positioned diagonally instead of horizontally but the code in answer causes a recursive loop issue.
Here is the code I am using, why does the code run in a recursive loop and why are the labels not drawn?:
from PyQt4.QtGui import QLabel, QPainter
from PyQt4.QtCore import QSize
from PyQt4 import QtCore, QtGui
import sys
class VerticalLabel(QLabel):
def __init__(self, text = ""):
QtGui.QLabel.__init__(self, text)
self.text = text
def paintEvent(self, event):
painter = QPainter(self)
painter.setPen(QtCore.Qt.black)
painter.translate(0, self.height()-1)
painter.rotate(-90)
self.setGeometry(self.x(), self.y(), self.height(), self.width())
painter.drawText(0, 0, self.text)
print("text: {0}".format(self.text))
painter.end()
def minimumSizeHint(self):
size = QLabel.minimumSizeHint(self)
return QSize(size.height(), size.width())
def sizeHint(self):
size = QLabel.sizeHint(self)
return QSize(size.height(), size.width())
def setText(self, newText):
self.text = newText
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
lbl1 = VerticalLabel('label 1')
lbl2 = VerticalLabel('label 2')
lbl3 = VerticalLabel('label 3')
hBoxLayout = QtGui.QHBoxLayout()
hBoxLayout.addWidget(lbl1)
hBoxLayout.addWidget(lbl2)
hBoxLayout.addWidget(lbl3)
self.setLayout(hBoxLayout)
self.setGeometry(300, 300, 250, 150)
self.show()
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
This is slightly modified version of the same code from another answer and remember if you put any print under paintEvent it will print every time if you have any activity on the widget like mouse move or re size or what ever even a touch in window will redraw, because that's what paintEvent means.
class MyLabel(QtGui.QWidget):
def __init__(self, text=None):
super(self.__class__, self).__init__()
self.text = text
def paintEvent(self, event):
painter = QtGui.QPainter(self)
painter.setPen(QtCore.Qt.black)
painter.translate(20, 100)
painter.rotate(-90)
if self.text:
painter.drawText(0, 0, self.text)
painter.end()
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
lbl1 = MyLabel('lbl 1')
lbl2 = MyLabel('lbl 2')
lbl3 = MyLabel('lbl 3')
hBoxLayout = QtGui.QHBoxLayout()
hBoxLayout.addWidget(lbl1)
hBoxLayout.addWidget(lbl2)
hBoxLayout.addWidget(lbl3)
self.setLayout(hBoxLayout)
self.setGeometry(300, 300, 250, 150)
self.show()
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()

Resources