Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QCompleter with a QSqlTableModel [Solved]
Forum Updated to NodeBB v4.3 + New Features

QCompleter with a QSqlTableModel [Solved]

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 7.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    luca
    wrote on last edited by
    #1

    Hi all,
    I need to use a QCompleter in a QLineEdit fetching data from a QSqlTableModel.

    I have a QSqlTableModel that I use to show data in a QTableView, and the model has 5 column.

    I'd like to use the same model (its second column) to load data in the completer. Is it possible?

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dangelog
      wrote on last edited by
      #2

      Sure. See QCompleter::setModel + QCompleter::setCompletionColumn.

      Software Engineer
      KDAB (UK) Ltd., a KDAB Group company

      1 Reply Last reply
      0
      • L Offline
        L Offline
        loladiro
        wrote on last edited by
        #3

        Absolutely, you can set the same model you use for your tableview to the completer using setModel() and select the Column using setCompletionColumn().

        1 Reply Last reply
        0
        • L Offline
          L Offline
          luca
          wrote on last edited by
          #4

          Thanks a lot, I didn't found it.
          The problem was that in docs there is:
          @
          ...
          The model can be list model or a tree model ...
          @

          1 Reply Last reply
          0
          • L Offline
            L Offline
            luca
            wrote on last edited by
            #5

            Now when I select a row from the completer popup I need to fetch the corresponding row in the QSqlTableModel.
            I connected this:
            @
            connect(my_completer, SIGNAL(activated(QModelIndex)), SLOT(setTableData(QModelIndex)));
            @

            And I'm trying to get the row this way:
            @
            setTableData(QModelIndex index)
            {
            int model_row;
            model_row = index.row();
            qDebug() << model_row;
            ....
            }
            @
            But I always get the row in the popup, not in the model. For example if the popup has 4 row and I select the second, I get model_row==1 while in the model it's the row 52.

            What do I wrong?

            1 Reply Last reply
            0
            • L Offline
              L Offline
              luca
              wrote on last edited by
              #6

              I found this solution:
              @
              setTableData(QModelIndex index)
              {
              ....
              QModelIndex index_table_model = qobject_cast<QAbstractProxyModel*> (m_Completer->completionModel())->mapToSource(index);
              int table_model_row;
              table_model_row = index_table_model.row();
              ....
              }
              @

              and it works...

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved