-->

PyQt5 QFileDialog is not returning correct paths i

2020-04-30 17:53发布

问题:

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?

回答1:

@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.