2012-02-03 5 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(); 
} 

이것은 기본적인 아이디어입니다. 또 다른 옵션은 mouseMoveEvent을 다시 구현하고 거기에 기능을 구현하는 것입니다. 자세한 내용은 this blog post을 확인하십시오.

+0

감사합니다. 감사합니다 !!!! 블로그 게시물은 검색 2 일 후에 나를 구해줬습니다. – user63898

관련 문제