Storing alias name in QCompleter's item?
-
I have a
QCompleter
with the tree model for aQPlainTextEdit
. 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 likemodule1 -> Func1
in the popup, and then have it outputFunc1(abcdefghijklmnopq)
to the textedit. -
Hi
We could store extra string via another role ( a custom role)
https://doc.qt.io/qt-5/qstandarditem.html#setDataand 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. -
Hi
We could store extra string via another role ( a custom role)
https://doc.qt.io/qt-5/qstandarditem.html#setDataand 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 });
-
@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 });
Oh wait
pFunctionItem->setData(function.fullName, 1);
pFunctionItem->setData(function.name, 2); // shorter versionshould 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 :)
-
Oh wait
pFunctionItem->setData(function.fullName, 1);
pFunctionItem->setData(function.name, 2); // shorter versionshould 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 :)