sh4rp3r
sh4rp3r

Reputation: 216

PyQt5 QFileDialog is not returning correct paths in ubuntu

I'm using this bit of code to open a file dialog and return the selected file names (PyQt5, Ubuntu)

QtWidgets.QFileDialog.getOpenFileNames(self, 'Open files', self.__target, self.__open_f)

But instead of getting this list:

['/home/python/Downloads/addresses.csv', '/home/python/Downloads/airtravel.csv']

I am getting this list:

['/run/user/1000/doc/9f194012/addresses.csv', '/run/user/1000/doc/885466d0/airtravel.csv']

here is my code:

import os
import sys
from mods import fixqt
from PyQt5 import QtWidgets
from PyQt5.QtGui import QIcon

from mods.csvdata import DataCSV
from mods.err_report import report_error
from mods.save_xl import save_excel_file
from ui.mainwindow import Ui_mwWCS

# this is the value of self.__target
home = os.path.expanduser("~/Desktop")
icon_path = os.path.join(os.path.join(os.path.dirname(os.path.realpath(__file__)), "ui"), "Icon.ico")
open_filter = "CSV files (*.csv)"
save_filter = "Excel Workbook (*.xlsx)"
input_data = DataCSV([])

class MainWindow(QtWidgets.QMainWindow):  # window = qtw.QMainWindow()
    def __init__(self, title="", mw_home="", op_filter="All files (*.*)", sv_filter="All files (*.*)", parent=None):
        super().__init__(parent)
        self.__title = title 
        self.ui = Ui_mwWCS() 
        self.ui.setupUi(self)  
        self.__target = mw_home
        self.__open_f = op_filter
        self.__save_f = sv_filter
        self.__excel_file = ""
        self.setWindowIcon(QIcon(icon_path))
        self.__input_data = DataCSV([])

    def __show_dialog(self):
        return QtWidgets.QFileDialog.getOpenFileNames(self, 'Open files', self.__target, self.__open_f)

    def __set_csv(self, lst):
        self.__input_data.set_files_list(lst)
        # print(lst)
        self.__input_data.open_csv_files()
        self.__input_data.exception_entries()
        self.__input_data.set_boxes_number()
        self.__input_data.set_plates_number()

    def on_add_clicked(self):
        try:
            list_names, _ = self.__show_dialog()

            self.ui.lstInput.addItems(list_names)
            self.__set_csv(list_names)

        except Exception as e:
            report_error("Error occurred (ADD)", e)

Can you please help on how can I get the proper filenames?

Update: trying my code in a terminal worked fine, could it be a problem related to pyCharm?

screenshot from python terminal

Upvotes: 6

Views: 1511

Answers (3)

nae9on
nae9on

Reputation: 369

I am using PyCharm and I had the same problem. The option "DontUseNativeDialog" as suggested by @musicamante also solved the problem for me.

QtWidgets.QFileDialog.getOpenFileName(parent=self, options=QtWidgets.QFileDialog.DontUseNativeDialog)

Upvotes: 3

Rishabh Bhardwaj
Rishabh Bhardwaj

Reputation: 338

Same issue happening for me aswell, finally i figured out the curprit is pycharm (in my case), try run your code in terminal and you will see it works fine. and after snap package your application it will also run smooth. So this was not a problem for me now.

Upvotes: 2

sh4rp3r
sh4rp3r

Reputation: 216

@musicamante, thank you for your help. The answer lays with DontUseNativeDialog if I'm running my code with PyCharm. Running it outside PyCharm, that flag isn't required.

Upvotes: 4

Related Questions