Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Storing alias name in QCompleter's item?



  • 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.


  • Lifetime Qt Champion

    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.



  • @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
        });
    
    

  • Lifetime Qt Champion

    @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 :)



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

    Qt::DisplayRole

    Thanks


Log in to reply