Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8853881
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:45:19+00:00 2026-06-14T13:45:19+00:00

I am building this application that defaults to a specific location where it will

  • 0

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_()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T13:45:20+00:00Added an answer on June 14, 2026 at 1:45 pm

    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.

    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)
            self.filesTable.itemClicked.connect(self.handleFolderChecked)
        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_()
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application that uses the .NET framework 3.5. I am building this
I am building a C++ application that uses Intel's IPP library. This library is
Ok, this is downright bizarre. I am building a web application that relies on
I am building a web application (in Django, though this isn't a Django-specific question).
I'm building an application that will require CouchDB's mobile syncing feature. So for each
I am building an Android application that requires OAuth. I have all the OAuth
I am building an WPF Prism MVVM application. This application will contain a lot
I'm building an application that will take an image of a single person's whole
I'm building an web application that I want users to have specific permissions to
I am building a Silverlight 3 application that uses .NET RIA Services. This application

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.