Results 1 to 8 of 8

Thread: [newbie] Set background of specific row in QListView

  1. #1
    Join Date
    Jun 2010
    Posts
    7
    Qt products
    Qt5
    Platforms
    Unix/X11

    Question [newbie] Set background of specific row in QListView

    Hi,

    I've found this: https://forum.qt.io/topic/76265/set-...-in-qtableview
    I have copied the setRowColor() from the link above into my code base call it like:
    Qt Code:
    1. void Widget::on_HostList_doubleClicked(const QModelIndex &index)
    2. {
    3. QAbstractItemModel *model = ui->HostList->model();
    4. setRowColor(model,index.row(),Qt::red,index);
    5. }
    To copy to clipboard, switch view to plain text mode 
    but it does not seem to work, the color is not changing. Can someone help me here?

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: [newbie] Set background of specific row in QListView

    ui->HostList->model()
    What is your model? Have you implemented the setData() method on it? If so, have you implemented it for the Qt:: BackgroundRole role?
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Jun 2010
    Posts
    7
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: [newbie] Set background of specific row in QListView

    Hi d_stranz,

    Thanks for your reply,
    I'm using a QStringListModel and my data gets populated as follows:
    Qt Code:
    1. int JTFTools::ListViewPopulate(QListView *listView, QStringList *stringList)
    2. {
    3. int rv = OK;
    4.  
    5. QStringListModel* listModel = new QStringListModel(*stringList, listView);
    6. QItemSelectionModel *oldModel = listView->selectionModel();
    7. listView->setModel(listModel);
    8.  
    9. delete oldModel;
    10.  
    11. return rv;
    12. }
    To copy to clipboard, switch view to plain text mode 
    setData() is here: http://doc.qt.io/qt-5/qstringlistmodel.html#setData but I couldn't find Qt:: BackgroundRole, how do I go about this?
    Thanks!
    Last edited by cerr; 3rd October 2017 at 04:24.

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: [newbie] Set background of specific row in QListView

    I have copied the setRowColor() from the link above into my code base call it like
    And you implemented this -exact- code, right?

    Qt Code:
    1. void setRowColor(QAbstractItemModel* model, int row, const QBrush& color, const QModelIndex& parent = QModelIndex())
    2. {
    3. if(!model)
    4. return;
    5. if(row<0 || row>=model->rowCount(parent))
    6. return;
    7.  
    8. const int colCount = model->columnCount(parent);
    9. for (int j = 0; j < colCount; ++j)
    10. model->setData(model->index(row,j,parent),color,Qt::BackgroundRole);
    11. }
    To copy to clipboard, switch view to plain text mode 

    "BackgroundRole" is one of the Qt::ItemDataRole enums.

    Does this function work if you call it from somewhere beside the double-click slot? For example, in your ListViewPopulate method, what happens if you populate the list, then run a loop that sets every even-numbered entry to red background?

    Qt Code:
    1. for ( int nRow = 0; nRow < listModel->rowCount(); nRow += 2 )
    2. setRowColor( listModel, nRow, Qt::red );
    To copy to clipboard, switch view to plain text mode 

    By the way, your code in ListViewPopulate is a memory leak. Each time the function executes, it creates a new list model and sets it on the view, but you never delete the old list model (listView->model()). Instead for some strange reason, you delete the selection model (which is entirely different from the string list model you create and set on the view).
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  5. #5
    Join Date
    Jun 2010
    Posts
    7
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: [newbie] Set background of specific row in QListView

    Thank you d_stranz and everyone else involved ,
    for future reference, I got an excellent working example here: https://forum.qt.io/post/418663

  6. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: [newbie] Set background of specific row in QListView

    I got an excellent working example here
    And I hope you realize that the critical line from that example:

    Qt Code:
    1. // set the bg color of row 2
    2. model->setData(model->index(1, 0), QBrush(Qt::red), Qt::BackgroundRole);
    To copy to clipboard, switch view to plain text mode 

    is no different from what you posted in the setRowColor() code and is no different from what I suggested to you to try in my previous post, right?
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  7. #7
    Join Date
    Jun 2010
    Posts
    7
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: [newbie] Set background of specific row in QListView

    Quote Originally Posted by d_stranz View Post
    And I hope you realize that the critical line from that example:

    Qt Code:
    1. // set the bg color of row 2
    2. model->setData(model->index(1, 0), QBrush(Qt::red), Qt::BackgroundRole);
    To copy to clipboard, switch view to plain text mode 

    is no different from what you posted in the setRowColor() code and is no different from what I suggested to you to try in my previous post, right?
    Yes, I definitely do and that the problem is with QStringListModel not supporting the color setting, but QStandardItemModel in place works properly.

  8. #8
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: [newbie] Set background of specific row in QListView

    Yes, I definitely do and that the problem is with QStringListModel not supporting the color setting, but QStandardItemModel in place works properly.
    Good to know this. That's something that is not in the documentation and should be.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Select specific item in QListView
    By Cerberus in forum Qt Programming
    Replies: 6
    Last Post: 17th October 2015, 14:55
  2. Replies: 4
    Last Post: 3rd July 2014, 15:52
  3. Replies: 2
    Last Post: 12th December 2012, 03:40
  4. Add Gradient to Background for QListView
    By vasant in forum Qt Programming
    Replies: 11
    Last Post: 19th February 2010, 08:08
  5. QListView: How to move the cursor to a specific row
    By muellerp in forum Qt Programming
    Replies: 2
    Last Post: 21st November 2008, 08:29

Tags for this Thread

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.