Results 1 to 2 of 2

Thread: QCompleter and a UnionModel query internal submodels

  1. #1
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    258
    Thanks
    22
    Thanked 19 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default QCompleter and a UnionModel query internal submodels

    Hi,
    I am trying to create a lineedit with a completer that queries different models based on the length of the lineedit's input. In the test example below, the completer should match against one model if the input has a length less or equal three characters. with 4-6 charachters, the completer should match against the other model and with more characters match against both. It doesn not work however . If I modify the code to start out in UnionModel::MatchAll mode and change onTextChanged to return immediately without applying changes to the match mode the completer can match against both models, so I think the code works to some extent, thus it may be some QCompleter internals that keep this from working ...
    Can anybody tell me what I am doing wrong?


    Qt Code:
    1. #include <QtGui>
    2.  
    3. class Model : public QAbstractListModel
    4. {
    5. Q_OBJECT
    6. public:
    7. Model(QStringList& list, QObject* parent = 0)
    8. : QAbstractListModel(parent), m_list(list) {}
    9.  
    10. int rowCount(const QModelIndex& parent = QModelIndex()) const {
    11. return m_list.count();
    12. }
    13.  
    14. QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const {
    15. if (!index.isValid()) return QVariant();
    16. if (role == Qt::DisplayRole) {
    17. return m_list.at(index.row());
    18. }
    19. return QVariant();
    20. }
    21.  
    22. private:
    23. QStringList m_list;
    24. };
    25.  
    26. class UnionModel : public QAbstractListModel
    27. {
    28. Q_OBJECT
    29. Q_PROPERTY(MatchMode matchMode READ matchMode WRITE setMatchMode)
    30. public:
    31. UnionModel(QObject* parent = 0)
    32. : QAbstractListModel(parent),
    33. m_matchMode(UnionModel::MatchInput) {
    34. m_applicationModel = new Model(QStringList() << "oneoneoneone" << "twotwotwotwo", this);
    35. m_inputModel = new Model(QStringList() << "oneoneone" << "twotwotwo", this);
    36. }
    37.  
    38. enum MatchMode { MatchApplications, MatchInput, MatchAll };
    39.  
    40. int rowCount(const QModelIndex& parent = QModelIndex()) const {
    41. Q_UNUSED(parent)
    42. switch (m_matchMode) {
    43. case UnionModel::MatchInput:
    44. return m_inputModel->rowCount();
    45. case UnionModel::MatchApplications:
    46. return m_applicationModel->rowCount();
    47. case UnionModel::MatchAll:
    48. return (m_inputModel->rowCount() + m_applicationModel->rowCount());
    49. }
    50. return 0;
    51. }
    52.  
    53. QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const {
    54. if (!index.isValid())
    55. return QVariant();
    56.  
    57. if (role == Qt::DisplayRole) {
    58. if (m_matchMode == MatchInput) {
    59. return m_inputModel->data(index);
    60. }
    61.  
    62. else if (m_matchMode == MatchApplications) {
    63. return m_applicationModel->data(index);
    64. }
    65.  
    66. if (index.row() < m_inputModel->rowCount()) {
    67. return m_inputModel->data(index);
    68. }
    69.  
    70. QModelIndex idx = m_applicationModel->index(qAbs(index.row() - m_inputModel->rowCount()));
    71. return m_applicationModel->data(idx);
    72. }
    73.  
    74. return QVariant();
    75. }
    76. MatchMode matchMode() const {
    77. return m_matchMode;
    78. }
    79.  
    80. public slots:
    81. void setMatchMode(UnionModel::MatchMode mode) {
    82. if (m_matchMode != mode) {
    83. m_matchMode = mode;
    84. emit matchModeChanged(mode);
    85. }
    86. }
    87.  
    88. private:
    89. MatchMode m_matchMode;
    90. Model* m_applicationModel;
    91. Model* m_inputModel;
    92.  
    93. signals:
    94. void matchModeChanged(UnionModel::MatchMode mode);
    95. };
    96.  
    97. class LineEdit : public QLineEdit
    98. {
    99. Q_OBJECT
    100. public:
    101. LineEdit(QWidget* parent = 0) : QLineEdit(parent) {
    102. QCompleter* c = new QCompleter(this);
    103. UnionModel* model = new UnionModel(c);
    104. c->setModel(model);
    105. c->setCompletionRole(Qt::DisplayRole);
    106. setCompleter(c);
    107. QObject::connect(this, SIGNAL(changeModeRequest(UnionModel::MatchMode)),
    108. model, SLOT(setMatchMode(UnionModel::MatchMode)));
    109.  
    110. connect(this, SIGNAL(textChanged(const QString&)),
    111. this, SLOT(onTextChanged(const QString&)));
    112. }
    113. private slots:
    114. void onTextChanged(const QString& text) {
    115. if (text.trimmed().isEmpty()) return;
    116.  
    117. UnionModel::MatchMode mode;
    118. int len = text.length();
    119.  
    120. if (len < 3) {
    121. mode = UnionModel::MatchInput;
    122. }
    123.  
    124. else if (len >= 3 && len <= 6) {
    125. mode = UnionModel::MatchApplications;
    126. }
    127.  
    128. else {
    129. mode = UnionModel::MatchAll;
    130. }
    131.  
    132. emit changeModeRequest(mode);
    133. }
    134. signals:
    135. void changeModeRequest(UnionModel::MatchMode mode);
    136. };
    137.  
    138. int main(int argc, char** argv)
    139. {
    140. QApplication app(argc, argv);
    141. LineEdit le;
    142. le.show();
    143. return app.exec();
    144. }
    145.  
    146. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

    p.s. Maybe I have to call a function to tell the view/completer etc. that the rowCount() has changed ...
    Last edited by momesana; 29th May 2008 at 14:25.

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    258
    Thanks
    22
    Thanked 19 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QCompleter and a UnionModel query internal submodels

    emitting layoutAboutToBeChanged and layoutChanged solved it. Thanks nonetheless.

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.