Domanda

I created CustomItemDelegate from QStyledItemDelegate and i'm using the paint() method to give a better look to my QListView.

If I click on an item, option.state never has State_Selected, why is that?
I have a selection model, single, row, and the selection rectangle is visible.

qDebug only prints out these:

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();

}

I'm using Qt 5.0.2 32bit (Win).

È stato utile?

Soluzione

The problem was in the delegate class I reimplemented editorEvent() function badly. I commented out that sections and it works.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top