Completer of QLineEdit get Memory
I have q method that shown at below:
void MainWindow::slotResults( const QList<QSqlRecord>& records )
{
ui->lineEditWord->setCompleter(0);
QStringList wordList;
for(int i = 0; i < records.count(); i++)
{
wordList.append( QString("%1").arg( records.value(i).value(0).toString()));
}
QCompleter *completer = new QCompleter(开发者_StackOverflow社区wordList, this);
// completer->setCaseSensitivity(Qt::CaseInsensitive);
ui->lineEditWord->setCompleter(completer);
}
But, when line ui->lineEditWord->setCompleter(completer) have beeen execited; memory usage increase and when i call this method for several times, memory usage grow up. so how can i release memory from this? Should i remove current completer of lineEdit
pls help
The QCompleter
constructor you are using takes a QStringList
as parameter. It is certainly (while this is not documented properly), a convenience constructor that creates a QStringListModel
filled with the strings passed to the constructor and set this model as the completion model with QCompleter::setModel()
.
You can update the string list represented by the model using the following code:
QStringList originalStringList;
originalStringList << "red" <<"orange" << "yellow";
QCompleter *completer = new QCompleter(originalStringList);
QStringListModel *stringListModel = qobject_cast< QStringListModel* >(completer->model());
QStringList newStringList;
newStringList << "blue" <<"green" << "purple";
stringListModel->setStringList(wewStringList);
If you want to be sure of what you are doing, I suggest to create the completer and data model separately:
QCompleter *completer = new QCompleter();
QStringListModel *stringListModel = new QStringListModel();
completer->setModel(stringListModel);
QStringList originalStringList;
originalStringList << "red" <<"orange" << "yellow";
stringListModel->setStringList(originalStringList);
In this case, you just have to store the stringListModel as a member of your MainWindow and update the string list each time you pass through the MainWindow::slotResults()
method.
You are allocating a QCompleter
on each pass through this method. Unless the QLineEdit
is deallocating it this is a memory leak
One solution is to store the pointer as a member of MainWindow
then delete
it in ~MainWindow()
another way would be to have a smart pointer member to store it so the memory is automatically deleted when the window goes out of scope.
You really shouldn't need to create more than one completer. If you do just remember to delete the previous in whatever way works best for your implementation requirements.
This isn't documented, but if the old completer has the QLineEdit
as parent, when you set a new completer, the old completer is deleted automatically when you call QLineEdit::setCompleter
. You just have to create the completer like that:
QCompleter *completer = new QCompleter(wordList, ui->lineEditWord);
Since you are apparently creating the completer from a SQL query, you could use a QSqlQueryModel
or a QSqlTableModel
instead of the list of records and pass it to QCompleter
constructor, this way, you will only have to create the completer once and call QSqlQueryModel::setQuery
or QSqlTableModel::select
to update the completer whenever you make any change to that part of the database.
精彩评论