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. Storing alias name in QCompleter's item?

Storing alias name in QCompleter's item?

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 311 Views
  • 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
    lansing
    wrote on last edited by
    #1

    I have a QCompleter with the tree model for a QPlainTextEdit. I have set it up so that on typing, a popup with the list will appear at the spot. When I'm done selecting, my selection will be pasted in the textEdit. The problem is that in my list, I have some very long strings, that makes the pop up width to stretch over my entire screen. How do I store the strings in shorter version in the popup but then give the original version when it was pasted?

    // adding items
    for (const auto & module : myList) {
        QStandardItem * pModuleItem = new QStandardItem(module.name);
        pRootItem->appendRow(pModuleItem);
    
        for(const auto & function : module.functions)
        {
            QStandardItem * pFunctionItem = new QStandardItem(function.name); // some of the names are too long
            pModuleItem->appendRow(pFunctionItem);
        }
    }
    

    When I select like module1 -> Func1(abcdefghijklmnopq), I want it to look like module1 -> Func1 in the popup, and then have it output Func1(abcdefghijklmnopq) to the textedit.

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      We could store extra string via another role ( a custom role)
      https://doc.qt.io/qt-5/qstandarditem.html#setData

      and then use
      https://doc.qt.io/qt-5/qcompleter.html#activated-1
      to get the index, then grab the other ROLE's text and then insert that into the QPlainTextEdit.

      L 1 Reply Last reply
      2
      • mrjjM mrjj

        Hi
        We could store extra string via another role ( a custom role)
        https://doc.qt.io/qt-5/qstandarditem.html#setData

        and then use
        https://doc.qt.io/qt-5/qcompleter.html#activated-1
        to get the index, then grab the other ROLE's text and then insert that into the QPlainTextEdit.

        L Offline
        L Offline
        lansing
        wrote on last edited by lansing
        #3

        @mrjj said in Storing alias name in QCompleter's item?:

        get the index, t

        Thanks I got it working

        QStandardItem * pFunctionItem = new QStandardItem();
        pFunctionItem->setData(function.fullName, 1);
        pFunctionItem->setData(function.name, 2); // shorter version
        pModuleItem->appendRow(pFunctionItem);
        
        /// in the signal
        connect(completer, QOverload<const QModelIndex &>::of(&QCompleter::activated),
            [=](const QModelIndex &index){  
                qDebug () << index.data(1); // print out the longer version
            });
        
        
        mrjjM 1 Reply Last reply
        1
        • L lansing

          @mrjj said in Storing alias name in QCompleter's item?:

          get the index, t

          Thanks I got it working

          QStandardItem * pFunctionItem = new QStandardItem();
          pFunctionItem->setData(function.fullName, 1);
          pFunctionItem->setData(function.name, 2); // shorter version
          pModuleItem->appendRow(pFunctionItem);
          
          /// in the signal
          connect(completer, QOverload<const QModelIndex &>::of(&QCompleter::activated),
              [=](const QModelIndex &index){  
                  qDebug () << index.data(1); // print out the longer version
              });
          
          
          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #4

          @lansing

          Oh wait

          pFunctionItem->setData(function.fullName, 1);
          pFunctionItem->setData(function.name, 2); // shorter version

          should be maybe be

          pFunctionItem->setData(function.fullName, Qt::UserRole );
          pFunctionItem->setData(function.name,Qt::DisplayRole);

          to follow
          enum ItemDataRole { DisplayRole, DecorationRole, EditRole, ToolTipRole, StatusTipRole, …, UserRole }.

          and be easier to read for others :)

          L 1 Reply Last reply
          2
          • mrjjM mrjj

            @lansing

            Oh wait

            pFunctionItem->setData(function.fullName, 1);
            pFunctionItem->setData(function.name, 2); // shorter version

            should be maybe be

            pFunctionItem->setData(function.fullName, Qt::UserRole );
            pFunctionItem->setData(function.name,Qt::DisplayRole);

            to follow
            enum ItemDataRole { DisplayRole, DecorationRole, EditRole, ToolTipRole, StatusTipRole, …, UserRole }.

            and be easier to read for others :)

            L Offline
            L Offline
            lansing
            wrote on last edited by
            #5

            @mrjj said in Storing alias name in QCompleter's item?:

            Qt::DisplayRole

            Thanks

            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