Results 1 to 4 of 4

Thread: [Python] QTableWidget not displaying data when set as Main Window's central widget.

  1. #1
    Join Date
    Feb 2012
    Posts
    3
    Qt products
    Platforms
    Windows

    Default [Python] QTableWidget not displaying data when set as Main Window's central widget.

    EDIT:
    using PyQt 4.9.1 (32 bit) for Python 2.7 (32 bit) on Windows XP SP3 (32 bit), with Python 2.7 (32 bit).
    /EDIT

    Hello.

    I am new to the forums and to PyQt. Generally speaking I am relatively new to programming as well.

    I have a small app I am working on for the company I work for, but cannot make any progress until I get a breaking bug ironed out.

    I have a MainWindow object and it has a QTextEdit widget set as an attribute and a QTableWidget set as an attribute. I have a function called initOpenDialog which does a check to see what type of file was opened. If the file is a .txt file, the TextEdit widget is set as the Main Window's central widget and the text is displayed within. If the file's type is .csv, the QTableWidget object is set as the Main Window's central widget, however, there are a couple different bugs when this happens.

    Print statements are telling me that I am in fact receiving ALL the necessary data from these files to construct a QTableWidget with. When I set the central widget to this table widget, the app's central background color is changed from the default empty window color to the editor white color, however, there are no rows or columns displayed within the widget, and no data shows up.

    Also, if any line of an opened text file exceeds the width of the QTextEdit widget, the widget is not even set as the main window's central widget at ALL.

    Keep in mind this is still indev/temporary code and not all of the comments are 100% accurate to what some of the functions do here. Here's the full code to the program so far:

    Qt Code:
    1. import sys, os, csv
    2. from PyQt4 import QtGui
    3. from PyQt4.Qt import *
    4.  
    5. class Spreadsheet(QTableWidget):
    6. def __init__(self, *args):
    7. super(Spreadsheet, self).__init__(*args)
    8.  
    9. def setData(self):
    10. r = 0
    11. for row in self.rows:
    12. c = 0
    13. for item in row:
    14. i = QTableWidgetItem(item)
    15. self.setItem(r, c, i)
    16. c += 1
    17. r += 1
    18.  
    19. class MainWindow(QMainWindow):
    20. def __init__(self):
    21. super(MainWindow, self).__init__()
    22.  
    23. self.initUi()
    24.  
    25. def setTooltipFont(self, font, size):
    26. QToolTip.setFont(QFont(str(font), int(size)))
    27.  
    28. def createAction(self, path, title, shortcut, tip, funct):
    29. icon = QIcon(os.path.join(path))
    30. action = QAction(icon, str(title), self)
    31. action.setShortcut(str(shortcut))
    32. action.setStatusTip(str(tip))
    33. action.triggered.connect(funct)
    34. return action
    35.  
    36. def getOpenDiologTip(self):
    37. return 'Open an Excel Workbook or Worksheet in CSV format.'
    38.  
    39. def xls2csv(self, xls): pass
    40.  
    41. def initOpenDiolog(self):
    42. fname = QFileDialog.getOpenFileName(
    43. self, self.getOpenDiologTip(), '/home')
    44.  
    45. name, ext = os.path.splitext(str(fname))
    46.  
    47. with open(fname, 'r') as f:
    48. editor = None
    49. if ext == '.txt':
    50. self.textEdit.setText(f.read())
    51. editor = self.textEdit
    52. elif ext == '.csv':
    53. self.worksheetEdit.rows = list(csv.reader(f, 'excel'))
    54. self.worksheetEdit.setData()
    55. editor = self.worksheetEdit
    56.  
    57. self.setCentralWidget(editor)
    58.  
    59. def initFileMenu(self, menubar):
    60. fileMenu = menubar.addMenu('&File')
    61.  
    62. fileMenu.addAction(self.createAction(
    63. os.path.join('resources', 'icons', 'Open.ico'),
    64. 'Open', 'Ctrl+O', self.getOpenDiologTip(), self.initOpenDiolog))
    65.  
    66. fileMenu.addAction(self.createAction(
    67. os.path.join('resources', 'icons', 'Exit.ico'),
    68. 'Quit', 'Ctrl+Q', 'Quit application.', self.close))
    69.  
    70. def getVersionInfo(self):
    71. try:
    72. with open('version_info.txt', 'r') as v:
    73. split_lines = [line.split() for line in v.readlines()]
    74. s = ''
    75. for l, line in enumerate(split_lines):
    76. s += makeString(split_lines[l])
    77. if l < len(split_lines) - 1:
    78. s += ', '
    79. return s
    80. except:
    81. return '>>>>> ERROR <<<<< - Could not retrieve version info.'
    82.  
    83. def initUi(self):
    84. menubar = self.menuBar()
    85. statusbar = self.statusBar()
    86.  
    87. self.textEdit = QTextEdit()
    88. self.worksheetEdit = Spreadsheet()
    89.  
    90. self.initFileMenu(menubar)
    91.  
    92. self.setGeometry(150, 150, 1024, 768)
    93. self.setWindowTitle(self.getVersionInfo())
    94. self.setWindowIcon(QIcon(os.path.join(
    95. 'resources', 'icons', 'logo.png')))
    96.  
    97. self.show()
    98.  
    99. def makeString(line):
    100. """ Concatenates a single string using the given list of strings. """
    101. s = ''
    102. for w, word in enumerate(line):
    103. W = word
    104. if w == 0:
    105. word += ' '
    106. elif w == len(line) - 1:
    107. word = ' ' + word
    108. if len(line) == 1:
    109. word = W
    110. s += word
    111. return s
    112.  
    113. def main():
    114. app = QApplication(sys.argv)
    115. mw = MainWindow()
    116. sys.exit(app.exec_())
    117.  
    118. if __name__ == '__main__':
    119. main()
    To copy to clipboard, switch view to plain text mode 

    The code should work without version_info.txt or the resources directory.

    Any help figuring this issue out would be greatly appreciated.

    -Adam
    Last edited by nice try; 13th February 2012 at 19:59.

  2. #2
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: [Python] QTableWidget not displaying data when set as Main Window's central widge

    You either need to construct your tableWidget large enough to hold all of your data or use QTableWidget::insertRow() and QTableWidget::insertColumn() in the loop where you add data. You can't set an item in a non-existent cell.

  3. #3
    Join Date
    Feb 2012
    Posts
    3
    Qt products
    Platforms
    Windows

    Default Re: [Python] QTableWidget not displaying data when set as Main Window's central widge

    Ahh yes. Fixed it, ty =)

  4. #4
    Join Date
    Feb 2012
    Posts
    3
    Qt products
    Platforms
    Windows

    Default Re: [Python] QTableWidget not displaying data when set as Main Window's central widge

    Edit:
    Wow. upon two more seconds of inspecting the code I noticed that the row and column counts are passed to the widget for construction. My bad. Wow.

    I also totally forgot about string.join()
    /Edit

    Actually, weird thought:

    I found this example app that I used to create my table widget, however, this app doesn't set the column or row count but still displays the rows, columns and the data thereof... thought you might find it interesting. Can anybody explain why this works but my app didn't?

    Qt Code:
    1. import sys
    2. from PyQt4.Qt import *
    3.  
    4. lista = ['aa', 'ab', 'ac']
    5. listb = ['ba', 'bb', 'bc']
    6. listc = ['ca', 'cb', 'cc']
    7. mystruct = {'A':lista, 'B':listb, 'C':listc}
    8.  
    9. class MyTable(QTableWidget):
    10. def __init__(self, thestruct, *args):
    11. QTableWidget.__init__(self, *args)
    12. self.data = thestruct
    13. self.setmydata()
    14.  
    15. def setmydata(self):
    16. n = 0
    17. for key in self.data:
    18. m = 0
    19. for item in self.data[key]:
    20. newitem = QTableWidgetItem(item)
    21. self.setItem(m, n, newitem)
    22. m += 1
    23. n += 1
    24.  
    25. def main(args):
    26. app = QApplication(args)
    27. table = MyTable(mystruct, 5, 3)
    28. table.show()
    29. sys.exit(app.exec_())
    30.  
    31. if __name__=="__main__":
    32. main(sys.argv)
    To copy to clipboard, switch view to plain text mode 
    Last edited by nice try; 14th February 2012 at 15:54.

Similar Threads

  1. Replies: 0
    Last Post: 14th November 2010, 19:38
  2. Displaying plot in main QT window
    By Gavin Harper in forum Qwt
    Replies: 1
    Last Post: 23rd August 2010, 13:59
  3. Problem displaying my main window
    By Salazaar in forum Newbie
    Replies: 104
    Last Post: 16th January 2008, 14:01
  4. show() in central main window
    By ufo-vl in forum Newbie
    Replies: 3
    Last Post: 28th July 2007, 02:33
  5. New to QT, problem displaying main window...
    By McCall in forum Qt Programming
    Replies: 4
    Last Post: 15th June 2007, 14:27

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.