Results 1 to 3 of 3

Thread: horizontalHeaderItem is None

  1. #1
    Join Date
    Oct 2016
    Posts
    3
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default horizontalHeaderItem is None

    Hi,

    I got _AttributeError: 'NoneType' object has no attribute 'text'_ at line 35 from the code below.

    In addition, I tested the index value and it seems to be correct.

    Any ideas why i get a NoneType?


    Qt Code:
    1. from PySide.QtCore import Slot
    2. from PySide import QtGui, QtCore
    3.  
    4. class Output(object):
    5.  
    6. def __init__(self, physical_type):
    7. ''' '''
    8. self.group_box = QtGui.QGroupBox('Output')
    9. self.layout = QtGui.QVBoxLayout()
    10. self.layout.addWidget(self.group_box, 1)
    11.  
    12. self.tabs = QtGui.QTabWidget()
    13.  
    14. def run(self):
    15. self.tabs.addTab(self.__genTable(), "None")
    16.  
    17. form_layout = QtGui.QFormLayout(self.group_box)
    18. form_layout.addWidget(self.tabs)
    19.  
    20. return self.layout
    21.  
    22. def __genTable(self, rows = 4, columns = 2):
    23. table_view = QtGui.QTableWidget(rows,columns)
    24.  
    25. table_view.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
    26. table_view.setSelectionBehavior( QtGui.QTableView.SelectItems )
    27.  
    28. table_view.horizontalHeader().sectionDoubleClicked.connect(self.changeHorizontalHeader)
    29.  
    30. return table_view
    31.  
    32.  
    33. def changeHorizontalHeader(self, index):
    34. model = self.tabs.currentWidget()
    35. oldHeader = model.horizontalHeaderItem(index).text()
    36. newHeader, ok = QtGui.QInputDialog.getText(QtGui.QInputDialog(),
    37. 'Change header label for column {}'.format(index),
    38. 'Header:',
    39. QtGui.QLineEdit.Normal,
    40. oldHeader)
    41. if ok:
    42. self.tabs.currentWidget().setText(newHeader)
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: horizontalHeaderItem is None

    QTableWidget::horizontalHeaderItem() returns the horizontal header item for column, column, if one has been set; otherwise returns 0. 0 is None in Python.

    Nothing I see in your code has created header items by, for example, setting header text.

  3. The following user says thank you to ChrisW67 for this useful post:

    Adler (15th November 2016)

  4. #3
    Join Date
    Oct 2016
    Posts
    3
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: horizontalHeaderItem is None

    @ChrisW67, your advice solved my problem.

    I added:
    Qt Code:
    1. table_view.setHorizontalHeaderLabels([str(x) for x in range(1, columns + 1)])
    2. table_view.setVerticalHeaderLabels([str(x) for x in range(1, rows + 1)])
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 4
    Last Post: 17th July 2010, 04:47

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.