QScintilla get the QStringlist from autocompletion - qt

I am currently using QScintilla to develop my own code editor.
QScintilla provide autocompletion and calltips feature already. What I want is to get rid of the autocompletion popup widget , retrieve the list it gets, parse and display them with my own widget.
So here is what I've got:
//disable the autocompletion feature:
QsciScintilla::setAutoCompletionSource( QsciScintilla::AcsNone);
//use this function call to get the list from api class:
void updateAutoCompletionList (const QStringList &context, QStringList &list)
The problem is ,the updateAutoCompletionList only give me the result from QsciScintilla::AcsAPIs, I have no way to get the result from QsciScintilla::AcsDocument.
Is there any way I could get result from QsciScintilla::AcsDocument?
thanks beforehand.

I've solved it myself.
No available API for me to do so, so I read the source code of QsciScintilla class, which has a method called "startAutoCompletion", you can get the answer from that.
Actually it does search all text, collects the words that match the autocompletion context,avoids duplicates. Not that fancy as I throught, but actually the performance is not as slow as I throught either :)

Related

How to automatically update a QLineEdit with the value of another variable

I've got an std::string object called str;
I've also got an QLineEdit object called line_edit.
I need that str reflects whatever the user writes in line_edit, this I know how to do trough signals and slots.
But I also need that whenever str changes , QLineEdit automatically displays the new contents of str.
How can I do this?
Is it MVC what I need? I've been reading about it but I can't get it yet.
Also, examples I've read try to keep mutually updated subobjects of QWidget
which makes me think there is some magic happening in there.
How can I achieve that reactivity?
First of all it might be easier to use QString instead of std::string.
You can call line_edit->setText(str); to update line_edit from your code.
You can use the signal QLineEdit::textChanged to modify the content of str when writing to the QLineEdit.
There are multiple ways of handling signals, one way would be to use QObject::connect
It might look like this: QObject::connect(line_edit, &QLineEdit::textChanged, this, &YourClassName::setText);
And then you create setText(){ str = line_edit->text}

Qt: detecting a non-translated QString (not from a tr())

What chances do I got in finding all QStrings that do miss a tr()-translate call in a very old and very huge Application?
I was thinking something like... make a special .ts file for debugging use only and add a static prefix to all translations. Then put Qt in some sort of debug mode and list all its QString to qDebug or whatever—with the chance of filtering for everything that's missing the static prefix from the debug translation file. Is this possible?
Or: is there a better way to find untranslated QString occurrences? Thanks
For a big old project, this is probably a lot of work, but one way is below:
First see here, you want this:
QT_NO_CAST_FROM_ASCII disables automatic conversions from C string literals and pointers to Unicode.
Define that for your project, and then you can't write C string literals where QString is expected. Then you will probably get a bunch of build errors, which you fix by doing explicit conversion, using tr where you want.
After that, or perhaps before that to avoid going through same strings twice, do find-in-files (Ctrl+Shift+F in Qt Creator) for regexps like QString.*", and check them all for need of tr.

Qt - signal for when QListWidget row is edited?

I am working in Qt4.7, and I have a QListWidget in my dialog. I have a QString that needs to match the current text in the row of this widget (the individual rows are editable). Looking at the signals associated with QListWidget, there seem to be signals for when a different index is selected but none for when the text of a the currently selected row changes. I thought currentTextChanged(QString) would do it, but it didn't. I also thought to try to connect each individual row to something, but QListWidgetItem doesn't have any built-in signals. Does anyone know of a way to do this? Thanks!
At first it seems like QListWidget::itemChanged is the way to go, but soon you run into a problem: the signal is sent for everything - inserts, changing colors, checking boxes, and anything else that "changes" the item! Predelnik pointed that out in his answer. Some people have tried to put in flags and filter everywhere by intercepting various signals to find out if editing was the actual event. It gets very messy.
There is also QAbstractItemModel::dataChanged , which would seem like a good solution. It even has a parameter "const QVector& lstRoles" so you could scan for Qt::EditRole and see if it was really edited. Alas, there's a catch - it gets called for everything just like QListWidget::itemChanged and unfortunately, for QListWidget anyway, the roles parameter is always empty when it's called (I tried it). So much for that idea...
Fortunately, there's still hope... This solution does the trick! :
http://falsinsoft.blogspot.com/2013/11/qlistwidget-and-item-edit-event.html
He uses QAbstractItemDelegate::closeEditor, but I prefer using QAbstractItemDelegate::commitData.
So make a connect like so...
connect(ui.pLstItems->itemDelegate(), &QAbstractItemDelegate::commitData, this, &MyWidget::OnLstItemsCommitData);
Then implement the slot like this...
void MyWidget::OnLstItemsCommitData(QWidget* pLineEdit)
{
QString strNewText = reinterpret_cast<QLineEdit*>(pLineEdit)->text();
int nRow = ui.pLstItems->currentRow();
// do whatever you need here....
}
Now you have a slot that gets called only when the list item's text has been edited!
I guess you need to look into the following signal:
void QListWidget::itemChanged(QListWidgetItem * item)
But be careful because it's being sent every time some property of item changed, not only text. I remember when we ran into the problem once when we changed item colors and got tons of false positive slots called because of that. If you need more fine tuning I guess it's better to write model/view classes yourself and not rely on QListWidget.

Updating a model in the backend in qt

I am new to Qt.
I have the following situation.
Can someone suggest me how to tackle it.Thanks
I have a custom made model which i should populate in the backend.ie when the program starts there is no guarantee there will be elements in the model.But these elements may be added later in the backend.So if i add a new element in the data structure in the model how do i let the view know the change.And this is an insertion and not a change in the data.
Also if i want the view i display to be sorted how should i notify the view
To update any connected views of an insertion, call beginInsertRows(const QModelIndex& parent, int first, int last) before you modify the model, and endInsertRows() after. Read the docs.
To sort views you need to use a QAbstractProxyModel, it is highly configurable so read the docs before asking more specific questions about it.

Populating a QComboBox and a QTable[View|Widget] from javascript

I'm trying to write a script for an application developed with Qt, using javascript for the business logic and a .ui file for the GUI, but I'm facing two problems.
In the ui I declared a QComboBox, to which I successfully connect javascript functions to handle
signals such as editTextChanged, etc. I was wondering I cannot populate the combobox from within
javascript code, because the addItem function is not exposed to script-side code.
combobox.editTextChanged[action](ComboBoxChanged); // OK (action is "connect" or "disconnect")
combobox.addItem("element 1"); // Error!
Is there any (other) way to do this?
I need to show a set of items (strings) in a table-like component. I tried using a QTableView and
QTableWidget but I cannot insert or get items. For example, from javascript I cannot access the
setModel function of a QTableView (if at least I could create a QAbstractItemModel from
script...), neither I can access the item(row,col) function of a QTableWidget class, to set an
item's text. Is there any way to show a table of strings to the user, let edit them and retrieve
the modified contents?
Thanks in advance.
Antonio
Because the addItem() function isn't a slot, you'll need an intermediate public slot to handle the transaction. It'll be the same with the other functions you are trying to get at as well.

Resources