[SOLVED] ListView not closed when the Application is closed



  • I have to set the Auto Completion for the QLineEdit. Here am using a QListView to display the suggestion list. Also am using QSortFilterProxyModel for sorting and filtering the items. Sorting and filtering works fine. The problem is when i close the application the ListView is not getting closed until i force close the list view. Herewith i have included my code and the snapshot of my output. How can i close the List View when the application is closed. Please suggest your views about it.

    LEAutoCompletion.h

    @#ifndef LEAUTOCOMPLETION_H
    #define LEAUTOCOMPLETION_H

    #include <QWidget>
    #include <QLineEdit>
    #include <QListView>
    #include <QModelIndex>

    namespace Ui {
    class LEAutoCompletion;
    }

    class LEAutoCompletion : public QWidget
    {
    Q_OBJECT

    public:
    explicit LEAutoCompletion(QWidget *parent = 0);
    ~LEAutoCompletion();

    private:
    Ui::LEAutoCompletion *ui;
    QLineEdit *mLineEdit;
    QListView *mView;

    protected:
    bool eventFilter(QObject *target, QEvent *event);

    private slots:
    void onTextChanged(const QString &text);
    void completeText(const QModelIndex &);
    void textModified();
    };

    #endif // LEAUTOCOMPLETION_H@

    LEAutoCompletion.cpp

    @#include "LEAutoCompletion.h"
    #include "ui_LEAutoCompletion.h"
    #include <QVBoxLayout>
    #include <QStringListModel>
    #include <QSortFilterProxyModel>
    #include <QEvent>
    #include <QKeyEvent>

    LEAutoCompletion::LEAutoCompletion(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::LEAutoCompletion)
    {
    ui->setupUi(this);
    mLineEdit = new QLineEdit(this);

    QVBoxLayout *layout = new QVBoxLayout(this);
    layout->addWidget(mLineEdit);
    setLayout(layout);
    
    knownList = NULL;
    
    setWindowTitle("Auto Completion");
    
    QStringList  list;
    list<< "America"<<"Arica"<<"Belgium"<<"Canada"<<"Denmark"<<"India"<<"France"<<"Australia";
    
    QStringListModel *model = new QStringListModel(this);
    model->setStringList(list);
    
    QSortFilterProxyModel *filterModel = new QSortFilterProxyModel(this);
    filterModel->setSourceModel(model);
    filterModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
    filterModel->sort(0,Qt::AscendingOrder);
    
    mView = new QListView(this);
    mView->setModel(filterModel);
    mView->setEditTriggers(QAbstractItemView::NoEditTriggers);
    mView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    mView->setSelectionBehavior(QAbstractItemView::SelectRows);
    mView->setSelectionMode(QAbstractItemView::SingleSelection);
    mView->setParent(0, Qt::ToolTip);
    mView->setFocusPolicy(Qt::NoFocus);
    mView->setFocusProxy(mLineEdit);
    
    connect(mLineEdit, SIGNAL(textChanged(QString)) , filterModel , SLOT(setFilterWildcard(QString)));
    connect(mLineEdit, SIGNAL(editingFinished()), this, SLOT(textModified()));
    connect(mLineEdit, SIGNAL(textChanged(QString)), this, SLOT(onTextChanged(const QString &)));
    connect(mView, SIGNAL(activated(const QModelIndex &)), this, SLOT(completeText(const QModelIndex &)));
    connect(mView, SIGNAL(clicked(const QModelIndex &)), this, SLOT(completeText(const QModelIndex &)));
    installEventFilter(this);
    

    }

    LEAutoCompletion::~LEAutoCompletion()
    {
    delete ui;
    delete mView;
    }

    bool LEAutoCompletion::eventFilter(QObject *target, QEvent *event)
    {
    if(target == this)
    {
    if(event->type() == QEvent::Resize || event->type() == QEvent::Move)
    {
    int lineEditWidth = mLineEdit->width();
    mView->setMinimumWidth(lineEditWidth);
    mView->setMaximumWidth(lineEditWidth);
    mView->setMaximumHeight(100);

            QPoint p(0, mLineEdit->height());
            int x = mLineEdit->mapToGlobal(p).x();
            int y = mLineEdit->mapToGlobal(p).y();
            mView->move(x, y);
        }
    }
    return QWidget::eventFilter(target, event);
    

    }

    void LEAutoCompletion::onTextChanged(const QString &text)
    {
    if(text.isEmpty())
    {
    mView->hide();
    }
    else
    {
    mView->show();
    }
    }

    void LEAutoCompletion::completeText(const QModelIndex &index)
    {
    mLineEdit->setText(index.data().toString());
    }

    void LEAutoCompletion::textModified()
    {
    QString text = mLineEdit->text();
    }
    @

    OUTPUT:

    ![IMG]http://i.imgur.com/Y4uKrOJ.png[/IMG](ListView Suggestion List)!

    Image of List View not Closed after the Application is closed.

    ![IMG]http://i.imgur.com/JLEQRnO.png[/IMG](ListView not closed after application closed)!


  • Moderators

    your problem is the following line:
    @mView->setParent(0, Qt::ToolTip);@

    Since you set the parent to 0 it wont get deleted when it's parent gets deleted. I assume you jsut want to set the window flag to Qt::ToolTip?
    You should use the "QWidget::setWindowFlags()":http://qt-project.org/doc/qt-4.8/qwidget.html#windowFlags-prop



  • Thanks a lot raven-worx. That works fine.... :-)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.