Skip to main content
 首页 » 编程设计

python之PyQt Paint 自定义日期格式

2025年05月04日8freeliver54

我正在使用 sqlmodel 在 qtableview 中显示来自 sql server 的一些信息。

我已经设置了一个自定义委托(delegate)来处理数据的编辑。

我想以特定格式显示我的日期,当他们的表格首次加载时,日期显示如下: 20011-04-30 但是当我编辑日期并单击单元格以接受日期时,显示如下: 2011 年 4 月 30 日 它是如何存储在数据库中的,以及我希望它如何开始显示,我不知道为什么它在编辑后会改变格式。

我猜我必须为该列重新实现 paint 方法我已经用进度条做了类似的事情,但我不知道如何为文本做。

这是我的委托(delegate),请注意,有些编辑器已为其他栏目设置,但我的主要问题仅与如何正确显示日期有关。

import sys 
import os 
 
from PyQt4.QtCore import * 
from PyQt4.QtGui import * 
from PyQt4.QtSql import * 
 
PROJECTID, PROJECTTITLE, CLIENTID, DEADLINE, PROGRESS, ROOT = range(6) 
 
class projectsDelegate(QSqlRelationalDelegate): 
     def __ini__(self, parent = None): 
    super(projectsDelegate, self).__init__(parent) 
 
def createEditor(self, parent, option, index): 
    if index.column() == DEADLINE: 
        editor = QDateEdit(parent) 
        #editor.setDisplayFormat("yyyy/MM/dd") 
        editor.setMinimumDate(QDate.currentDate()) 
        editor.setCalendarPopup(True) 
        return editor 
    elif index.column() == PROGRESS: 
        editor = QSpinBox(parent) 
        editor.setRange(0, 100) 
        editor.setSingleStep(5) 
        editor.setSuffix("%") 
        return editor 
    elif index.column() == ROOT: 
        editor = QFileDialog(parent) 
        editor.setFileMode(QFileDialog.Directory) 
        editor.setOptions(QFileDialog.ShowDirsOnly) 
        editor.setFixedSize(400, 400) 
        editor.setWindowTitle("Select A Root Folder For The Project") 
        return editor 
    else: 
        return QSqlRelationalDelegate.createEditor(self, parent, option, index) 
 
def setEditorData(self, editor, index): 
    if index.column() == DEADLINE: 
        text = index.model().data(index, Qt.DisplayRole).toDate() 
        editor.setDate(text) 
    elif index.column() == PROGRESS: 
        prog = index.model().data(index, Qt.DisplayRole).toInt()[0] 
        editor.setValue(prog) 
    elif index.column() == ROOT: 
        root = index.model().data(index, Qt.DisplayRole).toString() 
        editor.setDirectory(os.path.dirname(str(root))) 
        screen = QDesktopWidget().screenGeometry() 
        size = editor.geometry() 
        editor.move((screen.width() - size.width()) / 2, (screen.height() - size.height()) / 2) 
    else: 
        QSqlRelationalDelegate.setEditorData(self, editor, index) 
 
def setModelData(self, editor, model, index): 
    if index.column() == DEADLINE: 
        model.setData(index, QVariant(editor.date())) 
    elif index.column() == PROGRESS: 
        model.setData(index, QVariant(editor.value())) 
    elif index.column() == ROOT: 
        model.setData(index, QVariant(editor.directory().absolutePath())) 
    else: 
        QSqlRelationalDelegate.setModelData(self, editor, model, index) 
 
def paint(self, painter, option, index): 
    if index.column() == PROGRESS: 
        bar = QStyleOptionProgressBarV2() 
        bar.rect = option.rect 
        bar.minimum = 0 
        bar.maximum = 100 
        bar.textVisible = True 
        percent = index.model().data(index, Qt.DisplayRole).toInt()[0] 
        bar.progress = percent 
        bar.text = QString("%d%%" % percent) 
        QApplication.style().drawControl(QStyle.CE_ProgressBar, bar, painter)  
    else: 
        QSqlRelationalDelegate.paint(self, painter, option, index) 
 
def sizeHint(self, options, index): 
    if index.column() == PROGRESS: 
        return QSize(150, 30) 
    elif index.column() == ROOT: 
        return QSize(400, 800) 
    else: 
        return QSqlRelationalDelegate.sizeHint(self, options, index) 

谢谢,汤姆。

请您参考如下方法:

为什么 editor.setDisplayFormat("yyyy/MM/dd") 被注释掉了?这不应该处理格式吗?