pyQT5 - how to display form with QTableView and QPushButton
-
I'm new in PyQT5 and I'm trying to learn how to manage data from to SQLite db.
I created a form with QT Designer containing a QTableView to handle the SQL data (using also QSqlTableModel). I then added a QPushButton in the form with the intention to get it displayed below the QTableView.
I adapted a code provided by a tutorial (see below) but the result is when the I run the program the form displays only the QTableView and not even the QPushButton.
Which would be the correct code for handle it ? ThanksSet up the view
self.view = QTableView() self.view.setModel(self.model) self.view.setSelectionMode(QTableView.SingleSelection) self.view.setSelectionBehavior(QTableView.SelectItems)
set the first column not editable
delegate = ReadOnlyDelegate(self) self.view.setItemDelegateForColumn(0, delegate)
self.view.resizeColumnsToContents() self.setCentralWidget(self.view)
-
@Achab61 said in pyQT5 - how to display form with QTableView and QPushButton:
I then added a QPushButton in the form with the intention to get it displayed below the QTableView.
None of the above should be relevant then. Better show a screenshot of what your form looks like in Designer, or the content of the
.ui
file -
@Achab61 said in pyQT5 - how to display form with QTableView and QPushButton:
but the result is when the I run the program the form displays only the QTableView and not even the QPushButton.
But this screenshot does show the pushbutton below the table view?
And I did not mean to show a Preview from Designer, rather the design view, so that we know how you have laid out your widgets, and check you have used layouts etc. But there is not much point if it now turns out that the button is visible when you said it was not?
-
Hi,
From the looks of your code you are using a QMainWindow and set your view as central widget manually so it's not even certain what the relation is with Designer.
-
In my previuos post I attached the preview from the Designer (this is why the Qpushbutton is visible in the image...). Below is the entire py code and further down the .ui file.
Thanks for any suggestioncode_text import sys from PyQt5.uic import loadUi from PyQt5 import QtWidgets from PyQt5.QtCore import Qt from PyQt5.QtSql import QSqlDatabase, QSqlTableModel, QSqlQueryModel from PyQt5.QtWidgets import (QApplication,QMainWindow,QMessageBox,QTableView,QStyledItemDelegate, QPushButton) from connect_SQLITE import Database class ReadOnlyDelegate(QStyledItemDelegate): def createEditor(self, parent, option, index): QtWidgets.QMessageBox.critical(None, 'Attenzione', 'Campo non modificabile') return class Materiali(QMainWindow): def __init__(self, parent=None): super().__init__(parent) self.resize(415, 200) loadUi("/Users/MyUser/Documents/Micro/Ui_forms/materialiN1.ui", self) self.setWindowTitle("Gestione Materiali") # Set up the model self.model = QSqlTableModel(self) self.model.setTable("tb_Materiali") self.model.setEditStrategy(QSqlTableModel.OnFieldChange) # self.model.setHeaderData(0, Qt.Horizontal, "Codice materiale") self.model.setHeaderData(1, Qt.Horizontal, "Descrizione materiale") self.model.setHeaderData(2, Qt.Horizontal, "Peso specifico materiale") self.model.select() # Set up the view self.view = QTableView() self.view.setModel(self.model) self.view.setSelectionMode(QTableView.SingleSelection) self.view.setSelectionBehavior(QTableView.SelectItems) delegate = ReadOnlyDelegate(self) self.view.setItemDelegateForColumn(0, delegate) self.view.resizeColumnsToContents() self.setCentralWidget(self.view) app=QApplication([]) window=Materiali() window.show() app.exec() <?xml version="1.0" encoding="UTF-8"?> <ui version="4.0"> <class>Form</class> <widget class="QWidget" name="Form"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>809</width> <height>502</height> </rect> </property> <property name="windowTitle"> <string>Form</string> </property> <layout class="QFormLayout" name="formLayout"> <item row="1" column="0"> <widget class="QPushButton" name="bt_InsMat"> <property name="palette"> <palette> <active> <colorrole role="WindowText"> <brush brushstyle="SolidPattern"> <color alpha="216"> <red>0</red> <green>0</green> <blue>0</blue> </color> </brush> </colorrole> <colorrole role="Button"> <brush brushstyle="SolidPattern"> <color alpha="255"> <red>255</red> <green>255</green> <blue>255</blue> </color> </brush> </colorrole> <colorrole role="Light"> <brush brushstyle="SolidPattern"> <color alpha="255"> <red>255</red> <green>255</green> <blue>255</blue> </color> </brush> </colorrole> <colorrole role="Midlight"> <brush brushstyle="SolidPattern"> <color alpha="255"> <red>245</red> <green>245</green> <blue>245</blue> </color> </brush> </colorrole> <colorrole role="Dark"> <brush brushstyle="SolidPattern"> <color alpha="255"> <red>191</red> <green>191</green> <blue>191</blue> </color> </brush> </colorrole> <colorrole role="Mid"> <brush brushstyle="SolidPattern"> <color alpha="255"> <red>169</red> <green>169</green> <blue>169</blue> </color> </brush> </colorrole> <colorrole role="Text"> <brush brushstyle="SolidPattern"> <color alpha="255"> <red>0</red> <green>0</green> <blue>0</blue> </color> </brush> </colorrole> <colorrole role="BrightText"> <brush brushstyle="SolidPattern"> <color alpha="255"> <red>255</red> <green>255</green> <blue>255</blue> </color> </brush> </colorrole> <colorrole role="ButtonText"> <brush brushstyle="SolidPattern"> <color alpha="255"> <red>0</red> <green>0</green> <blue>0</blue> </color> </brush> </colorrole> <colorrole role="Base"> <brush brushstyle="SolidPattern"> <color alpha="255"> <red>0</red> <green>144</green> <blue>81</blue> </color> </brush> </colorrole> <colorrole role="Window"> <brush brushstyle="SolidPattern"> <color alpha="255"> <red>0</red> <green>144</green> <blue>81</blue> </color> </brush> </colorrole> <colorrole role="Shadow"> <brush brushstyle="SolidPattern"> <color alpha="255"> <red>0</red> <green>0</green> <blue>0</blue> </color> </brush> </colorrole> <colorrole role="Highlight"> <brush brushstyle="SolidPattern"> <color alpha="255"> <red>165</red> <green>205</green> <blue>255</blue> </color> </brush> </colorrole> <colorrole role="HighlightedText"> <brush brushstyle="SolidPattern"> <color alpha="216"> <red>0</red> <green>0</green> <blue>0</blue> </color> </brush> </colorrole> <colorrole role="Link"> <brush brushstyle="SolidPattern"> <color alpha="255"> <red>9</red> <green>79</green> <blue>209</blue> </color> </brush> </colorrole> <colorrole role="LinkVisited"> <brush brushstyle="SolidPattern"> <color alpha="255"> <red>255</red> <green>0</green> <blue>255</blue> </color> </brush> </colorrole> <colorrole role="AlternateBase"> <brush brushstyle="SolidPattern"> <color alpha="255"> <red>245</red> <green>245</green> <blue>245</blue> </color> </brush> </colorrole> <colorrole role="NoRole"> <brush brushstyle="SolidPattern"> <color alpha="255"> <red>0</red> <green>0</green> <blue>0</blue> </color> </brush> </colorrole> <colorrole role="ToolTipBase"> <brush brushstyle="SolidPattern"> <color alpha="255"> <red>255</red> <green>255</green> <blue>255</blue> </color> </brush> </colorrole> <colorrole role="ToolTipText"> <brush brushstyle="SolidPattern"> <color alpha="255"> <red>0</red> <green>0</green> <blue>0</blue> </color> </brush> </colorrole> <colorrole role="PlaceholderText"> <brush brushstyle="SolidPattern"> <color alpha="63"> <red>0</red> <green>0</green> <blue>0</blue> </color> </brush> </colorrole> </active> <inactive> <colorrole role="WindowText"> <brush brushstyle="SolidPattern"> <color alpha="216"> <red>0</red> <green>0</green> <blue>0</blue> </color> </brush> </colorrole> <colorrole role="Button"> <brush brushstyle="SolidPattern"> <color alpha="255"> <red>236</red> <green>236</green> <blue>236</blue> </color> </brush> </colorrole> <colorrole role="Light"> <brush brushstyle="SolidPattern"> <color alpha="255"> <red>255</red> <green>255</green> <blue>255</blue> </color> </brush> </colorrole> <colorrole role="Midlight"> <brush brushstyle="SolidPattern"> <color alpha="255"> <red>245</red> <green>245</green> <blue>245</blue> </color> </brush> </colorrole> <colorrole role="Dark"> <brush brushstyle="SolidPattern"> <color alpha="255"> <red>191</red> <green>191</green> <blue>191</blue> </color> </brush> </colorrole> <colorrole role="Mid"> <brush brushstyle="SolidPattern"> <color alpha="255"> <red>169</red> <green>169</green> <blue>169</blue> </color> </brush> </colorrole> <colorrole role="Text"> <brush brushstyle="SolidPattern"> <color alpha="216"> <red>0</red> <green>0</green> <blue>0</blue> </color> </brush> </colorrole> <colorrole role="BrightText"> <brush brushstyle="SolidPattern"> <color alpha="255"> <red>255</red> <green>255</green> <blue>255</blue> </color> </brush> </colorrole> <colorrole role="ButtonText"> <brush brushstyle="SolidPattern"> <color alpha="216"> <red>0</red> <green>0</green> <blue>0</blue> </color> </brush> </colorrole> <colorrole role="Base"> <brush brushstyle="SolidPattern"> <color alpha="255"> <red>255</red> <green>255</green> <blue>255</blue> </color> </brush> </colorrole> <colorrole role="Window"> <brush brushstyle="SolidPattern"> <color alpha="255"> <red>236</red> <green>236</green> <blue>236</blue> </color> </brush> </colorrole> <colorrole role="Shadow"> <brush brushstyle="SolidPattern"> <color alpha="255"> <red>0</red> <green>0</green> <blue>0</blue> </color> </brush> </colorrole> <colorrole role="Highlight"> <brush brushstyle="SolidPattern"> <color alpha="255"> <red>212</red> <green>212</green> <blue>212</blue> </color> </brush> </colorrole> <colorrole role="HighlightedText"> <brush brushstyle="SolidPattern"> <color alpha="216"> <red>0</red> <green>0</green> <blue>0</blue> </color> </brush> </colorrole> <colorrole role="Link"> <brush brushstyle="SolidPattern"> <color alpha="255"> <red>0</red> <green>0</green> <blue>255</blue> </color> </brush> </colorrole> <colorrole role="LinkVisited"> <brush brushstyle="SolidPattern"> <color alpha="255"> <red>255</red> <green>0</green> <blue>255</blue> </color> </brush> </colorrole> <colorrole role="AlternateBase"> <brush brushstyle="SolidPattern"> <color alpha="255"> <red>245</red> <green>245</green> <blue>245</blue> </color> </brush> </colorrole> <colorrole role="NoRole"> <brush brushstyle="NoBrush"> <color alpha="255"> <red>0</red> <green>0</green> <blue>0</blue> </color> </brush> </colorrole> <colorrole role="ToolTipBase"> <brush brushstyle="SolidPattern"> <color alpha="255"> <red>255</red> <green>255</green> <blue>255</blue> </color> </brush> </colorrole> <colorrole role="ToolTipText"> <brush brushstyle="SolidPattern"> <color alpha="255"> <red>0</red> <green>0</green> <blue>0</blue> </color> </brush> </colorrole> <colorrole role="PlaceholderText"> <brush brushstyle="SolidPattern"> <color alpha="63"> <red>0</red> <green>0</green> <blue>0</blue> </color> </brush> </colorrole> </inactive> <disabled> <colorrole role="WindowText"> <brush brushstyle="SolidPattern"> <color alpha="63"> <red>0</red> <green>0</green> <blue>0</blue> </color> </brush> </colorrole> <colorrole role="Button"> <brush brushstyle="SolidPattern"> <color alpha="255"> <red>255</red> <green>255</green> <blue>255</blue> </color> </brush> </colorrole> <colorrole role="Light"> <brush brushstyle="SolidPattern"> <color alpha="255"> <red>255</red> <green>255</green> <blue>255</blue> </color> </brush> </colorrole> <colorrole role="Midlight"> <brush brushstyle="SolidPattern"> <color alpha="255"> <red>245</red> <green>245</green> <blue>245</blue> </color> </brush> </colorrole> <colorrole role="Dark"> <brush brushstyle="SolidPattern"> <color alpha="255"> <red>191</red> <green>191</green> <blue>191</blue> </color> </brush> </colorrole> <colorrole role="Mid"> <brush brushstyle="SolidPattern"> <color alpha="255"> <red>169</red> <green>169</green> <blue>169</blue> </color> </brush> </colorrole> <colorrole role="Text"> <brush brushstyle="SolidPattern"> <color alpha="63"> <red>0</red> <green>0</green> <blue>0</blue> </color> </brush> </colorrole> <colorrole role="BrightText"> <brush brushstyle="SolidPattern"> <color alpha="255"> <red>255</red> <green>255</green> <blue>255</blue> </color> </brush> </colorrole> <colorrole role="ButtonText"> <brush brushstyle="SolidPattern"> <color alpha="63"> <red>0</red> <green>0</green> <blue>0</blue> </color> </brush> </colorrole> <colorrole role="Base"> <brush brushstyle="SolidPattern"> <color alpha="255"> <red>0</red> <green>144</green> <blue>81</blue> </color> </brush> </colorrole> <colorrole role="Window"> <brush brushstyle="SolidPattern"> <color alpha="255"> <red>0</red> <green>144</green> <blue>81</blue> </color> </brush> </colorrole> <colorrole role="Shadow"> <brush brushstyle="SolidPattern"> <color alpha="255"> <red>0</red> <green>0</green> <blue>0</blue> </color> </brush> </colorrole> <colorrole role="Highlight"> <brush brushstyle="SolidPattern"> <color alpha="255"> <red>212</red> <green>212</green> <blue>212</blue> </color> </brush> </colorrole> <colorrole role="HighlightedText"> <brush brushstyle="SolidPattern"> <color alpha="63"> <red>0</red> <green>0</green> <blue>0</blue> </color> </brush> </colorrole> <colorrole role="Link"> <brush brushstyle="SolidPattern"> <color alpha="255"> <red>0</red> <green>0</green> <blue>255</blue> </color> </brush> </colorrole> <colorrole role="LinkVisited"> <brush brushstyle="SolidPattern"> <color alpha="255"> <red>255</red> <green>0</green> <blue>255</blue> </color> </brush> </colorrole> <colorrole role="AlternateBase"> <brush brushstyle="SolidPattern"> <color alpha="255"> <red>245</red> <green>245</green> <blue>245</blue> </color> </brush> </colorrole> <colorrole role="NoRole"> <brush brushstyle="NoBrush"> <color alpha="255"> <red>0</red> <green>0</green> <blue>0</blue> </color> </brush> </colorrole> <colorrole role="ToolTipBase"> <brush brushstyle="SolidPattern"> <color alpha="255"> <red>255</red> <green>255</green> <blue>255</blue> </color> </brush> </colorrole> <colorrole role="ToolTipText"> <brush brushstyle="SolidPattern"> <color alpha="255"> <red>0</red> <green>0</green> <blue>0</blue> </color> </brush> </colorrole> <colorrole role="PlaceholderText"> <brush brushstyle="SolidPattern"> <color alpha="63"> <red>0</red> <green>0</green> <blue>0</blue> </color> </brush> </colorrole> </disabled> </palette> </property> <property name="autoFillBackground"> <bool>false</bool> </property> <property name="styleSheet"> <string notr="true"/> </property> <property name="text"> <string>Inserimento Materiale</string> </property> </widget> </item> <item row="0" column="0" colspan="2"> <widget class="QTableView" name="tableView"/> </item> </layout> </widget> <resources/> <connections/> </ui>
-
Why aren't you using the QTableView you define in Designer ?
-
You already have a QTableView in your designer widget, you should use that one and not replace it with another one in your constructor.
-
Not sure to get you're point.
Below the code I changed to use the QtableView defined in the designer (named tableView).
I did not get what should be changed in the constructor according to your commentdef init(self, parent=None):
super().init(parent)
self.resize(415, 200)
loadUi("/Users/MyUser/Documents/Micro/Ui_forms/materiali.ui", self)
self.setWindowTitle("Gestione Materiali")# Set up the model self.model = QSqlTableModel(self) self.model.setTable("tb_Materiali") self.model.setEditStrategy(QSqlTableModel.OnFieldChange) self.model.setHeaderData(0, Qt.Horizontal, "Codice materiale") self.model.setHeaderData(1, Qt.Horizontal, "Descrizione materiale") self.model.setHeaderData(2, Qt.Horizontal, "Peso specifico materiale") self.model.select()
Set up the view
self.tableView.setModel(self.model) self.tableView.setSelectionMode(QTableView.SingleSelection) self.tableView.setSelectionBehavior(QTableView.SelectItems) delegate = ReadOnlyDelegate(self) self.tableView.setItemDelegateForColumn(0, delegate) self.tableView.resizeColumnsToContents() self.setCentralWidget(self.tableView)
-
You are doing something fishy. Your UI file indicates that you used a QWidget as base widget however you are using a QMainWindow in your code. One key difference is that the QMainWindow has already a layout that cannot be replaced by the one you defined in Designer. Hence you are having troubles in different parts of you code:
- Wrong base widget in your code
- You create a new QTableView that is unrelated to the one you created in Designer
With the size of your UI, it would be simpler and faster to just create it in code.