zhiyiYo / PyQt-Fluent-Widgets

A fluent design widgets library based on C++ Qt/PyQt/PySide. Make Qt Great Again.
https://qfluentwidgets.com
GNU General Public License v3.0
4.99k stars 464 forks source link

新组件提议:InputDialog(已实现完整功能) #862

Closed abcdesteve closed 2 months ago

abcdesteve commented 2 months ago

没用过PR就不直接提交了 先放效果 image image 应该还算符合fluent design 下面使用了EditableComboBox,可以手动传参,决定是否允许自行填写 实现代码

class InputDialog(MaskDialogBase, Ui_Input_dialog):
    def __init__(self):
        pass

    def run(self, parent, title: str, content: str, options: list[str] = [], editable: bool = True) -> str:
        '''
        Receive an optional argv `options` which will be shown in the `ComboBox`\n
        Return input txt
        '''
        super().__init__(parent)
        self.setupUi(self.widget)
        self.widget.setFixedSize(360, 220)
        FluentStyleSheet.DIALOG.apply(self)
        self.setWindowFlag(Qt.WindowType.WindowStaysOnTopHint, True)

        self.btn_cancel.setShortcut('Alt+N')
        self.btn_ok.setShortcut('Alt+Y')
        self.btn_cancel.clicked.connect(self.cancel)
        self.btn_ok.clicked.connect(self.ok)
        self.title.setText(title)
        self.content.setText(content)
        # 这是一个自用的工具集, 在最下面贴出了最小代码
        sltk.unique_add_items(self.comboBox, options, format_item=False)
        self.comboBox.setReadOnly(not editable)
        # self.show()
        self.exec()
        return self.comboBox.currentText() if self.status else None

    def cancel(self):
        self.status = False
        self.close()

    def ok(self):
        self.status = True
        self.close()

class sltk():
    def unique_add_items(widget: QComboBox | ComboBox | QListWidget | ListWidget, *items: str | list[str], format_item: bool = True):
        '''为ComboBox不重复地添加一个或多个item,同时格式化路径(如果可以)'''
        previous_items = [(widget.itemText(i) if type(widget) in [
                           QComboBox, ComboBox]else widget.item(i).text()) for i in range(widget.count())]
        if type(items[0]) == list:
            items = items[0]
        for i in items:
            if format_item:
                i = os.path.realpath(i)
            if i not in previous_items:
                widget.addItem(i)

ui文件 input_dialog.zip

rainzee commented 2 months ago

感觉这个还是用 MessageBox Base 让用户自己来实现比较好,WinUI 组件库中也只提供 ContentDialog