Results 1 to 3 of 3

Thread: QTableView HOVERING ISSUE

  1. #1
    Join Date
    Mar 2011
    Location
    Hyderabad
    Posts
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QTableView HOVERING ISSUE

    Hi all,
    I am trying to implement hovering on QTableview i subclassed QStyledItemDelegate and reimplemented paint as show in the below code
    i am not able to get hovering properly for the total row
    the same code is working fine with QTreeview.


    void Delegate:aint(QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const
    {
    if (!index.isValid())
    {
    return; //early exit
    }

    const QTableView* const view = dynamic_cast< QTableView* >(this->parent());
    Q_ASSERT (NULL != view);
    if (NULL == view)
    {
    return; //another early exit
    }

    int width = 0;
    for (int colIndex = 0; colIndex < TOTAL_COLUMNS; colIndex++)
    {
    if (!view->isColumnHidden(colIndex))
    {
    width += view->columnWidth(colIndex);
    }
    }

    const QRect rowRect = option.rect.united(QRect(0, option.rect.top(), width, option.rect.height()));

    if (option.state & QStyle::State_Selected)
    {
    painter->fillRect(option.rect, QColor(110, 161, 241));
    }

    if(option.state & QStyle::State_MouseOver)
    {
    painter->fillRect(rowRect ,QColor(191, 205, 228));
    }


    QString dispText;
    QRect textRect = option.rect.adjusted(4, 3, 0, 0);
    QString text = displayText( index.data(), view ? view->locale() : QLocale() );

    switch(index.column())
    {
    case COL_1:
    {

    dispText = option.fontMetrics.elidedText(text, Qt::ElideRight, textRect.width());
    painter->drawText(textRect, Qt::AlignAbsolute, tr("%1 ").arg(dispText));

    }
    break;
    case COL_2:
    {
    }
    Kiran Pechetti

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: QTableView HOVERING ISSUE

    Kiran, Please use Code tags to post code, it is very difficult to read the code.

    So, you say it is working for QTreeView, and does not work for QTableView, Did you try debugging it, what happens when you hover the mouse, is State_MouseOver option event received?

    Qt Code:
    1. for (int colIndex = 0; colIndex < TOTAL_COLUMNS; colIndex++)
    To copy to clipboard, switch view to plain text mode 
    can be better written as
    Qt Code:
    1. for (int colIndex = 0; colIndex < view->model()->columnCount(); colIndex++)
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Aug 2008
    Posts
    45
    Thanks
    1
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTableView HOVERING ISSUE

    Do understand that the delegate is used to draw each item in the view. In QTableView one item is same as one cell. When the view paints itself it draws those items from top to bottom, and left to right. With your implementation you are painting the mouse over effect outside the rectangle that current item occupies. When next item in the row is painted it will paint itself over the mouseover effect. Maybe this the reason?

    I think you should check the mouseover status by manually instead of trusting the style option's state information as it is valid only for the current item/cell.

    Maybe something like this:
    Qt Code:
    1. QPoint mousePosition = view->mapFromGlobal( QCursor::pos() );
    2. if( rowRect.contains( mousePosition ) ) // check if mouse cursor is on top of current row
    3. {
    4. // paint mouseover effect only for current item
    5. painter->fillRect(option.rect, QColor(191, 205, 228));
    6. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. QTableView massive model data updates issue
    By cyberbob in forum Newbie
    Replies: 3
    Last Post: 7th December 2010, 23:09
  2. QStandardItemModel / QTableView sorting issue
    By AlphaWolfXV in forum Qt Programming
    Replies: 2
    Last Post: 31st August 2010, 05:42
  3. hovering problem in QGraphicsItem
    By wagmare in forum Qt Programming
    Replies: 4
    Last Post: 13th March 2009, 17:31
  4. QTableView focus issue
    By gemidjy in forum Qt Programming
    Replies: 4
    Last Post: 19th February 2008, 15:51
  5. QTableView number of rows and scrollbar issue
    By jnk5y in forum Qt Programming
    Replies: 3
    Last Post: 1st March 2006, 06:55

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.