Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] QStyledItemDelegate custom mouse cursor for tableWidget item
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] QStyledItemDelegate custom mouse cursor for tableWidget item

Scheduled Pinned Locked Moved General and Desktop
10 Posts 3 Posters 7.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    bepehr
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      1
      • B Offline
        B Offline
        bepehr
        wrote on last edited by
        #3

        [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

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          • B Offline
            B Offline
            bepehr
            wrote on last edited by
            #5

            [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

            1 Reply Last reply
            0
            • A Offline
              A Offline
              andre
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0
              • B Offline
                B Offline
                bepehr
                wrote on last edited by
                #7

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

                   }
                

                }@

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  ghezbad
                  wrote on last edited by
                  #8

                  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 :->

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    andre
                    wrote on last edited by
                    #9

                    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?

                    1 Reply Last reply
                    0
                    • B Offline
                      B Offline
                      bepehr
                      wrote on last edited by
                      #10

                      [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

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved