1

我创建了 CustomItemDelegateQStyledItemDelegate并使用该paint()方法更好地查看我的QListView.

如果我点击一个项目,option.state从来没有State_Selected,这是为什么呢?
我有一个选择模型,单行,选择矩形是可见的。

qDebug只打印出这些:

QStyle::State( "Active | Enabled" ) 
QStyle::State( "Active | Enabled | MouseOver" ) 

void SyrupItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{


   QRect rect = option.rect;
   qDebug() << option.state;

   if (option.state & QStyle::State_Selected)
   {


     painter->drawRoundedRect(option.rect,5,5);
     painter->setPen(QPen(QPalette::HighlightedText ) );

     if (option.state & QStyle::State_Active)
     {
        painter->setBrush(QBrush(QPalette().highlight()));
     } else
            {
                //painter->setBrush(QBrush(QPalette().color(QPalette::Inactive,
                //QPalette::Highlight)));
                QLinearGradient gradient(0, 0, 0, 100);
                gradient.setColorAt(0.0, QColor(0,0,230));
                gradient.setColorAt(1.0, QColor(250,250,250));
                painter->setBrush(gradient);
            }



   } else
        painter->setPen(QPen(QPalette::Text));

   if ( !index.isValid() )
   return;
    int row = index.row();

  //  painter->save();
   // painter->setRenderHint(QPainter::Antialiasing,true);


    QString res =   index.sibling(row,SyrupsSQLModel::SYRUP_NM_COL_INDEX).data().toString();

    QRectF rc(rect);

    rc.setTop(rc.top()+ PADDING);
    rc.setLeft(rc.left()+ 2* PADDING +  IMG_WIDTH);

    QFont font = option.font;
    font.setPointSize(font.pointSize()+4);
    painter->setFont(font);

    painter->drawText(rc,res);

    res = index.sibling(row,SyrupsSQLModel::SYRUP_GRP_COL_INDEX).data().toString().toLower();

    rc.setTop(rect.top()+PADDING );
    rc.setLeft(rect.left()+PADDING );
    painter->drawPixmap(rc.topLeft(),QIcon(":/prodgrp/"+res).pixmap(QSize(IMG_WIDTH,IMG_HEIGHT)));

    //SyrupsSQLModel::FORMULA_COL_INDEX:
    //SyrupsSQLModel::SYRUP_ID_COL_INDEX:
    //Painter->restore();

}

我正在使用 Qt 5.0.2 32bit (Win)。

4

1 回答 1

0

问题出在我重新实现 editorEvent() 函数的委托类中。我注释掉了这些部分并且它有效。

于 2013-07-18T11:45:17.327 回答