Delete rows and send text to an item in a QTableWidget

0

I have a problem trying to send a text to an item in a tablewidget.

This is the code I use:

from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5 import uic 
from PyQt5.Qt import *


class Principal(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        uic.loadUi("gif.ui",self)

        self.table.setColumnWidth(0,200)
        print(self.table.item(1,1).text())
        self.s = self.table.item(0,1)
        self.s.setText("s")



app = QApplication([])
p = Principal()
p.show()
app.exec_()

and this is an error I receive:

Traceback (most recent call last):
File "C:\Users\Angel\Desktop\gif.py", line 21, in <module>
p = Principal()
File "C:\Users\Angel\Desktop\gif.py", line 16, in __init__
self.s.setText("s")
AttributeError: 'NoneType' object has no attribute 'setText'
[Finished in 0.5s with exit code 1]

Annex to this how can I do to delete a row?

.ui file

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>800</width>
    <height>600</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <widget class="QTableWidget" name="table">
    <property name="geometry">
     <rect>
      <x>40</x>
      <y>150</y>
      <width>641</width>
      <height>281</height>
     </rect>
    </property>
    <property name="frameShape">
     <enum>QFrame::NoFrame</enum>
    </property>
    <property name="frameShadow">
     <enum>QFrame::Plain</enum>
    </property>
    <property name="lineWidth">
     <number>11</number>
    </property>
    <property name="midLineWidth">
     <number>7</number>
    </property>
    <row>
     <property name="text">
      <string>1</string>
     </property>
    </row>
    <row>
     <property name="text">
      <string>2</string>
     </property>
    </row>
    <row>
     <property name="text">
      <string>3</string>
     </property>
    </row>
    <column>
     <property name="text">
      <string>1</string>
     </property>
    </column>
    <column>
     <property name="text">
      <string>2</string>
     </property>
    </column>
    <column>
     <property name="text">
      <string>3</string>
     </property>
    </column>
    <item row="1" column="1">
     <property name="text">
      <string>kjhkjhj</string>
     </property>
    </item>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>800</width>
     <height>21</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>
    
asked by Revsky01 15.07.2018 в 23:32
source

1 answer

1

You are assuming that if a QTableWidget has n rows and p columns all items are created, but it is not, by design having elements in fields vacion would involve memory expenses. So in your case the item (0,1) is not created, you will have to create it.

from PyQt5 import QtWidgets, uic 


class Principal(QtWidgets.QMainWindow):
    def __init__(self):
        super(Principal, self).__init__()
        uic.loadUi("gif.ui",self)
        self.table.setColumnWidth(0,200)

        pos = (0, 1)
        it = self.table.item(*pos)
        if it is None:
            it = QtWidgets.QTableWidgetItem()
            self.table.setItem(*pos, it)
        it.setText("s")


if __name__ == '__main__':
    import sys

    app = QtWidgets.QApplication(sys.argv)
    p = Principal()
    p.show()
    sys.exit(app.exec_())

To remove a row you must use the removeRow () method:

example:

class Principal(QtWidgets.QMainWindow):
    def __init__(self):
        super(Principal, self).__init__()
        uic.loadUi("gif.ui",self)

        self.table.removeRow(1)

Finally I recommend you check the documentation of Qt, Qt is already many years old so it is most likely that it has a function for what you want, and finally Google will always help you.

    
answered by 16.07.2018 / 00:09
source