Stories

Detail Return Return

在PySide6/PyQt6的開發框架中,增加對錶格多種格式錄入的處理,以及主從表的數據顯示和保存操作。 - Stories Detail

在PySide6/PyQt6的開發框架中, 為了方便對錶格數據的快速錄入,有時候包括多種錄入的類型,包括文本框、數字格式(整數、小數)、日期、時間、下拉列表、自定義彈出框、單選框組、百分比、金額、顏色、圖片、內置圖標等多樣化的處理需要,本篇就是基於此需求,在PySide6/PyQt6的開發框架中,增加對錶格多種格式錄入的處理,以及主從表的數據顯示和保存操作。

PySide6/PyQt6的開發框架主界面支持多文檔管理,可包括菜單欄、工具欄、內容區和狀態欄等,內容區以選項卡方式展示多個窗口,如下所示。

image

常規的編輯界面如用户界面,雙擊列表彈出展示,如下所示。

image

主從表展示界面如下所示。

image

主從表直接編輯界面,在彈出對話框中進行編輯主表信息和明細記錄,明細記錄通過表格方式直接錄入,方便各種類型的數據錄入處理。

 

1、在PySide6/PyQt6的開發框架中表格的數據錄入

我們先來看看測試案例,查看下錶格中多種格式錄入的效果

image

視頻效果如下所示

如我們在產品報價單中進行主從表編輯數據的時候,界面如下所示,其中產品信息通過彈出自定義產品列表進行選擇。

image

其他如產品類型、產品規格、產品型號、標準單位、產品尺寸等通過綁定系統字典類型,作為下拉列表的數據源。

image

上面就是實際的報價單的界面錄入,可以通過自定義的對話框選擇,也可以直接錄入文本,還可以通過綁定字典類型獲得下拉列表選擇等處理操作。

 

2、表格數據直接錄入的委託類處理

在PySide6/PyQt6中,對於 QTableView 的定製化輸入,是通過繼承 QStyledItemDelegate 來實現定製化的表格單元格輸入或者顯示的。如我們自定義類如下所示。

class CustomDelegate(QStyledItemDelegate):

我們讓它支持的類型包括:

  •     - text: 普通文本 (QLineEdit)
  •     - int: 整數 (QSpinBox)
  •     - double: 浮點數 (QDoubleSpinBox)
  •     - date: 日期 (QDateEdit)
  •     - combo: 下拉選擇 (QComboBox)
  •     - check: 複選框 (直接顯示)
  •     - radio: 單選按鈕組 (QRadioButton)
  •     - slider: 滑動條 (QSlider)
  •     - multiline: 多行文本 (QTextEdit)
  •     - password: 密碼文本 (QLineEdit)
  •     - percent: 百分比 (QDoubleSpinBox)
  •     - currency: 貨幣 (QDoubleSpinBox)
  •     - time: 時間 (QTimeEdit)
  •     - datetime: 日期時間 (QDateTimeEdit)
  •     - color: 顏色選擇 (QPushButton)
  •     - icon: 圖標選擇 (QPushButton)
  •     - bitmap: 位圖選擇 (QPushButton)
  •     - custom: 自定義不可編輯控件,同時觸發 customTriggered 信號,傳出單元格索引和字段名稱

最終它的配置示例如下代碼所示。

    config = {
        "name": {"type": "text"},
        "age": {"type": "int", "min": 0, "max": 120},
        "score": {"type": "double", "min": 0, "max": 1000, "decimals": 3, "step": 0.01},
        "birthday": {"type": "date", "format": "yyyy-MM-dd"},
        "tag": {"type": "combo", "items": ["選項A", "選項B", "選項C"]},
        "married": {"type": "check", "true": "是", "false": "否"},
        "gender": {"type": "radio", "items": ["男", "女", "未知"], "width": 180}, 
        "score_2": {"type": "slider", "min": 0, "max": 100, "step": 1},
        "description": {"type": "multiline"},
        "password": {"type": "password"},
        "percent": {"type": "percent", "min": 0, "max": 100, "decimals": 2, "step": 0.1},
        "currency": {"type": "currency", "min": 0, "max": 1000000, "decimals": 2, "step": 0.1},
        "time": {"type": "time", "format": "HH:mm:ss"},
        "datetime": {"type": "datetime", "format": "yyyy-MM-dd HH:mm:ss"},
        "color": {"type": "color", "format": "color"},
        "icon": {"type": "icon", "default": "SP_ArrowDown"},
        "bitmap": {"type": "bitmap", "default": "bitmap.png"},
    }  
    view = QTableView()
    view.setModel(model)
    delegate = CustomDelegate(config, view)
    view.setItemDelegate(delegate)

同時我們為了支持自定義的列表對話框選擇,那麼我們通過觸發信號來獲得對應的事件處理即可,如下所示。

        #自定義單元格編輯事件
        self.delegate.customTriggered.connect(self.on_custom_triggered)
        
       def on_custom_triggered(self, index: QModelIndex, field_name: str):
        """自定義單元格編輯事件"""
        # print(f"on_custom_triggered: index={index}, field_name={field_name}")
        if field_name == "productno":
            # 彈出選擇產品對話框
            dlg = FrmProductSelect(self)
            if dlg.exec() == QDialog.DialogCode.Accepted:
                info = dlg.select_product
                if info is not None:
                    # print(f"選擇的產品信息:{info}")
                    row = index.row()
                    self.sub_table_model.SetValueByKey(row, "productno", info.productno)
                    
                    # #同步更新產品名稱/條形碼/規格/型號/單位/顏色/尺寸等
                    self.sub_table_model.SetValueByKey(row, "productname", info.productname)
                    ....
                    # 同步更新數量
                    ....
                    # 同步更新金額小結
                    ....

            dlg.deleteLater()

通過對自定義委託類中的 信號對象 customTriggered 進行監聽,我們就可以獲得用户觸發選擇某個單元格的事件,並可以通過彈出自定義對話框獲取列表選擇,並把數據寫回到對應單元格中即可。

而對於指定系統字典類型,作為下拉列表的操作,我們只需要設置字段類型為combo或者radio,並動態設置字典類型綁定即可。

        #表格單元格的編輯控件配置, 動態指定字典
        config = {
            "orderno": {"type": "text"},
            "quantity": {"type": "int", "min": 0, "max": 1000},
            "saleprice": {"type": "double", "min": 0, "decimals": 3, "step": 0.01},
            "subamout": {"type": "double", "min": 0, "decimals": 3, "step": 0.01},
            "expiredate": {"type": "date", "format": "yyyy-MM-dd"},
            "note": {"type": "multiline"},
            
            "productno": {"type": "custom"},
            "producttype": {"type": "combo"},
            "model": {"type": "combo"},
            "specification": {"type": "combo"},
            "unit": {"type": "combo"},
            "color": {"type": "combo"},
            "productsize": {"type": "radio"},
        }

在窗體的初始化函數中,指定字典類型即可。

    async def init_dict_items(self):
        """初始化字典數據"""
        await self.txtOrderStatus.bind_dictType("報價單狀態")
        
        #設置單元格的下拉列表為指定字典類型
        await self.delegate.SetEditor_DictType("producttype", "產品類型")
        await self.delegate.SetEditor_DictType("model", "產品型號")
        await self.delegate.SetEditor_DictType("specification", "產品規格")
        await self.delegate.SetEditor_DictType("unit", "產品標準單位")
        await self.delegate.SetEditor_DictType("color", "產品顏色")
        await self.delegate.SetEditor_DictType("productsize", "產品尺寸")       

image    image

image

image

最終界面可以實現數據表格的直接編輯處理,可以再常規的表中進行編輯,也可以在主從表的編輯界面中進行數據的快速錄入。

image

user avatar u_17037082 Avatar u_17400586 Avatar u_17353607 Avatar u_14540126 Avatar lizhuo6 Avatar itwhat Avatar ligaai Avatar shumile_5f6954c414184 Avatar daqianduan Avatar xingchendahai_68d7dff410962 Avatar tssc Avatar yejianfeixue Avatar
Favorites 67 users favorite the story!
Favorites

Add a new Comments

Some HTML is okay.