2010-03-02 198 views
1

在Qt中,我想定製QT Symbian中的listview。所以爲了定製目的,我爲listview設置了委託。我也處理繪畫事件..問題是文字顏色。錯誤代碼

從列表視圖中選擇項目時,我沒有得到默認的文本顏色。

,即下面的示例代碼,我有兩個字符串即「項」(主字符串)和「測試」(子字符串)

主弦與默認選擇的顏色繪製,但子字符串不能用與主要字符串相同的顏色繪製。

我認爲我們需要處理選擇和獲取調色板顏色..我沒有得到確切的方式。

請給我解決方案。

這裏是代碼

class SettingDeligate : public QStyledItemDelegate 
{ 
//  Q_OBJECT 

    public: 
     explicit SettingDeligate(QObject* parent = 0); 
     virtual ~SettingDeligate(); 
     enum ItemDataRole {SubheaderTextRole = Qt::UserRole + 100}; 
     QSize sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const; 
     void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const; 
}; 

SettingDeligate::SettingDeligate(QObject* parent) 
     : QStyledItemDelegate(parent) 
{ 
} 

SettingDeligate::~SettingDeligate() 
{ 
} 

QSize SettingDeligate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const 
{ 
     QIcon icon = qvariant_cast<QIcon>(index.data(Qt::DecorationRole)); 
     QString line1 = index.data(Qt::DisplayRole).toString(); 
     QString line2 = index.data(SubheaderTextRole).toString(); 

     int textW = qMax(option.fontMetrics.width(line1), option.fontMetrics.width(line2)); 
     //QSize iconSize = icon.actualSize(option.decorationSize); 
     QSize iconSize(27,19); 
     return QSize(qMax(textW, iconSize.width()) + 4, 
        iconSize.height() + 2 + option.fontMetrics.lineSpacing() * 2 + 4);  
} 

void SettingDeligate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const 
{ 
     QStyledItemDelegate::paint(painter,option,index); 
     QString headerText(qvariant_cast<QString>(index.model()->data(index, SubheaderTextRole))); 
      QPoint point1 (option.rect.topLeft()); 
     painter->drawText(point1.x()+25,point1.y()+25,headerText); 
} 
int main(int argc, char *argv[]) 
{ 
     QApplication a(argc, argv); 

     QListView *MylistView = new QListView(); 
     MylistView->setViewMode(QListView::ListMode); 

     MylistView->setMovement(QListView::Free); 
     MylistView->setItemDelegate(new (SettingDeligate)); 

     MylistView->setSelectionMode(QAbstractItemView::SingleSelection); 


     QStandardItemModel *model = new QStandardItemModel(); 
     MylistView->setModel(model); 


       QStandardItem *item = new QStandardItem("item1"); 
       item->setData("Test1", SettingDeligate::SubheaderTextRole); 
       model->appendRow(item); 

      QStandardItem *item1 = new QStandardItem("item2"); 
       item1->setData("Test2", SettingDeligate::SubheaderTextRole); 
      model->appendRow(item1); 

      QStandardItem *item2 = new QStandardItem("item3"); 
      item2->setData("Test3", SettingDeligate::SubheaderTextRole); 
      model->appendRow(item2); 

       QDesktopWidget* desktopWidget = QApplication::desktop(); 
       //get client rect. 
       QRect clientRect = desktopWidget->availableGeometry(); 

       MylistView->setGeometry(clientRect); 
       MylistView->show(); 
       return a.exec(); 
} 

請幫我

感謝ü

+0

你怎麼禁用Q_OBJECT宏? – bgs 2010-03-02 17:39:09

+0

@Karol,Q_OBJECT宏只有當我們使用QT中的使用信號和插槽時才需要..對於元對象.. 因爲我沒有在這裏使用任何信號插槽..所以它不是必需的 – Naruto 2010-03-03 04:55:28

回答

0

這是symbian Qt 4.6.2中的一個bug。

希望它會在下一版本中得到修復..

FYI ....

Check out here

0

試試這個 -

void SettingDeligate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const 
{ 
    QStyledItemDelegate::paint(painter,option,index); 
    QString headerText(qvariant_cast<QString>(index.model()->data(index, SubheaderTextRole))); 
    QPoint point1 (option.rect.topLeft()); 
    QPalette::ColorGroup colorGroup = QPalette::Normal; 
    if(!option.state.testFlag(QStyle::State_Enabled)) 
     colorGroup = QPalette::Disabled; 
    else if (!option.state.testFlag(QStyle::Active)) 
     colorGroup = QPalette::Inactive; 

    QPalette::ColorRole colorRole = QPalette::Text; 
    if(option.state.testFlag(QStyle::State_Selected)) 
     colorRole = QPalette::HighlightedText; 
    if(option.status.testFlag(QStyle::State_Editing)) 
     colorRole = QPallete::Text; 
    painter->setPen(option.palette.color(colorGroup, colorRole)); 
    painter->drawText(point1.x()+25,point1.y()+25,headerText); 
} 

的狀態,以顏色組/ colorrole映射是從文本繪製用於CE_ItemViewItem元素的QCommonStyle :: DrawControl部分。

+0

嘿,我越來越問題,當項目被選中.. 我沒有得到默認的顏色來繪製當選擇的項目 – Naruto 2010-03-03 07:10:33