toglez
4th October 2007, 17:31
Hi to all !!!
I'm new here and a newbie at the same time, so please don't be harsh...
I use Python with PyQt4 to develop a program reading a file with numbers and displaying them in a QTableWidget. So far so good.
What I have not accomplished yet, is to select certain columns from the QTableWidget and display them in another table. I would also like to store the selected tables' indexes in a global list.
My main.py file, the one that materializes the GUI and uses the derivative file from pyuic4 looks something like this:
from PyQt4 import QtGui, QtCore
from tstMain import Ui_tstMain
import sys, string
from os.path import isfile
from os import chdir
import variousFuncs
## Create a dictionary of updateable variables. These will be updated by the various
## functions of Class startGui
updVars = {"InitialFile": [], "SelectedColumns": []}
class startGui(QtGui.QMainWindow):
def __init__(self, parent = None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_tstMain()
self.ui.setupUi(self)
## create our slots here
QtCore.QObject.connect(self.ui.btn_SetWorkingDir, QtCore.SIGNAL("clicked()"), self.setWorkingDir)
QtCore.QObject.connect(self.ui.btn_OpenFile, QtCore.SIGNAL("clicked()"), self.openFile)
QtCore.QObject.connect(self.ui.btn_ListShow, QtCore.SIGNAL("clicked()"), self.showList)
def setWorkingDir(self):
wd = QtGui.QFileDialog(self)
currentDir = QtCore.QString(wd.getExistingDirectory())
self.ui.lnEdt_WorkingDir.setText(currentDir)
chdir(currentDir)
def openFile(self):
fd = QtGui.QFileDialog(self)
self.file = fd.getOpenFileName()
if isfile(self.file):
fileToOpen = open(self.file)
initialFile = [line.split() for line in fileToOpen.readlines()]
variousFuncs.dbl_str2num(initialFile)
updVars["InitialFile"] = initialFile
variousFuncs.tablePainter(initialFile, self.ui.tbl_OpenFile)
updVars["SelectedColumns"] = self.ui.tbl_OpenFile.selectedItems()
def showList(self):
tempSelected = updVars["SelectedColumns"]
variousFuncs.tablePainter(tempSelected, self.ui.tbl_ListShow)
while the variousFuncs file which contains various functions, looks something like this:
#!/usr/bin/python
import string, random, math
from PyQt4 import QtGui, QtCore
##
def dbl_str2num(lst):
""" This function transforms the items of the list "lst" from strings to numbers.
The list must be a nested list. """
for i in range(len(lst)):
for j in range(len(lst[0])):
lst[i][j] = eval(lst[i][j])
def tablePainter(someTwoDimLst, someTbl):
""" This function creates a QTableWidgetItem out of a 2-dimension nested list which contains numbers,
and then uses it to "paint" the someTbl object, which is a QTableWidget """
someTbl.setColumnCount(len(someTwoDimLst[0]))
someTbl.setRowCount(len(someTwoDimLst))
for i in range(len(someTwoDimLst)):
for j in range(len(someTwoDimLst[0])):
itemListShow = QtGui.QTableWidgetItem(QtCore.QString.number(someT woDimLst[i][j]), QtGui.QTableWidgetItem.Type)
someTbl.setItem(i, j, itemListShow)
currently, when I select certain columns from the tbl_OpenFile QTableWidget and try to display them in the tbl_ListShow one, the whole thing ends up with the error:
Traceback (most recent call last):
File "D:\Tutorial\main.py", line 52, in showList
itemListShow = QtGui.QTableWidgetItem(QtCore.QString.number(tempS elected[0][0]))
IndexError: list index out of range
which IMHO means that the line
updVars["SelectedColumns"] = self.ui.tbl_OpenFile.selectedItems()
returned nothing in the list (???)
Any help would be much appreciated.
Thank you all in advance,
Thomas
I'm new here and a newbie at the same time, so please don't be harsh...
I use Python with PyQt4 to develop a program reading a file with numbers and displaying them in a QTableWidget. So far so good.
What I have not accomplished yet, is to select certain columns from the QTableWidget and display them in another table. I would also like to store the selected tables' indexes in a global list.
My main.py file, the one that materializes the GUI and uses the derivative file from pyuic4 looks something like this:
from PyQt4 import QtGui, QtCore
from tstMain import Ui_tstMain
import sys, string
from os.path import isfile
from os import chdir
import variousFuncs
## Create a dictionary of updateable variables. These will be updated by the various
## functions of Class startGui
updVars = {"InitialFile": [], "SelectedColumns": []}
class startGui(QtGui.QMainWindow):
def __init__(self, parent = None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_tstMain()
self.ui.setupUi(self)
## create our slots here
QtCore.QObject.connect(self.ui.btn_SetWorkingDir, QtCore.SIGNAL("clicked()"), self.setWorkingDir)
QtCore.QObject.connect(self.ui.btn_OpenFile, QtCore.SIGNAL("clicked()"), self.openFile)
QtCore.QObject.connect(self.ui.btn_ListShow, QtCore.SIGNAL("clicked()"), self.showList)
def setWorkingDir(self):
wd = QtGui.QFileDialog(self)
currentDir = QtCore.QString(wd.getExistingDirectory())
self.ui.lnEdt_WorkingDir.setText(currentDir)
chdir(currentDir)
def openFile(self):
fd = QtGui.QFileDialog(self)
self.file = fd.getOpenFileName()
if isfile(self.file):
fileToOpen = open(self.file)
initialFile = [line.split() for line in fileToOpen.readlines()]
variousFuncs.dbl_str2num(initialFile)
updVars["InitialFile"] = initialFile
variousFuncs.tablePainter(initialFile, self.ui.tbl_OpenFile)
updVars["SelectedColumns"] = self.ui.tbl_OpenFile.selectedItems()
def showList(self):
tempSelected = updVars["SelectedColumns"]
variousFuncs.tablePainter(tempSelected, self.ui.tbl_ListShow)
while the variousFuncs file which contains various functions, looks something like this:
#!/usr/bin/python
import string, random, math
from PyQt4 import QtGui, QtCore
##
def dbl_str2num(lst):
""" This function transforms the items of the list "lst" from strings to numbers.
The list must be a nested list. """
for i in range(len(lst)):
for j in range(len(lst[0])):
lst[i][j] = eval(lst[i][j])
def tablePainter(someTwoDimLst, someTbl):
""" This function creates a QTableWidgetItem out of a 2-dimension nested list which contains numbers,
and then uses it to "paint" the someTbl object, which is a QTableWidget """
someTbl.setColumnCount(len(someTwoDimLst[0]))
someTbl.setRowCount(len(someTwoDimLst))
for i in range(len(someTwoDimLst)):
for j in range(len(someTwoDimLst[0])):
itemListShow = QtGui.QTableWidgetItem(QtCore.QString.number(someT woDimLst[i][j]), QtGui.QTableWidgetItem.Type)
someTbl.setItem(i, j, itemListShow)
currently, when I select certain columns from the tbl_OpenFile QTableWidget and try to display them in the tbl_ListShow one, the whole thing ends up with the error:
Traceback (most recent call last):
File "D:\Tutorial\main.py", line 52, in showList
itemListShow = QtGui.QTableWidgetItem(QtCore.QString.number(tempS elected[0][0]))
IndexError: list index out of range
which IMHO means that the line
updVars["SelectedColumns"] = self.ui.tbl_OpenFile.selectedItems()
returned nothing in the list (???)
Any help would be much appreciated.
Thank you all in advance,
Thomas