Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] ListView not closed when the Application is closed

[SOLVED] ListView not closed when the Application is closed

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 1.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Macro
    wrote on last edited by
    #1

    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)!

    1 Reply Last reply
    0
    • raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      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

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • M Offline
        M Offline
        Macro
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved