Results 1 to 4 of 4

Thread: How customize items of ModelView?

  1. #1
    Join Date
    Mar 2007
    Posts
    2
    Qt products
    Qt4

    Default How customize items of ModelView?

    Hello,
    I'm french, sorry for my english.

    I have a QSqlTableModel and a QTableView.

    I want to customize items in my tableView.

    Exemple, change the background color of a selected items with values 69:
    Qt Code:
    1. QModelIndex id = tableView->currentIndex();
    2. if ( model->data(id,Qt::DisplayRole) == 69 )
    3. id->setBackground ( pink) ..... //SOMETHING THAT
    To copy to clipboard, switch view to plain text mode 

    Or , I want an icons near items with values = 69.

  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: How customize items of ModelView?

    Quote Originally Posted by istdasklar View Post
    Exemple, change the background color of a selected items with values 69:

    Qt Code:
    1. QModelIndex id = tableView->currentIndex();
    2. if ( model->data(id,Qt::DisplayRole) == 69 )
    3. id->setBackground ( pink) ..... //SOMETHING THAT
    To copy to clipboard, switch view to plain text mode 
    Subclass QSqlTableModel and reimplement QAbstractItemModel::data():
    Qt Code:
    1. QVariant MySqlTableModel::data(const QModelIndex& index, int role) const
    2. {
    3. QVariant val = QSqlTableModel::data(index, role);
    4. if (role == Qt::BackgroundRole && val == 69)
    5. {
    6. return QVariant::fromValue(QBrush(Qt::red)); // or QColor?
    7. }
    8. return val;
    9. }
    To copy to clipboard, switch view to plain text mode 

    Or , I want an icons near items with values = 69.
    Similar approach works. Use QModelIndex::sibling() to test sibling items' values.
    J-P Nurmi

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How customize items of ModelView?

    Another possibilities are to use a proxy model that will do that (reimplementing data() as suggested) or a custom item delegate that will paint the item in a different way (changing the background colour or adding an icon).

  4. #4
    Join Date
    Mar 2007
    Posts
    13
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: How customize items of ModelView?

    if instead of the QTableView would be a QListView what are the posibilities to change an item position ?

    for example, when the mode in the list is Free you can move the items around in the widget so this tells me that items can be placed in a different position.

    what i want to achieve to make all the items in a QListView \ QListWidget to be displayed at an offset from the default position ( i want to make them appear more to the right and down)

    { anwers to this post were moved to: custom painting in QListWidget }
    Last edited by jacek; 29th March 2007 at 00:42.

Similar Threads

  1. Get Visible Items from the QListWidget
    By srj in forum Qt Programming
    Replies: 4
    Last Post: 22nd November 2006, 21:13
  2. QGraphicsItem problem - how to save items info ??
    By aamer4yu in forum Qt Programming
    Replies: 3
    Last Post: 17th October 2006, 13:17
  3. Replies: 3
    Last Post: 27th July 2006, 13:48
  4. Selective highlighting of Items
    By Kapil in forum Qt Programming
    Replies: 3
    Last Post: 26th May 2006, 13:20
  5. [Qt4]: Adding centered items in QListWidget
    By Jojo in forum Qt Programming
    Replies: 4
    Last Post: 16th March 2006, 21:04

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.