• <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 逛奔的蝸牛 閱讀(8775) 評(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)論
              

            伊人久久大香线蕉精品不卡| 久久精品国产99久久久| 狠狠色婷婷综合天天久久丁香| 国产亚洲美女精品久久久| 久久香蕉国产线看观看乱码| 99久久国产综合精品成人影院| 久久久WWW成人免费精品| 久久久无码精品亚洲日韩蜜臀浪潮 | 欧美精品一区二区久久| 99久久免费只有精品国产| 久久91精品国产91久久户| 97久久综合精品久久久综合| 久久99国产综合精品| 国产午夜免费高清久久影院| 996久久国产精品线观看| 久久精品毛片免费观看| 国产精品久久影院| 国产亚洲美女精品久久久| 亚洲午夜福利精品久久| 久久久这里有精品| 久久99国产精品尤物| 91精品日韩人妻无码久久不卡| 久久精品亚洲精品国产欧美| 久久国产成人| 久久久久亚洲AV无码麻豆| 办公室久久精品| 久久精品中文无码资源站| 97久久超碰国产精品2021| 久久久免费观成人影院| 囯产极品美女高潮无套久久久 | 国产成人无码精品久久久性色| 国产三级久久久精品麻豆三级 | 欧美久久一级内射wwwwww.| 亚洲精品乱码久久久久久久久久久久| AAA级久久久精品无码片| 欧美无乱码久久久免费午夜一区二区三区中文字幕 | 久久亚洲精品成人AV| 国产成人久久久精品二区三区 | 韩国无遮挡三级久久| 亚洲а∨天堂久久精品9966| 99久久无码一区人妻a黑|