QT 常用部件 (四)自动完成LineEdit
来源:互联网 发布:光信息科学与技术知乎 编辑:程序博客网 时间:2024/05/02 00:12
http://mobile.51cto.com/symbian-271180.htm
Qt 自动完成LineEdit是本文要介绍的内容,内容虽少,取其精华。简单的代码实现出很好的效果,先来看内容。
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); // 动态的显示完成列表
- void completeText(const QModelIndex &index); // 点击完成列表中的项,使用此项自动完成输入的单词
- protected:
- virtual void keyPressEvent(QKeyEvent *e);
- virtual void focusOutEvent(QFocusEvent *e);
- private:
- QStringList words; // 整个完成列表的单词
- 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) {
- // 按向下方向键时,移动光标选中下一个完成列表中的项
- 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) {
- // 按向下方向键时,移动光标选中上一个完成列表中的项
- 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键时,隐藏完成列表
- listView->hide();
- } else if (Qt::Key_Enter == key || Qt::Key_Return == key) {
- // 按下回车键时,使用完成列表中选中的项,并隐藏完成列表
- 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;
- }
- // 如果完整的完成列表中的某个单词包含输入的文本,则加入要显示的完成列表串中
- 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();
- }
小结:Qt 自动完成LineEdit的内容介绍介绍完了,效果是不是很满意,希望本文对你有所帮助。
- QT 常用部件 (四)自动完成LineEdit
- Qt 常用部件 (五) Model/View
- QT 常用部件
- Qt常用部件介绍
- 第二部分: Dijit 2.1 自动完成部件(Autocompletor) 。。。
- QT 常用部件 之窗体篇 widget 属性(一)
- QT 常用部件 (三)QTreeView 文件浏览器
- QT 常用部件 之窗体篇 widget 属性(一)
- 【Qt教程】Qt常用部件介绍
- qt 自定义 lineedit
- Qt LineEdit textChanged(QString)
- QT之LineEdit
- Qt笔记-qml-lineEdit
- QT lineedit 小疑问
- Qt常用的部件及相关函数
- qt之LineEdit的样式
- Qt小程序(四)-添加窗口部件到Qt Designer
- QT笔记(3)——Qt的lineEdit和textEdit的右键菜单汉化
- linux下用函数和系统调用分别实现计算文件长度(不解) 学习笔记
- 书单
- 类图画法 http://blog.csdn.net/xhf55555/article/details/6896316
- 入门视频采集与处理(BT656简介)
- String StringBuffer StringBuilder 三者的区别
- QT 常用部件 (四)自动完成LineEdit
- isbn
- 汇文问题汇总+ORACLE
- 关于写Windows service程序启动和停止需要注意的几个地方
- 【C#】程序分析——泛型
- 抠图
- opencv细节
- C++笔记
- ORM 的功能