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. How to use multi column model in QCompleter?
Forum Updated to NodeBB v4.3 + New Features

How to use multi column model in QCompleter?

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 2 Posters 1.1k Views 2 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.
  • D Offline
    D Offline
    deleted385
    wrote on last edited by
    #1

    I've used the stock code editor example of Qt. In the code editor I've added these:

    completer = new QCompleter(this);
    completer->setWidget(this);
    completer->setCaseSensitivity(Qt::CaseInsensitive);
    completer->setModel(model("://Keyword.txt"));
    

    and the model function returns this:

    QStandardItemModel *CodeEditor::model(const QString &fileName)
    {
        QFile file(fileName);
        file.open(QIODevice::ReadOnly);
        auto model = new QStandardItemModel(completer);
        model->setColumnCount(2);
        while (!file.atEnd()){
            auto line = file.readLine();
            if (!line.isEmpty()){
                auto col1 = new QStandardItem(QIcon(":/key.svg"), line.trimmed());
                auto col2 = new QStandardItem("Keyword");
                model->appendRow(QList<QStandardItem *>() << col1 << col2);
            }
        }
        file.close();
        ....
        return model;
    }
    

    So I've two columns in the model. First with an Icon and the second with static text. When the completer is triggered, it shows only the first column like this:
    x1.gif

    I want to show the second column and also want that column to be right aligned, grey font color and italic. How do you do that?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #6

      QCompleter::setPopup

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      D 1 Reply Last reply
      1
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Hi,

        You can create a QStyledItemDelegate that you set on the view used by the completer.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        D 1 Reply Last reply
        1
        • SGaistS SGaist

          Hi,

          You can create a QStyledItemDelegate that you set on the view used by the completer.

          D Offline
          D Offline
          deleted385
          wrote on last edited by
          #3

          @SGaist, where is the second column? Why doesn't it show that?

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #4

            Because the widget set as popup is likely a QListView.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            D 1 Reply Last reply
            1
            • SGaistS SGaist

              Because the widget set as popup is likely a QListView.

              D Offline
              D Offline
              deleted385
              wrote on last edited by
              #5

              @SGaist, I haven't changed anything. That QListView probably is the default. How to replace that view with something else that is able to accommodate the second column?

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #6

                QCompleter::setPopup

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                D 1 Reply Last reply
                1
                • SGaistS SGaist

                  QCompleter::setPopup

                  D Offline
                  D Offline
                  deleted385
                  wrote on last edited by deleted385
                  #7

                  @SGaist, very close to what I wanted with QTableView. There's a small gap between row cells and it's clearly visible when an item is selected:

                  x2.gif

                  still couldn't find a way to remove that gap.

                  One more issue is, it doesn't resize to content size automatically. I've set the width manually table->setFixedWidth(totalColumnWidth + 20). The 20 is for vertical scrollbar and when that becomes invisible there's another big gap at the end.

                  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