Question

For two days I am suffering with a strange error, I can not understand what the problem is asking you to help. I have a unique feature.

def find_config(self, path):
    dic = list(path.split('/'))
    print dic
    size = len(dic)
    print '/'.join(dic)

When I run it within the class constructor, it works fine, but when I run it inside the event handler button, it hangs on the join function.

constructor:

class MainWindow(QtGui.QMainWindow, Ui_MainWindow):
    def __init__(self):
            QMainWindow.__init__(self)
            self.setupUi(self)
            self.filepath = ""
            self.action_Open.activated.connect(self.file_open_func)

            print self.find_config('/home/juster/asdfa/main.c')

the handler button:

def file_open_func(self, path = 0):

    try:
        self.filepath = 0;
        if not path:        
            self.filepath = QFileDialog.getOpenFileName(self, 'Open file', self.rootpath, "C/C++ (*.c);; All (*.*);; Makefile (makefile)")  
        else:
            self.filepath = path

        print self.find_config(self.filepath)
        f = open(self.filepath, 'a+')

See what else I bring enough to the terminal variable dic

function find_config from constuctor:

['', 'home', 'juster', 'asdfa', 'main.c']

function find_config from handler:

[PyQt4.QtCore.QString(u''), PyQt4.QtCore.QString(u'home'), PyQt4.QtCore.QString(u'juster'), PyQt4.QtCore.QString(u'asdfa'), PyQt4.QtCore.QString(u'main.c')]

This is magic?

Was it helpful?

Solution

It looks like your path string when called from the handler is not a regular Python str, but rather an instance of the QT class QString. This appears to work for split, but not for join. I imagine you'll find the problem goes away if you convert it to a regular string with str.

def find_config(self, path):
    dic = list(str(path).split('/')) # added str() call to this line
    print dic
    size = len(dic)
    print '/'.join(dic)

Note that your dic variable has a rather misleading name. It's a list, not a dictionary, so calling it dic is asking for confusion (though the list call when it is created seems unnecessary). I'm also not sure what this function does. It seems to split a string, then rejoin it exactly as it was.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top