Results 1 to 5 of 5

Thread: Subclass of QSqlRelationTableModel: How can i get value of antoher column?

  1. #1
    Join Date
    Jun 2009
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Subclass of QSqlRelationTableModel: How can i get value of antoher column?

    I have Subclassed (i hope you call my doing "subclass", in dont now exactly!) a QSqlRelationTableModel to prepare some Strings in my QTableView.

    Here is the Code:
    Qt Code:
    1. QVariant qtbooksqlrelationmodel::data(const QModelIndex &index, int role) const
    2. {
    3. QVariant value = QSqlRelationalTableModel::data(index, role);
    4.  
    5. if (value.isValid() && role == Qt::DisplayRole)
    6. {
    7. if (index.column() == 1)
    8. {
    9. if (QDateTime::fromString(value.toString(), "yyyy-MM-dd").isValid())
    10. {
    11. return QString((QDateTime::fromString(value.toString(), "yyyy-MM-dd").toString("dd.MM.yyyy")));
    12. }
    13. else if (value == iFixkosten_JEDEN_MONAT )
    14. {
    15. return qApp->trUtf8("Jeden Monat");
    16. }
    17. else if (value == iFixkosten_JEDE_WOCHE )
    18. {
    19. return qApp->trUtf8("Jeden Woche");
    20. }
    21. }
    22. else if (index.column() == 3)
    23. {
    24. check_Kategorie_ist_einnahme(index.model()->index(index.row(), 3));
    25. return value.toString().prepend(QString::fromUtf8("+")).append(QString::fromUtf8(" €"));
    26. }
    27. }
    28. //if (role == Qt::TextColorRole && index.column() == 1)
    29. // return qVariantFromValue(QColor(Qt::blue));
    30. return value;
    31. }
    To copy to clipboard, switch view to plain text mode 
    (Sorry, for the German Variable-Names)

    Now, for better understanding:
    I have two Columns:
    category | price
    1 | 3,40€
    2 | 5,70€
    6 | 14,69€
    and so one.

    Now i want to prepare the price value related to its category.
    If its category 6 i want to value.prepend(QString::fromUtf8("+")), if not i want value.prepend(QString::fromUtf8("-")).

    How could i get the value of column 1 if iam "in" column 2? Is there any way?

    I tried something like:
    Qt Code:
    1. index.model()->index(index.row(), 1).data().toString()
    To copy to clipboard, switch view to plain text mode 
    But after that, my Application died with this Message:
    The program has unexpectedly finished.
    Thanks in advanced.

    Havoc][

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Subclass of QSqlRelationTableModel: How can i get value of antoher column?

    Hi,
    currently I can't validate my answer, but isn't it simply:
    Qt Code:
    1. data(index(index.row,0),Qt::DisplayRole).toString()
    To copy to clipboard, switch view to plain text mode 
    0=the first column of your table.

  3. #3
    Join Date
    Jun 2009
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Subclass of QSqlRelationTableModel: How can i get value of antoher column?

    Any other Ideas :-)?

    Neither "value" nor "index" has any equivalent "data(index)" function.

    Thanks in advance.

    Havoc][

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Subclass of QSqlRelationTableModel: How can i get value of antoher column?

    Quote Originally Posted by 'Havoc
    Neither "value" nor "index" has any equivalent "data(index)" function.
    yes, but your model does. If you want you can also write this->data(index, role)!

  5. #5
    Join Date
    Jun 2009
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Subclass of QSqlRelationTableModel: How can i get value of antoher column?

    Ah! Okay. Thanks. It works now:

    Qt Code:
    1. this->data(QAbstractItemModel::createIndex(index.row(), 2, 0),Qt::DisplayRole).toString()
    To copy to clipboard, switch view to plain text mode 

    Havoc][

Similar Threads

  1. Replies: 0
    Last Post: 5th November 2008, 14:31
  2. QSortFilterProxyModel - lessThan sorts wrong column
    By ghorwin in forum Qt Programming
    Replies: 2
    Last Post: 21st July 2008, 09:06
  3. QSqlQueryModel write subclass
    By skuda in forum Qt Programming
    Replies: 6
    Last Post: 29th October 2007, 16:18
  4. Replies: 0
    Last Post: 10th November 2006, 13:46
  5. hidden QListView column suddenly visible
    By edb in forum Qt Programming
    Replies: 10
    Last Post: 27th January 2006, 08:00

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.