Results 1 to 5 of 5

Thread: Change column data based on data from another column in QTableView?

  1. #1
    Join Date
    Oct 2013
    Posts
    142
    Thanks
    36
    Thanked 3 Times in 3 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Change column data based on data from another column in QTableView?

    So I have a QTableView, QSqlQueryModel as its model, what I want is to set up the data displayed in column Y inside that table based on data from column X.

    Below is the code I have for changing the data of the column based on it's own data:
    Qt Code:
    1. QVariant ModelLProd::data(const QModelIndex &index, int role) const
    2. {
    3. QVariant value = QSqlQueryModel::data(index, role);
    4. if (value.isValid() && role == Qt::DisplayRole) {
    5. if (index.column() == 8 && value==99){
    6. return value.toString().replace("99","0");
    7. }
    8. return value;
    9. }
    To copy to clipboard, switch view to plain text mode 
    So basically in index.column()==8 I have percentages and if the value==99 I want to go to index.column()==9 and return value.toString().replace("99","0")

    Any ideeas? As I ran out of them.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Change column data based on data from another column in QTableView?

    If you want to alter the value of column 9, then you have to check for request for column 9.
    You are currently working solely within column 8

    Cheers,
    _

  3. #3
    Join Date
    Oct 2013
    Posts
    142
    Thanks
    36
    Thanked 3 Times in 3 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Change column data based on data from another column in QTableView?

    Can you be more specific about it?
    If I put
    Qt Code:
    1. if (index.column() == 8 && value==99){
    2. if(index.column()==9){
    3. return value.toString().replace("99","0");
    4. }
    5. }
    To copy to clipboard, switch view to plain text mode 

    ,for example, nothing happens and I ran of ideeas how to target column 9 from inside column 8 block.
    I understand how to target them normally by checking if index.column matches the column I want, but I can't figure it out how connect those 2 columns.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Change column data based on data from another column in QTableView?

    Well, index.column() can obviously not be 8 and 9 at the same time, so first checking for 8 and the checking for 9 will always fail.

    Just imagine what the view will do when it wants to get the value of column 9 in, lets say the first row.
    It will call data() with an index that contains row=0 and column=9, right?

    So checking for 8 is not going to help you in any way, right?

    So you now have the value for row 0, column 9. You want to return that value or a changed value depending on the value in column 8.
    How do you get the value of column 8, same row?

    Cheers,
    _

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

    adutzu89 (27th February 2014)

  6. #5
    Join Date
    Oct 2013
    Posts
    142
    Thanks
    36
    Thanked 3 Times in 3 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Change column data based on data from another column in QTableView?

    Thank you for your guiding. I finally solved with the following:
    Qt Code:
    1. QVariant ModelLProd::data(const QModelIndex &index, int role) const
    2. {
    3. QVariant value = QSqlQueryModel::data(index, role);
    4. if (value.isValid() && role == Qt::DisplayRole && index.column() == 9) {
    5. int rand=index.row();
    6. int val=index.sibling(rand,8).data().toInt();
    7. if(val==99){
    8. return value.toString().replace(value.toString(),"0.00");
    9. }
    10. }
    11. return value;
    12. }
    To copy to clipboard, switch view to plain text mode 

    I was looking at the issue the wrong way.

Similar Threads

  1. Get data from QTableView sorted by column
    By qks1 in forum Qt Programming
    Replies: 0
    Last Post: 26th June 2013, 09:27
  2. How to get all column data of a QTableView
    By npascual in forum Qt Programming
    Replies: 1
    Last Post: 26th September 2012, 09:05
  3. Replies: 4
    Last Post: 21st August 2011, 20:38
  4. Replies: 2
    Last Post: 5th September 2010, 14:06
  5. How to change the order of QTableView Column ?
    By Kode.Cooper in forum Qt Programming
    Replies: 1
    Last Post: 27th January 2010, 08: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.