简体   繁体   中英

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM