Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. pyQT5 - how to display form with QTableView and QPushButton
Forum Updated to NodeBB v4.3 + New Features

pyQT5 - how to display form with QTableView and QPushButton

Scheduled Pinned Locked Moved Solved Qt for Python
11 Posts 3 Posters 852 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Achab61
    wrote on 22 Feb 2022, 17:19 last edited by
    #1

    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 ? Thanks

    Set 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)
    
    J 2 Replies Last reply 22 Feb 2022, 18:21
    0
    • A Achab61
      22 Feb 2022, 17:19

      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 ? Thanks

      Set 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)
      
      J Offline
      J Offline
      JonB
      wrote on 22 Feb 2022, 18:21 last edited by JonB
      #2

      @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

      1 Reply Last reply
      0
      • A Offline
        A Offline
        Achab61
        wrote on 23 Feb 2022, 16:45 last edited by
        #3

        Below the screenshot of the form:

        Screenshot form.png

        1 Reply Last reply
        0
        • A Achab61
          22 Feb 2022, 17:19

          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 ? Thanks

          Set 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)
          
          J Offline
          J Offline
          JonB
          wrote on 23 Feb 2022, 18:46 last edited by
          #4

          @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?

          1 Reply Last reply
          0
          • S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 23 Feb 2022, 19:08 last edited by
            #5

            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.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • A Offline
              A Offline
              Achab61
              wrote on 24 Feb 2022, 08:04 last edited by
              #6

              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 suggestion

              code_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>
              
              1 Reply Last reply
              0
              • S Offline
                S Offline
                SGaist
                Lifetime Qt Champion
                wrote on 24 Feb 2022, 20:46 last edited by
                #7

                Why aren't you using the QTableView you define in Designer ?

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                1
                • A Offline
                  A Offline
                  Achab61
                  wrote on 25 Feb 2022, 08:22 last edited by
                  #8

                  I tried, but still the QPushButton (bt_InsMat) does not get displayed.
                  Maybe the problem could be in the self.setCentralWidget statement is passed only the QtableView ? How should be added even the QPushButton widget ?

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on 25 Feb 2022, 20:21 last edited by
                    #9

                    You already have a QTableView in your designer widget, you should use that one and not replace it with another one in your constructor.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    1
                    • A Offline
                      A Offline
                      Achab61
                      wrote on 27 Feb 2022, 09:02 last edited by
                      #10

                      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 comment

                      def 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)
                      
                      1 Reply Last reply
                      0
                      • S Offline
                        S Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on 27 Feb 2022, 20:17 last edited by
                        #11

                        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:

                        1. Wrong base widget in your code
                        2. 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.

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        2

                        1/11

                        22 Feb 2022, 17:19

                        • Login

                        • Login or register to search.
                        1 out of 11
                        • First post
                          1/11
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved