2012-02-03 21 views
1

アイテムの代理人のテキストの上にマウスがある場合、マウスアイコンを変更するにはどうすればよいですか? 私はこの部分を持っていますが、マウスポインタを変更する例は見つかりません。QStyledItemDelegateでマウスの上にマウスを置くと、マウスポインタをどのように変更できますか?

私には何が欠けていますか?

void ListItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, 
          const QModelIndex &index) const 
    { 
    if (index.isValid()) 
     { 
     int j = index.column(); 
     if(j==4) 
     { 
     QString headerText_DisplayRole = index.data(Qt::DisplayRole).toString() ; 
     QApplication::style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &option, painter); 

     QFont font = QApplication::font(); 
     QRect headerRect = option.rect; 
     font.setBold(true); 
     font.setUnderline(true); 
     painter->setFont(font); 
     painter->setPen(QPen(option.palette.brush(QPalette::Text), 0)); 
     const bool isSelected = option.state & QStyle::State_Selected;  
     if (isSelected) 
     painter->setPen(QPen(option.palette.brush(QPalette::HighlightedText), 0)); 
     else 
     painter->setPen(QPen(option.palette.brush(QPalette::Text), 0)); 
     painter->save();  
     painter->drawText(headerRect,headerText_DisplayRole); 
     painter->restore(); 
     bool hover = false; 
     if (option.state & QStyle::State_MouseOver) 
     { 
      hover = true; 
     } 
     if(hover) 
     { 
     // THIS part i missing , how detect when mouse is over the text 
     // and if its over how to change the icon of the mouse? 
     } 


     } 
     else 
     { 
     QStyledItemDelegate::paint(painter, option, index); 
     } 
    } 
    } 

答えて

8

まずマウスの位置が必要です。あなたはQCursor::pos静的関数を使ってそれを得ることができます。

QPoint globalCursorPos = QCursor::pos(); 

結果はグローバルな画面座標であるため、ウィジェット座標で変換する必要があります。デリゲートを使用しているウィジェットがmyWidgetであるとしましょう。翻訳を行うためには、最後にあなたがビューポートでのアイテムのモデルインデックスを返しQAbstractItemViewからindexAtポイントを座標必要としているQWidget

QPoint widgetPos = myWidget->mapFromGlobal(globalCursorPos); 

mapFromGlobal機能が必要になります。

myViewは、使用しているビューの名前である場合、現在位置のインデックスです:あなたは正確にするために、viewport()が必要になる場合があり

QModelIndex currentIndex = myView->itemAt(widgetPos); 

注意してください。この場合、必要となるグローバル座標にマッピングするために、最後に

QPoint viewportPos = myWidget->viewport()->mapFromGlobal(globalCursorPos); 

をあなたはitemAtから返されたインデックスは、あなたのpaint機能のインデックスと同じであるかどうかを確認する必要があります。はいの場合はカーソルを目的のカーソルに変更します。それ以外の場合はデフォルトのカーソルを復元します。

if(hover) 
{ 
    QPoint globalCursorPos = QCursor::pos(); 
    QPoint viewportPos = myWidget->viewport()->mapFromGlobal(globalCursorPos); 
    QModelIndex currentIndex = myView->itemAt(widgetPos); 

    if (currentIndex == index) 
     QApplication::setOverrideCursor(QCursor(Qt::PointingHandCursor)); 
    else 
     QApplication::restoreOverrideCursor(); 
} 

これは基本的な考え方です。もう1つの選択肢はmouseMoveEventを再実装し、そこに機能を実装することです。詳細についてはthis blog postを確認してください。

+0

ありがとう、ありがとう、ありがとう!ブログの投稿は検索の2日後に私を救った – user63898

関連する問題