• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            逛奔的蝸牛

            我不聰明,但我會(huì)很努力

               ::  :: 新隨筆 ::  ::  :: 管理 ::

            -------------------------------------CompleteLineEdit.h-------------------------------------

            #ifndef COMPLETELINEEDIT_H

            #define COMPLETELINEEDIT_H


            #include <QtGui/QLineEdit>

            #include <QStringList>


            class QListView;

            class QStringListModel;

            class QModelIndex;


            class CompleteLineEdit : public QLineEdit {

                Q_OBJECT

            public:

                CompleteLineEdit(QStringList words, QWidget *parent = 0);


            public slots:

                void setCompleter(const QString &text); // 動(dòng)態(tài)的顯示完成列表

                void completeText(const QModelIndex &index); // 點(diǎn)擊完成列表中的項(xiàng),使用此項(xiàng)自動(dòng)完成輸入的單詞


            protected:

                virtual void keyPressEvent(QKeyEvent *e);

                virtual void focusOutEvent(QFocusEvent *e);


            private:

                QStringList words; // 整個(gè)完成列表的單詞

                QListView *listView; // 完成列表

                QStringListModel *model; // 完成列表的model

            };


            #endif // COMPLETELINEEDIT_H


            -------------------------------------CompleteLineEdit.cpp-------------------------------------

            #include "CompleteLineEdit.h"

            #include <QKeyEvent>

            #include <QtGui/QListView>

            #include <QtGui/QStringListModel>

            #include <QDebug>


            CompleteLineEdit::CompleteLineEdit(QStringList words, QWidget *parent)

                : QLineEdit(parent), words(words) {

                listView = new QListView(this);

                model = new QStringListModel(this);

                listView->setWindowFlags(Qt::ToolTip);


                connect(this, SIGNAL(textChanged(const QString &)), this, SLOT(setCompleter(const QString &)));

                connect(listView, SIGNAL(clicked(const QModelIndex &)), this, SLOT(completeText(const QModelIndex &)));

            }


            void CompleteLineEdit::focusOutEvent(QFocusEvent *e) {

                //listView->hide();

            }


            void CompleteLineEdit::keyPressEvent(QKeyEvent *e) {

                if (!listView->isHidden()) {

                    int key = e->key();

                    int count = listView->model()->rowCount();

                    QModelIndex currentIndex = listView->currentIndex();


                    if (Qt::Key_Down == key) {

                        // 按向下方向鍵時(shí),移動(dòng)光標(biāo)選中下一個(gè)完成列表中的項(xiàng)

                        int row = currentIndex.row() + 1;

                        if (row >= count) {

                            row = 0;

                        }


                        QModelIndex index = listView->model()->index(row, 0);

                        listView->setCurrentIndex(index);

                    } else if (Qt::Key_Up == key) {

                        // 按向下方向鍵時(shí),移動(dòng)光標(biāo)選中上一個(gè)完成列表中的項(xiàng)

                        int row = currentIndex.row() - 1;

                        if (row < 0) {

                            row = count - 1;

                        }


                        QModelIndex index = listView->model()->index(row, 0);

                        listView->setCurrentIndex(index);

                    } else if (Qt::Key_Escape == key) {

                        // 按下Esc鍵時(shí),隱藏完成列表

                        listView->hide();

                    } else if (Qt::Key_Enter == key || Qt::Key_Return == key) {

                        // 按下回車鍵時(shí),使用完成列表中選中的項(xiàng),并隱藏完成列表

                        if (currentIndex.isValid()) {

                            QString text = listView->currentIndex().data().toString();

                            setText(text);

                        }


                        listView->hide();

                    } else {

                        // 其他情況,隱藏完成列表,并使用QLineEdit的鍵盤按下事件

                        listView->hide();

                        QLineEdit::keyPressEvent(e);

                    }

                } else {

                    QLineEdit::keyPressEvent(e);

                }

            }


            void CompleteLineEdit::setCompleter(const QString &text) {

                if (text.isEmpty()) {

                    listView->hide();

                    return;

                }


                if ((text.length() > 1) && (!listView->isHidden())) {

                    return;

                }


                // 如果完整的完成列表中的某個(gè)單詞包含輸入的文本,則加入要顯示的完成列表串中

                QStringList sl;

                foreach(QString word, words) {

                    if (word.contains(text)) {

                        sl << word;

                    }

                }


                model->setStringList(sl);

                listView->setModel(model);


                if (model->rowCount() == 0) {

                    return;

                }


                // Position the text edit

                listView->setMinimumWidth(width());

                listView->setMaximumWidth(width());


                QPoint p(0, height());

                int x = mapToGlobal(p).x();

                int y = mapToGlobal(p).y() + 1;


                listView->move(x, y);

                listView->show();

            }


            void CompleteLineEdit::completeText(const QModelIndex &index) {

                QString text = index.data().toString();

                setText(text);

                listView->hide();

            }


            -------------------------------------main.cpp----------------------------------

            #include <QtGui/QApplication>

            #include "CompleteLineEdit.h"

            #include <QtGui>

            #include <QCompleter>

            #include <QStringList>


            int main(int argc, char *argv[]) {

                QApplication a(argc, argv);


                QStringList sl = QStringList() << "Biao" << "Bin" << "Huang" << "Hua" << "Hello" << "BinBin" << "Hallo";

                QWidget widgetw;

                CompleteLineEdit * edit= new CompleteLineEdit(sl);

                QPushButton *button = new QPushButton("Button");

                QHBoxLayout *layout = new QHBoxLayout();

                layout->addWidget(edit);

                layout->addWidget(button);

                widgetw.setLayout(layout);


                widgetw.show();


                CompleteLineEdit e(sl);

                e.show();


                return a.exec();

            }


            posted on 2009-10-31 02:09 逛奔的蝸牛 閱讀(8798) 評(píng)論(1)  編輯 收藏 引用 所屬分類: Qt

            評(píng)論

            # re: Qt:自動(dòng)完成的QLineEdit(非使用QCompleter版)[未登錄] 2012-02-25 17:39 tony
            這個(gè)代碼感覺bug比較多,我自己寫了一個(gè)稍微好一點(diǎn)的:http://download.csdn.net/detail/jyxpm/4089691  回復(fù)  更多評(píng)論
              

            色偷偷88欧美精品久久久 | 久久亚洲高清观看| 91久久精品国产91性色也| 久久久久一级精品亚洲国产成人综合AV区| 久久中文字幕视频、最近更新| 色婷婷综合久久久中文字幕| 26uuu久久五月天| 精品国产乱码久久久久久人妻| 精品999久久久久久中文字幕| 亚洲国产高清精品线久久 | 久久这里都是精品| 精品久久久久久国产| 人人狠狠综合久久亚洲高清| 久久国产高潮流白浆免费观看| 天天影视色香欲综合久久| 久久久久亚洲Av无码专| 亚洲欧美国产精品专区久久| 久久国产亚洲精品麻豆| 午夜久久久久久禁播电影| 国产免费福利体检区久久| 91精品国产综合久久婷婷| 狠狠色丁香久久婷婷综合图片| 精品久久久久久无码中文字幕 | 久久国产视屏| 久久成人18免费网站| 久久综合九色综合久99| 91精品国产91久久久久久蜜臀| 高清免费久久午夜精品| 久久天天躁狠狠躁夜夜96流白浆| 少妇人妻综合久久中文字幕| 久久亚洲欧洲国产综合| 久久婷婷五月综合色99啪ak| 国产三级精品久久| 日本亚洲色大成网站WWW久久| 精品乱码久久久久久夜夜嗨| 久久996热精品xxxx| 久久久久亚洲AV无码去区首| 亚洲国产成人久久综合碰| 欧美伊人久久大香线蕉综合| 亚洲中文字幕久久精品无码喷水| 欧美黑人又粗又大久久久|