2011-04-30 87 views
2

我正在使用sqlmodel在qtableview中顯示來自sql server的一些信息。PyQt Paint自定義日期格式

我已經設置了一個自定義委託來處理數據的編輯。

我想在一個特定的格式來顯示我的日期,當他們表第一次加載日期顯示爲這樣: 20011-04-30 但是,當我修改的日期和點擊關閉單元的接受日期則顯示像: 30/04/2011 這是如何將其存儲在數據庫中,我希望如何顯示它開始,我不知道爲什麼它編輯後更改格式。

我猜我必須重新實現該列的paint方法我已經做了一些類似的進度條,但我不知道如何去做文本。

這是我的代表,因爲它代表着,注意是有一些編輯器設置爲其他列,但我的主要問題只涉及如何正確顯示日期。

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) 

謝謝,湯姆。

回答

1

爲什麼editor.setDisplayFormat(「yyyy/MM/dd」)被註釋掉了?不應該照顧格式?

+0

這就是爲什麼我在第一個地方添加它我認爲這可以解決它,但它似乎沒有區別,它在內或外,或設置爲不同的格式,我仍然結束了相同的結果。 – supertom44 2011-04-30 15:51:03