How can I change the mouse pointer when mouse over text with QStyledItemDelegate?

余生颓废 提交于 2021-02-08 09:16:15

问题


How can I change the mouse icon when the mouse is over the text in item delegate? I have this part, but I can't find any example to change the mouse pointer.

What am I missing?

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

回答1:


First of all you will need the mouse position. You can get it using the QCursor::pos static function.

QPoint globalCursorPos = QCursor::pos();

Notice that the result is in global screen coordinates, so you will have to translate it in widget coordinates. Let's assume that the widget on which you are using the delegate is called myWidget. In order to do the translation you will need the mapFromGlobal function of QWidget

QPoint widgetPos = myWidget->mapFromGlobal(globalCursorPos);

Finally you are going to need the indexAt from QAbstractItemView which returns the model index of the item at the viewport coordinates point.

If myView is the name of the view you are using then the index at the current position is:

QModelIndex currentIndex = myView->itemAt(widgetPos);

Notice that you may need the viewport() in order to be precise. In this case in order to map the global coordinates you will need:

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

Finally you have to check if the returned index from the itemAt is the same with the index in your paint function. If yes change the cursor to whta you want, else restore the default cursor

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

This is the basic idea. Another option would be to reimplement the mouseMoveEvent and implement the functionality there. Check this blog post for more details.



来源:https://stackoverflow.com/questions/9131727/how-can-i-change-the-mouse-pointer-when-mouse-over-text-with-qstyleditemdelegate

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!