PDA

View Full Version : How to enable completion by symbols \s \t \n in QLineEdit?



NameSurname
17th October 2007, 14:32
Hi to everyone!

Please tell me how enable completion by white space symbols \s \t \n in QLineEdit as in $QTDIR\examples\tool\completer\ wchich is using QDirModel for completing directories?
I know that it is required to use QCompletion class.
From this example I have learned how to use it, but I think there are no model for this purpose.
As in QDirModel I have tried to use QAbstractItemModel as it ancestor.
This very small project of WhiteSpaceEdit control in the attached file WhiteSpaceEdit.zip.
But my model is not working.
So please tell me what I should correct in this model?

Kind regards,
Valery

wysota
20th October 2007, 23:00
I'm not exactly sure what you are trying to do, but looking at your model I'm pretty sure you can substitute it with QStringListModel. I don't know if you'll get the result you are after, though...

NameSurname
21st October 2007, 07:37
Hi dear Wysota!
Thank you very much for your reply!

For the sake of clearing up what I want from such model I attach the figure of a control with desirable functionality. (I want to make my model working as QDirModel, but without icons an in place of directory names should appear symbols s t n (space, tab, line feed).

Kind regards,
Valery

wysota
21st October 2007, 09:45
Is this what you want?

#include <QApplication>
#include <QCompleter>
#include <QStringListModel>
#include <QLineEdit>

int main(int argc, char **argv){
QApplication app(argc, argv);
QLineEdit le;
QCompleter com;
QStringListModel model(QStringList() << "\\s" << "\\t" << "\\n");
com.setModel(&model);
le.setCompleter(&com);
le.show();
return app.exec();
}

NameSurname
21st October 2007, 15:24
Hi again dear Wysota!
Thank you very much for your reply!

But your code which I have inserted to my project (here) doesn't work at all with my Qt 4.3.1 and possible other versions.

whitespaceedit.h content here:

//---------------------------------------------------------------------------
#ifndef WHITESPACEEDIT_H
#define WHITESPACEEDIT_H
//---------------------------------------------------------------------------
#include<QLineEdit>
#include<QCompleter>
#include<QStringListModel>
//---------------------------------------------------------------------------
class WhiteSpaceEdit : public QLineEdit
{
Q_OBJECT
public:
WhiteSpaceEdit(QWidget *Parent = 0);
~WhiteSpaceEdit();
};
//---------------------------------------------------------------------------
#endif// WHITESPACEEDIT_H
//---------------------------------------------------------------------------

whitespaceedit.cpp content here:

//---------------------------------------------------------------------------
#include"whitespaceedit.h"
//---------------------------------------------------------------------------
WhiteSpaceEdit::WhiteSpaceEdit(QWidget *Parent) : QLineEdit(Parent)
{
QCompleter Completer;
QStringListModel Model(QStringList() << "\\t" << "\\s" << "\\n");
Completer.setModel(&Model);
setCompleter(&Completer);
setDragEnabled(true);
}
//---------------------------------------------------------------------------
WhiteSpaceEdit::~WhiteSpaceEdit()
{
}
//---------------------------------------------------------------------------

I think that it is required to modify QStringListModel to fit my needs.
But I do not know how to do it, because I do not know the matter of its methods.

Kind regards,
Valery

wysota
21st October 2007, 16:04
This code won't work because you create the completer object on stack, so it gets out of scope and gets deleted when you exit the constructor. Create it on heap (using the "new" operator) instead. In my code the completer was created inside the scope of main(), so that was not the problem - your issue is strictly C++ and has nothing to do with Qt.

NameSurname
21st October 2007, 20:30
Hi again!
Thank you very much for you reply!

I have corrected my code (it works but in wrong way (it fails to list completion entries after I have entered on of them in my QLineEdit (please, see attached file Figure2.gif in comparison with wanted functionality in the Figure1.gif))):

whitespaceedit.cpp:

//---------------------------------------------------------------------------
#include"whitespaceedit.h"
//---------------------------------------------------------------------------
WhiteSpaceEdit::WhiteSpaceEdit(QWidget *Parent) : QLineEdit(Parent)
{
FEntries = new QStringList();
FEntries->append("\\t");
FEntries->append("\\s");
FEntries->append("\\n");
FModel = new QStringListModel(*FEntries);
FCompleter = new QCompleter();
FCompleter->setModel(FModel);
setCompleter(FCompleter);
}
//---------------------------------------------------------------------------
WhiteSpaceEdit::~WhiteSpaceEdit()
{
delete FEntries;
delete FModel;
delete FCompleter;
}
//---------------------------------------------------------------------------

whitespaceedit.h:

//---------------------------------------------------------------------------
#ifndef WHITESPACEEDIT_H
#define WHITESPACEEDIT_H
//---------------------------------------------------------------------------
#include<QLineEdit>
#include<QStringList>
#include<QStringListModel>
#include<QCompleter>
//---------------------------------------------------------------------------
class WhiteSpaceEdit : public QLineEdit
{
Q_OBJECT
public:
WhiteSpaceEdit(QWidget *Parent = 0);
~WhiteSpaceEdit();
private:
QStringList *FEntries;
QStringListModel *FModel;
QCompleter *FCompleter;
};
//---------------------------------------------------------------------------
#endif// WHITESPACEEDIT_H
//---------------------------------------------------------------------------

Kind regards,
Valery

wysota
21st October 2007, 20:39
Please try to restrain yourself from colouring the post manually. It looks pretty "happy", but please use the [code] tags next time.

From what I understand you want to enter an unlimited number of \[stn] combinations, right? In that case I suggest trying one of two solutions:
1. setting the completion prefix manually in a slot connected to the textChanged signal of the line edit
2. modifying the model so that the string list model acts like it had parents and children (pointing to the same data)

BTW. Your code leaks memory - you don't have to create the string list on heap, you pass a copy of the object to the model anyway, so it has its own private copy and the original can be destroyed when going out of scope.