I am building this application that defaults to a specific location where it will table view all the folders in that folder and the number of files in those folders. Despite the fact that you don’t have the config file, it should just open up your C or home directory. The idea is that I would place a check mark in those folders who’s files I wanted to interact with. I basically have a print statement in handleFolderChecked to let me know that I have selected the right item, and it works.
The issues come about when I open File -> open from the menu bar and then hit the Select Folder button on the window that pops up, with or without selection, I end up with a new display in my table view, however, if I click a checkmark in any item the print statement will occur the number of items I have opened the Select Folder window and that is not what I want.
Also something that is bothersome, when I hit cancel on the Select Folder window the program will dump to the home directory, anyone know how I make it stay in the directory that it is in?
from PyQt4 import QtCore, QtGui
from os import getcwd
from sys import platform
from sys import argv
from lxml import etree as ET
class Anaylzer(QtGui.QMainWindow):
try:
tree = ET.parse(''.join([getcwd(),'/config.xml']))
if 'win' in platform:
save_path = tree.find('SAVE').find('win').text
if 'linux' in platform:
save_path = tree.find('SAVE').find('linux').text
del tree
except IOError:
if 'win' in platform:
save_path = "C:/"
if 'linux' in platform:
save_path = "/home"
def __init__(self, parent=None):
super(Anaylzer, self).__init__(parent)
self.setWindowTitle("Anaylzer")
self.resize(700,300)
self.statusBar()
self.pushWidgets()
self.displayTableItems()
'''Create objects'''
def createFilesTable(self):
self.filesTable = QtGui.QTableWidget(0, 2)
self.filesTable.setSelectionBehavior(QtGui.QAbstractItemView.SelectRows)
self.filesTable.setHorizontalHeaderLabels(("Folder", "Files"))
self.filesTable.horizontalHeader().setResizeMode(0, QtGui.QHeaderView.Stretch)
self.filesTable.verticalHeader().hide()
self.filesTable.setShowGrid(False)
def createButton(self, text, member):
button = QtGui.QPushButton(text)
button.clicked.connect(member)
return button
'''Actions'''
def open(self):
directory = QtGui.QFileDialog.getExistingDirectory(self, "Select Folder",
Anaylzer.save_path)
Anaylzer.save_path = ''.join([str(directory),"\\"])
self.displayTableItems()
def displayTableItems(self):
from os import listdir
self.filesTable.setRowCount(0)
folders = listdir(Anaylzer.save_path)
if folders:
for fl in folders:
folder_name_item = QtGui.QTableWidgetItem(fl)
folder_name_item.setFlags(QtCore.Qt.ItemIsUserCheckable | QtCore.Qt.ItemIsEnabled)
folder_name_item.setCheckState(QtCore.Qt.Unchecked)
try:
folder_files_names = listdir(''.join([Anaylzer.save_path,fl]))
folder_files_count = QtGui.QTableWidgetItem(str(len(folder_files_names)))
folder_files_count.setFlags(QtCore.Qt.ItemIsEnabled)
folder_files_count.setTextAlignment(QtCore.Qt.AlignCenter)
row = self.filesTable.rowCount()
self.filesTable.insertRow(row)
self.filesTable.setItem(row, 0, folder_name_item)
self.filesTable.setItem(row, 1, folder_files_count)
except WindowsError: pass
self.filesTable.itemClicked.connect(self.handleFolderChecked)
def handleFolderChecked(self, folder_name_item):
if folder_name_item.checkState() == QtCore.Qt.Checked:
print '{s} Checked'.format(s="".join([Anaylzer.save_path,str(folder_name_item.text())]))
def createActions(self):
self.openAct = QtGui.QAction("&Open...", self, shortcut=QtGui.QKeySequence.Open,
statusTip="Open folder", triggered=self.open)
self.exitAct = QtGui.QAction("E&xit", self, shortcut="Ctrl+Q",
statusTip="Exit the application", triggered=self.close)
def createMenus(self):
self.fileMenu = self.menuBar().addMenu("&File")
self.fileMenu.addAction(self.openAct)
self.fileMenu.addAction(self.exitAct)
'''Push widgets'''
def pushWidgets(self):
'''create'''
self.createActions()
self.createMenus()
self.createFilesTable()
'''put on window'''
widget = QtGui.QWidget()
self.setCentralWidget(widget)
vbox = QtGui.QVBoxLayout()
vbox.addStretch(1)
vbox.addWidget(self.filesTable)
widget.setLayout(vbox)
if __name__ == '__main__':
app = QtGui.QApplication(argv)
window = Anaylzer()
window.show()
app.exec_()
Ok I figured it out, apparently methods that are responsible for adjusting QtGui.QTableWidgetItem cannot also have a *.connect line in them, by merely moving self.filesTable.itemClicked.connect(self.handleFolderChecked) out of def displayTableItems(self) and putting it into a method that would only be called once, but after the table is constructed, like at the end of def createFilesTable(self) or anywhere outside of, but after that method, and in an area where the program would only run across it once, then it works without the above mentioned error. I’ll post the final code so anyone can see how the simple line move solves my issue.
I still don’t know why the cancel button in *QtGui.QFileDialog.getExistingDirectory(self, “Select Folder”,
Anaylzer.save_path)* dumps me back to a root directory, oh well.