Results 1 to 8 of 8

Thread: How to enable completion by symbols \s \t \n in QLineEdit?

  1. #1
    Join Date
    Oct 2007
    Posts
    4
    Thanks
    3
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Question How to enable completion by symbols \s \t \n in QLineEdit?

    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
    Attached Files Attached Files

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to enable completion by symbols \s \t \n in QLineEdit?

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

  3. The following user says thank you to wysota for this useful post:

    NameSurname (21st October 2007)

  4. #3
    Join Date
    Oct 2007
    Posts
    4
    Thanks
    3
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to enable completion by symbols \s \t \n in QLineEdit?

    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
    Attached Images Attached Images

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to enable completion by symbols \s \t \n in QLineEdit?

    Is this what you want?
    Qt Code:
    1. #include <QApplication>
    2. #include <QCompleter>
    3. #include <QStringListModel>
    4. #include <QLineEdit>
    5.  
    6. int main(int argc, char **argv){
    7. QApplication app(argc, argv);
    8. QStringListModel model(QStringList() << "\\s" << "\\t" << "\\n");
    9. com.setModel(&model);
    10. le.setCompleter(&com);
    11. le.show();
    12. return app.exec();
    13. }
    To copy to clipboard, switch view to plain text mode 

  6. The following user says thank you to wysota for this useful post:

    NameSurname (21st October 2007)

  7. #5
    Join Date
    Oct 2007
    Posts
    4
    Thanks
    3
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to enable completion by symbols \s \t \n in QLineEdit?

    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

  8. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to enable completion by symbols \s \t \n in QLineEdit?

    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.

  9. The following user says thank you to wysota for this useful post:

    NameSurname (21st October 2007)

  10. #7
    Join Date
    Oct 2007
    Posts
    4
    Thanks
    3
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to enable completion by symbols \s \t \n in QLineEdit?

    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
    Attached Images Attached Images

  11. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to enable completion by symbols \s \t \n in QLineEdit?

    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.

Similar Threads

  1. Unhandled exception in qatomic
    By NewGuy in forum Qt Programming
    Replies: 14
    Last Post: 23rd July 2013, 09:49
  2. KDE 3.5.0 crash while opening project
    By MarkoSan in forum KDE Forum
    Replies: 2
    Last Post: 19th October 2007, 16:21
  3. QWT 5, QT3, SuSE 10.2. Crash and burn
    By DrMcCleod in forum Qwt
    Replies: 8
    Last Post: 7th September 2007, 20:53

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.