Results 1 to 9 of 9

Thread: Highlighting the border of cell in Table

  1. #1
    Join Date
    Mar 2006
    Location
    Vadodara, Gujarat, India
    Posts
    65
    Thanks
    16
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Question Highlighting the border of cell in Table

    Hi all,

    i want to highlight the boundry of a cell in a table instead of changing the background, same as it happens in excel.

    how can i do it ??
    Do what u r afraid to do, and the death of fear is sure.

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Highlighting the border of cell in Table

    For example with a custom delegate.
    Inherit QItemDelegate and override drawFocus().

  3. #3
    Join Date
    Mar 2006
    Location
    Vadodara, Gujarat, India
    Posts
    65
    Thanks
    16
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Unhappy Re: Highlighting the border of cell in Table

    can u explain a bit more ... i m new to qt and not able to get how to do it.

    i made a new class QGrid inherited QTablewidget in it.

    now how to get the individual cell dimension in it.

    rect is a inherited function of QTableWidget, but not of QTableWidgetItem, so how to get the dim. of cell.

    //class declaration.

    Qt Code:
    1. class QGrid : public QTableWidget
    2. {
    3. public:
    4. QGrid(int x,int y,QWidget *parent = 0);
    5. }
    To copy to clipboard, switch view to plain text mode 

    //implementation

    Qt Code:
    1. QGrid *table = new QGrid(5,5); //this creates a table
    2.  
    3. for(int i=0;i<5;i++)
    4. {
    5. for(int j =0;j<5;j++)
    6. {
    7. t->setText("");
    8. table->setItem(j,i,t);
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 

    where can i get the cell dimensions to make the boundry darker if selected.

    if i m going wrong .... please correct it ...
    Do what u r afraid to do, and the death of fear is sure.

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Highlighting the border of cell in Table

    Create a new class which inherits QItemDelegate, and override:
    void QItemDelegate::drawFocus ( QPainter * painter, const QStyleOptionViewItem & option, const QRect & rect ) const [virtual protected]
    Renders the region within the rectangle specified by rect, indicating that it has the focus, using the given painter and style option.
    At simplest possible, the implementation of drawFocus() could look something like this:
    Qt Code:
    1. void GridDelegate::drawFocus(QPainter* painter, const QStyleOptionViewItem &option, const QRect &rect) const
    2. {
    3. if (option.state & QStyle::State_HasFocus)
    4. {
    5. QPen pen(Qt::black);
    6. pen.setWidth(3);
    7. painter->setPen(pen);
    8. painter->drawRect(rect);
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 
    Note: above code snippet is just a dummy example for you to get started. I suggest you to take a quick look at QItemDelegate sources, there you will get a neat reference from..

    Then, for example in the constructor of Grid, you would set the item delegate to the one you created:
    Qt Code:
    1. setItemDelegate(new GridDelegate(this));
    To copy to clipboard, switch view to plain text mode 

  5. The following user says thank you to jpn for this useful post:

    ankurjain (18th March 2006)

  6. #5
    Join Date
    Mar 2006
    Location
    Vadodara, Gujarat, India
    Posts
    65
    Thanks
    16
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Thumbs up Re: Highlighting the border of cell in Table

    hi jpn,
    today i m finally done with the border highlighting part. Thanks a lot for help.

    But there is one more thing that is happening. i want that only the border is highlighted and not the background turns blue. i used the paint function and overrided it in my class's .cpp file. now one problem occurs.
    the paint function paints the table created to white and not the color i selected. also it suppresses the effect of the drawFocus function and no focus is seen in the table.

    i tried using palette in the table but can't make it work.

    can u suggest somethng in that.

    my code is as follows.


    Qt Code:
    1. //griddelegate.h
    2. class GridDelegate : public QItemDelegate
    3. {
    4.  
    5. public:
    6.  
    7. GridDelegate(QObject *parent = 0);
    8.  
    9. void drawFocus(QPainter* painter,
    10. const QStyleOptionViewItem &option,
    11. const QRect &rect) const;
    12.  
    13. void paint(QPainter * painter,
    14. const QStyleOptionViewItem & option,
    15. const QModelIndex & index ) const;
    16.  
    17.  
    18. };
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. //griddelegate.cpp
    2.  
    3. #include <QtGui>
    4. #include <GridDelegate.h>
    5.  
    6. GridDelegate::GridDelegate(QObject *parent) : QItemDelegate(parent)
    7. {
    8.  
    9. }
    10.  
    11. void GridDelegate::paint(QPainter * painter,
    12. const QStyleOptionViewItem & option,
    13. const QModelIndex & index ) const
    14. {
    15.  
    16. if (option.state & QStyle::State_Selected)
    17. {
    18. QBrush brsh(Qt::red);
    19. painter->setBrush(brsh);
    20. }
    21. else if (option.state & QStyle::State_HasFocus)
    22. {
    23. QBrush brsh1(Qt::red);
    24. painter->setBrush(brsh1);
    25. }
    26.  
    27. }
    28.  
    29. void GridDelegate::drawFocus(QPainter* painter,
    30. const QStyleOptionViewItem &option,
    31. const QRect &rect) const
    32.  
    33. {
    34. if (option.state & QStyle::State_HasFocus)
    35.  
    36. {
    37.  
    38. QPen pen(Qt::black);
    39.  
    40. pen.setWidth(3);
    41.  
    42. painter->setPen(pen);
    43.  
    44. painter->drawRect(rect);
    45.  
    46. } else
    47. if (option.state & QStyle::State_Active)
    48.  
    49. {
    50.  
    51. QPen pen(Qt::green);
    52.  
    53. pen.setWidth(5);
    54.  
    55. painter->setPen(pen);
    56.  
    57. painter->drawRect(rect);
    58.  
    59. } else
    60. if (option.state & QStyle::State_KeyboardFocusChange)
    61.  
    62. {
    63.  
    64. QPen pen(Qt::black);
    65.  
    66. pen.setWidth(5);
    67.  
    68. painter->setPen(pen);
    69.  
    70. painter->drawRect(rect);
    71.  
    72. }
    73. }
    To copy to clipboard, switch view to plain text mode 


    in the constructor of my main program, i merely call the setItemDelegate function as suggested by u.
    Do what u r afraid to do, and the death of fear is sure.

  7. #6
    Join Date
    Mar 2006
    Location
    Vadodara, Gujarat, India
    Posts
    65
    Thanks
    16
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Cool Re: Highlighting the border of cell in Table

    Thanx buddy... i was able to set the background color by using palette.... thanx a lot ...
    Do what u r afraid to do, and the death of fear is sure.

  8. #7
    Join Date
    Mar 2006
    Location
    Vadodara, Gujarat, India
    Posts
    65
    Thanks
    16
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Highlighting the border of cell in Table

    one more problem jpn,

    i want to set one cell, say (0,0), as default if the form opens. i was able to set focus on the cell by function, setCurrentCell(0,0), but not able to draw that default focus (thicken the boundry) how can it be done ??




    Right now the window looks like above ... as i select the events in the left pane .... the table in the right pane opens up with the cell (0,0) selected ... it is indicated by the blue color.... as i had changed the palette ... now when i want the boundry of the selected cell must also get thick, as it is the behaviour i had made on click of mouse or selection by keyboard.

    please help....
    Last edited by ankurjain; 20th March 2006 at 08:09.
    Do what u r afraid to do, and the death of fear is sure.

  9. #8
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Highlighting the border of cell in Table

    Try setting the focus to your table widget during the startup:
    Qt Code:
    1. table->setFocus(Qt::ActiveWindowFocusReason);
    To copy to clipboard, switch view to plain text mode 

  10. The following user says thank you to jpn for this useful post:

    ankurjain (21st March 2006)

  11. #9
    Join Date
    Mar 2006
    Location
    Vadodara, Gujarat, India
    Posts
    65
    Thanks
    16
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Highlighting the border of cell in Table

    thanx jpn,
    it works now by with setCurrentCell ...
    Do what u r afraid to do, and the death of fear is sure.

Similar Threads

  1. Problems with QString
    By cyberboy in forum Qt Programming
    Replies: 2
    Last Post: 13th October 2008, 09:18
  2. Making Table cell as a combobox?
    By kaushal_gaurav in forum Qt Programming
    Replies: 9
    Last Post: 1st August 2008, 13:04
  3. Replies: 4
    Last Post: 4th February 2008, 07:16
  4. Highlighting a cell in QTable
    By sumsin in forum Qt Programming
    Replies: 1
    Last Post: 18th April 2007, 07:28
  5. Replies: 11
    Last Post: 8th September 2006, 00:15

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.