0

使用 pytest-qt 的qtbot夹具,我希望测试代码单击QFileDialog. 但是我实际上很难以编程方式获取这些项目。这是一个MRE:

import sys
from PyQt5 import QtWidgets 

def show_desc_comps(comp, depth=0): 
    indent = '  ' * depth
    for child in comp.children():
        print(f'{indent}comp {child.__class__} objectName |{child.objectName()}|')
        show_desc_comps(child, depth+1)

class MyFd(QtWidgets.QFileDialog):
    def __init__(self, parent):
        super().__init__(parent)
        self.setWindowTitle('Find files to click MRE')

        # I am running a "non-native" dialog: for some reason I don't need this 
        # line on my machine, but you might...
        # self.setOptions(QtWidgets.QFileDialog.DontUseNativeDialog)

        self.show()
        
        show_desc_comps(self)
        
        file_list_view = self.findChild(QtWidgets.QListView, 'listView')
        print(f'file_list_view {file_list_view}') # QListView
        
        file_list_model = file_list_view.model()
        print(f'file_list_model {file_list_model}') # QFileSystemModel
        
        for i_row in range(file_list_model.rowCount()):
            for j_col in range(file_list_model.columnCount()):
                qm_index = file_list_model.index(i_row, j_col)
                print(f'  row {i_row}, col {j_col}: |{file_list_model.data(qm_index)}|')
        
        item_delegate = file_list_view.itemDelegate() 
        print(f'item_delegate {item_delegate}') # QStyledItemDelegate

app = QtWidgets.QApplication([])
dlg = QtWidgets.QDialog()
MyFd(dlg)
sys.exit(app.exec())

我的操作系统是 W10。

当我运行上面的文件时,据说文件列表中有两行 4 列:“C:”和“D:”。这不仅与我在我面前看到的不相符(CWD 中有很多文件和目录),而且,如果我QFileDialog尽可能地向上导航,实际上有 4 个驱动器,而不仅仅是2. 不仅如此,更令人费解的是,C: 和 D: 的“最后修改”日期(第 3 列)与此处打印的不匹配。

我不太确定如何QStyledItemDelegate呈现用户看到的内容:这可能与它有关:这些可能只是文件和目录的“图像”,而不是适当的项目。在那种情况下,我想我需要找出这些图像占用的屏幕空间,然后qtbot在其中单击。

4

0 回答 0