[SOLVED] QStyledItemDelegate custom mouse cursor for tableWidget item



  • hi , i want to set Qt::PointingHandCursor for a item of my tableWidget
    my idea is change option.widget cursor but its a const widget and i cant change cursor
    is my idea is wrong at all ?

    @void sDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
    {

    if ( index.column() == 2 )
    {
         //my custom item with underline for text and PointingHandCursor 
    
        Q_ASSERT(index.isValid());
        QStyleOptionViewItem opt = option;
        initStyleOption(&opt, index);
        const QWidget *widget =option.widget;
        QStyle *style = widget ? widget->style() : QApplication::style();
        opt.font.setUnderline(true);
        style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, widget);
    }
    else{
    QStyledItemDelegate::paint(painter  , option , index);
    }
    

    }@

    ty



  • Drawing code is not the suitable place to change the mouse cursor. For that, reimplement editorEvent (from QAbstractItemDelegate). There, you can check if the mouse if over your underlined text, and if so, change the mouse cursor.



  • [quote author="Andre" date="1359576338"]Drawing code is not the suitable place to change the mouse cursor. For that, reimplement editorEvent (from QAbstractItemDelegate). There, you can check if the mouse if over your underlined text, and if so, change the mouse cursor. [/quote]

    i tested that way before but main problem was start editorEvent with click or some other ways like press edit key , but i want to change mouse cursor when mouse move over items , not when user clicked on them

    even i tested mouseMoveEvent of QTableWidget but didnt succesfull
    ty



  • You might need to enable mouse tracking for your QTableView and/or the viewport in order to receive mouseMove events.



  • [quote author="Andre" date="1359577167"]You might need to enable mouse tracking for your QTableView and/or the viewport in order to receive mouseMove events.[/quote]

    i have done that , and i receive mouseMove events , but how i can know mouse is over of my costume item ?

    ty



  • Compare the rect you paint in with the position of the mouse, of course :-)

    You can either somehow cache that information or just calculate on the mouse move. Because all you do is underline all text, you could use something like [[doc:QFontMetrics]] to figure out the size of the text.



  • [quote author="Andre" date="1359578536"]Compare the rect you paint in with the position of the mouse, of course :-)

    You can either somehow cache that information or just calculate on the mouse move. Because all you do is underline all text, you could use something like [[doc:QFontMetrics]] to figure out the size of the text. [/quote]

    i have done this now ! mouse cursor now change on that items , after 2 days search , ty Andre
    but i have 1 more problem , when my mouse move over from that item to a header item i cant know and change the pointer to normal pointer ...
    how i can fix this ?
    did i explain correctly ? or u need a picture of it ? my english sucks ... :D
    ty

    @void myTableWidget::mouseMoveEvent(QMouseEvent *event)
    {
    QAbstractItemModel *m(model());
    if (m)
    {
    QModelIndex index = indexAt(event->pos());
    if (index.isValid())
    {
    if (index.column() == 2)
    {
    setCursor(QCursor(Qt::PointingHandCursor));
    }
    else
    {
    setCursor(QCursor(Qt::ArrowCursor));
    }
    }
    else
    {
    setCursor(QCursor(Qt::ArrowCursor));
    }

       }
    

    }@



  • it's really easy bepehr, think, u will find the answer!
    i allready know the answer but I want you to be Independent programmer :d :->



  • Perhaps there are other interesting events to listen for?
    Also, your implementation will make the hand cursor appear for all and the whole cell in your column, instead of only when hovering underlined text. Is that really what you want?



  • [quote author="Andre" date="1359622677"]Perhaps there are other interesting events to listen for?
    Also, your implementation will make the hand cursor appear for all and the whole cell in your column, instead of only when hovering underlined text. Is that really what you want?[/quote]

    yes its enough to set hand cursor for column , ty for your help


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.