Results 1 to 6 of 6

Thread: Table and combo with, almost, the same data

  1. #1
    Join Date
    Feb 2017
    Posts
    19
    Thanks
    5
    Qt products
    Qt5

    Default Table and combo with, almost, the same data

    I have a db table named 'clients' with fields 'firstname' and 'lastname'.
    It's first row is empty.

    I want this table to 'feed' a QTableView and a QComboBox.

    In the table I want the first row (the empty) not to be shown.
    Also the table to be editable.
    I need changes made in the model of the table to be filled back in the database.
    And the combo 's model to be automatically updated.

    In the combo I want all the rows (and the first one) to be shown. But I also want the 2 fields concatenated.

    I thought of using a proxy, because:
    1. I 'feed' two widgets from one db table.
    2. I want the second model to be updated when the first changes.
    3. their data, slightly, differ (by the first row).

    The first row in the db table is empty because I want the combo to have an option of 'nothing', no option, Null.
    But I don't want the table to have an empty row.
    So I thought of a model 'feeding' all the data to the combo, and a filter (here I thought of QSortFilterProxyModel) that removes the first row for the table.

    How can I accomplish this without using 'external' libraries like KDE?
    Last edited by panoss; 24th February 2017 at 21:31.

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Table and combo with, almost, the same data

    You are in the right path, keep going the same way.

    Just one suggestion, empty row in DB sounds strange and is redundant, better have a main model (QSqlTableModel) and connect two QSortFilterProxyModel to it one for Table, another for Combo box.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. #3
    Join Date
    Feb 2017
    Posts
    19
    Thanks
    5
    Qt products
    Qt5

    Default Re: Table and combo with, almost, the same data

    Quote Originally Posted by Santosh Reddy View Post
    You are in the right path, keep going the same way.
    The problem is I 'm in no way!
    I haven't done anything yet, I 'm trying to become familiar with models and their subclassing, I can't say I understand much, they seem to me very complicated.

    Quote Originally Posted by Santosh Reddy View Post
    Just one suggestion, empty row in DB sounds strange and is redundant, better have a main model (QSqlTableModel) and connect two QSortFilterProxyModel to it one for Table, another for Combo box.
    So you 're suggesting:
    Source model: a QSqlTableModel.
    Table model: a QSortFilterProxyModel.
    Combo model: another QSortFilterProxyModel.

    If I remove the empty first row from my db table, I shall add it in the combo 's model with model->insertRow()?
    If I do this, it will also be added in the source model and in the table 's model, right?

    Or:
    1. Remove empty row from db table.
    2. Insert an empty row in the source model.
    3. The table 's model filters out the empty row.
    4. Result: table doesn't contain the empty row, but the combo does contain it.

    How shall I concatenate the 2 fields for the combo?
    FirstName + LastName to be replaced with FullName?
    Last edited by panoss; 27th February 2017 at 08:25.

  4. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Table and combo with, almost, the same data

    Quote Originally Posted by panoss View Post
    I haven't done anything yet, I 'm trying to become familiar with models and their subclassing,...
    Without trying anything, this is how far you can get familiar with Qt MVC. I suggest to start implementing some basic working model / view, you will get a more clarity.

    Quote Originally Posted by panoss View Post
    If I remove the empty first row from my db table, I shall add it in the combo 's model with model->insertRow()?
    If I do this, it will also be added in the source model and in the table 's model, right?
    No, it should taken care by overriding
    Qt Code:
    To copy to clipboard, switch view to plain text mode 

    Quote Originally Posted by panoss View Post
    How shall I concatenate the 2 fields for the combo?
    FirstName + LastName to be replaced with FullName?
    Override the
    Qt Code:
    To copy to clipboard, switch view to plain text mode 


    Added after 1 51 minutes:


    For a head start, this is how ComboBox model will look like (working code)

    Qt Code:
    1. class ComboModel : public QAbstractProxyModel
    2. {
    3. public:
    4. ComboModel(QObject * parent = 0)
    5. { }
    6.  
    7. QModelIndex mapFromSource(const QModelIndex & sourceIndex) const
    8. {
    9. if(!sourceModel())
    10. return QModelIndex();
    11.  
    12. if(!sourceIndex.isValid())
    13. return QModelIndex();
    14.  
    15. return index(sourceIndex.row() + 1, sourceIndex.column());
    16. }
    17.  
    18. QModelIndex mapToSource(const QModelIndex & proxyIndex) const
    19. {
    20. if(!proxyIndex.isValid())
    21. return QModelIndex();
    22.  
    23. if(sourceModel())
    24. {
    25. if(proxyIndex.row() == 0)
    26. return QModelIndex();
    27. else
    28. return sourceModel()->index(proxyIndex.row() - 1, proxyIndex.column());
    29. }
    30.  
    31. return QModelIndex();
    32. }
    33.  
    34. protected:
    35. QModelIndex index(int row, int column, const QModelIndex & parent = QModelIndex()) const
    36. {
    37. if(!parent.isValid())
    38. return createIndex(row, column);
    39. return QModelIndex();
    40. }
    41.  
    42. QModelIndex parent(const QModelIndex & /*child*/) const
    43. {
    44. return QModelIndex();
    45. }
    46.  
    47. int rowCount(const QModelIndex & parent) const
    48. {
    49. if(!parent.isValid() && sourceModel())
    50. return sourceModel()->rowCount(QModelIndex()) + 1;
    51.  
    52. return 0;
    53. }
    54.  
    55. int columnCount(const QModelIndex & parent) const
    56. {
    57. if(!parent.isValid() && sourceModel())
    58. return sourceModel()->columnCount(QModelIndex());
    59.  
    60. return 0;
    61. }
    62.  
    63. Qt::ItemFlags flags(const QModelIndex & index) const
    64. {
    65. if(index.row() == 0)
    66. return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable;
    67.  
    68. return sourceModel()->flags(mapToSource(index));
    69. }
    70.  
    71. QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const
    72. {
    73. if(!index.isValid() || !sourceModel())
    74. return QVariant();
    75.  
    76. int row = index.row();
    77.  
    78. if(row > 0)
    79. {
    80. if(role == Qt::DisplayRole)
    81. {
    82. QModelIndex firstNameIndex = mapToSource(this->index(row, 0));
    83. QModelIndex lastNameIndex = mapToSource(this->index(row, 1));
    84.  
    85. return QString("%1, %2")
    86. .arg(firstNameIndex.data().toString())
    87. .arg(lastNameIndex.data().toString());
    88. }
    89. else
    90. return mapToSource(this->index(row, 0)).data(role);
    91. }
    92.  
    93. if(role == Qt::DisplayRole)
    94. return QString("--no selection--");
    95. if(role == Qt::ForegroundRole)
    96. return QVariant(QColor(Qt::lightGray));
    97.  
    98. return QVariant();
    99. }
    100. };
    To copy to clipboard, switch view to plain text mode 
    Last edited by Santosh Reddy; 27th February 2017 at 11:16.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

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

    panoss (27th February 2017)

  6. #5
    Join Date
    Feb 2017
    Posts
    19
    Thanks
    5
    Qt products
    Qt5

    Default Re: Table and combo with, almost, the same data

    Thank you very much Santosh.
    Works perfectly, but you shouldn't give me ready - made code, you 'll spoil me and make me lazy!
    I only had to comment out 2 lines in data function:
    Qt Code:
    1. // if(role == Qt::ForegroundRole)
    2. // return QVariant(QColor(Qt::lightGray));
    To copy to clipboard, switch view to plain text mode 
    The would raise an error: "C2440: '<function-style-cast>' : cannot convert from 'Qt::GlobalColor' to 'QColor'
    Source or target has incomplete type".


    Added after 1 53 minutes:


    I can't beleive this, I 'm trying to achieve this:
    Qt Code:
    1. QModelIndex firstNameIndex = mapToSource(this->index(row, 0));
    2. QModelIndex lastNameIndex = mapToSource(this->index(row, 1));
    3.  
    4. return QString("%1, %2")
    5. .arg(firstNameIndex.data().toString())
    6. .arg(lastNameIndex.data().toString());
    To copy to clipboard, switch view to plain text mode 

    for 4 days now...and it was so simple (and logical) now that you posted it...
    Thank you so much.
    Last edited by panoss; 27th February 2017 at 15:13.

  7. #6
    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: Table and combo with, almost, the same data

    Removing the first row could have also be done by simply deriving from QSortFilterProxyModel and rejecting the first row in the override of filterAcceptsRow().

    Cheers,
    _

Similar Threads

  1. C++Qt Getting data from HTML table
    By danalex07 in forum Newbie
    Replies: 2
    Last Post: 29th November 2015, 15:54
  2. Replies: 1
    Last Post: 20th January 2015, 17:49
  3. Replies: 1
    Last Post: 11th June 2013, 16:56
  4. Replies: 1
    Last Post: 8th June 2011, 14:13
  5. inserting combo box in table ..
    By wagmare in forum Qt Programming
    Replies: 3
    Last Post: 2nd June 2009, 07:42

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.