Results 1 to 10 of 10

Thread: TableView/Model Display Question

  1. #1
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default TableView/Model Display Question

    I'm implementing a qTableView/Model where I want some of the table cells to be editable, and others to be simply for display. I can control this with the flags() function. But I'd like the editable cells to use a different text color, so their editable status is visually apparent.What is the simplest way to accomplish this?

    If the only way to do this is to subclass a custom delegate, is it possible to just set the brush in the paint() routine to the color I want (based on the model index) and then call the parent paint routine? Or will the parent perform a save()/restore() and wipe out my modifications?

    I'm hoping such subclassing isn't necessary in the first place.

  2. #2
    Join Date
    Jan 2008
    Location
    Bengaluru
    Posts
    144
    Thanks
    8
    Thanked 7 Times in 7 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: TableView/Model Display Question

    Check out the stylesheet of QTableview, Depending on the editable and non-editable fields set the background color.. For Ref. http://doc.trolltech.com/4.6/stylesh...ing-qtableview

  3. #3
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: TableView/Model Display Question

    Thank you; I think that will work. I had already looked at stylesheets as a possible solution, but hadn't realized you could set arbitrary properties on them.

  4. #4
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: TableView/Model Display Question

    Sadly, this simple and elegant approach won't work. The 'editable' property isn't exposed by QTableView, so apparently the only way to do this is to subclass ItemDelegate and tweak it's paint method. I'm hoping it won't involve anything more than changing the pen color and kicking the actual drawing back to the parent class.

  5. #5
    Join Date
    Jan 2008
    Location
    Bengaluru
    Posts
    144
    Thanks
    8
    Thanked 7 Times in 7 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: TableView/Model Display Question

    Ok. Can't you use Disable / Enable property. I think, these properties should be there. The fields which are not editable, make it disabled and try setting the stylesheet. I haven't given a try, check out once.

    I have done something similar for QMenu, Find some snippets below regarding it. Hope it helps!

    Qt Code:
    1. .QMenu::item:!disabled:selected { }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. .QMenu::item:disabled, .QMenu::item:disabled:selected { }
    To copy to clipboard, switch view to plain text mode 
    Last edited by nikhilqt; 20th April 2010 at 14:46.

  6. #6
    Join Date
    Apr 2009
    Posts
    36
    Thanks
    1
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: TableView/Model Display Question

    You may not have to use a custom delegate. If you look at the documentation for the Qt::ItemDataRole, you'll notice that there is a Qt::ForegroundRole which you can specify in your model's data method to return whatever decoration you want. Since your model should know which fields are editable, then you can use this logic to return data in different QBrush values.

  7. #7
    Join Date
    Apr 2009
    Posts
    36
    Thanks
    1
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: TableView/Model Display Question

    For example, you could have in your model's data method:

    Qt Code:
    1. if( role == Qt::ForegroundRole && isEditable ) return QBrush(QColor(0, 0, 255)); //blue
    To copy to clipboard, switch view to plain text mode 

    This should be a less painful way to do what you want to do.

  8. The following 2 users say thank you to bmhautz for this useful post:

    franku (27th August 2010), SixDegrees (20th April 2010)

  9. #8
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: TableView/Model Display Question

    This works beautifully, although it's a bit disquieting to be setting colors - which are essentially GUI items - down in the data model, which is normally concerned with...well, data, not presentation. But it solves my problem.

    Now, if I could figure out how to keep the editable cell's contents from disappearing when they're clicked on, and stick around while the user modifies them, most of my table issues will be resolved.

    Thanks again.

  10. #9
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: TableView/Model Display Question

    All problems are now solved. I realized, after applying the last tip, that the data() function can also be called in an EditRole, which demands the return of the data in an editable form. Not doing this returns...nothing, so nothing is what replaces the cell's contents while editing.

  11. The following user says thank you to SixDegrees for this useful post:

    fede (15th March 2011)

  12. #10
    Join Date
    Aug 2010
    Posts
    17
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: TableView/Model Display Question

    Thank you, so I am now able to highlight several "selected" Rows constantly out of the model:

    Qt Code:
    1. if( role == Qt::ForegroundRole ) {
    2. if(myElementDataContainer.at(index.row).isSelected())
    3. return QBrush(QColor(0, 0, 255)); //blue
    4. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. QListView does not display its model
    By mtrpoland in forum Qt Programming
    Replies: 1
    Last Post: 22nd February 2008, 19:31
  2. Model display problem
    By gyre in forum Newbie
    Replies: 3
    Last Post: 31st December 2007, 13:46
  3. tableview model view problem
    By skuda in forum Qt Programming
    Replies: 5
    Last Post: 3rd December 2007, 14:02
  4. Replies: 1
    Last Post: 10th October 2007, 10:11
  5. Model-view: Display items in different ways
    By taboom in forum Qt Programming
    Replies: 3
    Last Post: 13th August 2007, 19:05

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.