QT学习笔记之十四 StringMatch
2009-07-18 23:51
483 查看
用于正则表达式,通配符,完整匹配三种方式
============================================================
stringmatch.h
stringmatch.cpp
软件截图:
===========================================================
直到今天才把Qt4的中文乱码搞定太尴尬了.......鄙视下自己.....
只要程序中加入
当然要看情况来说,你的环境可能是GBK或者其他的...
============================================================
stringmatch.h
#ifndef STRINGMATCH_H #define STRINGMATCH_H #include <QtGui/QDialog> class QComboBox; class QLabel; class QLineEdit; class QListView; class QSortFilterProxyModel; class QStringListModel; namespace Ui { class StringMatch; } class StringMatch : public QDialog { Q_OBJECT public: StringMatch(QWidget *parent = 0); ~StringMatch(); private slots: void reapplyFilter(); private: Ui::StringMatch *ui; QStringListModel *sourceModel; QSortFilterProxyModel *proxyModel; QListView *listView; QLabel *filterLabel; QLabel *syntaxLabel; QLineEdit *filterLineEdit; QComboBox *syntaxComboBox; }; #endif // STRINGMATCH_H
stringmatch.cpp
#include <QtGui> #include "stringmatch.h" #include "ui_stringmatch.h" StringMatch::StringMatch(QWidget *parent) : QDialog(parent), ui(new Ui::StringMatch) { ui->setupUi(this); QTextCodec::setCodecForCStrings(QTextCodec::codecForLocale()); QTextCodec::setCodecForTr(QTextCodec::codecForName("utf8")); //sourceModel sourceModel = new QStringListModel(this); sourceModel->setStringList(QColor::colorNames()); //proxyModel proxyModel = new QSortFilterProxyModel(this); proxyModel->setSourceModel(sourceModel); proxyModel->setFilterKeyColumn(0); //listView listView = new QListView; listView->setModel(proxyModel); listView->setEditTriggers(QAbstractItemView::NoEditTriggers); //filter filterLabel = new QLabel(tr("要匹配的字符串")); filterLineEdit = new QLineEdit; filterLabel->setBuddy(filterLineEdit); //syntax syntaxLabel = new QLabel("匹配方式选择"); syntaxComboBox = new QComboBox; syntaxComboBox->addItem("正则表达式",QRegExp::RegExp); //A rich Perl-like pattern matching syntax. This is the default. syntaxComboBox->addItem("通配符",QRegExp::Wildcard); //This provides a simple pattern matching syntax similar to that used by shells (command interpreters) for "file globbing". syntaxComboBox->addItem("完全匹配",QRegExp::FixedString); //This is equivalent to using the RegExp pattern on a string in which all metacharacters are escaped using //当filterLineEdit和syntaxComboBox connect(filterLineEdit,SIGNAL(textChanged(const QString &)),this,SLOT(reapplyFilter())); connect(syntaxComboBox,SIGNAL(currentIndexChanged(int)),this,SLOT(reapplyFilter())); //布局 QGridLayout *layout = new QGridLayout; layout->addWidget(listView,0,0,1,2); layout->addWidget(filterLabel,1,0); layout->addWidget(filterLineEdit,1,1); layout->addWidget(syntaxLabel,2,0); layout->addWidget(syntaxComboBox,2,1); setLayout(layout); setWindowTitle(tr("String Matching")); } StringMatch::~StringMatch() { delete ui; } void StringMatch::reapplyFilter() { QRegExp::PatternSyntax syntax = QRegExp::PatternSyntax(syntaxComboBox->itemData( syntaxComboBox->currentIndex()).toInt()); QRegExp regExp(filterLineEdit->text(),Qt::CaseInsensitive,syntax); proxyModel->setFilterRegExp(regExp); }
软件截图:
===========================================================
直到今天才把Qt4的中文乱码搞定太尴尬了.......鄙视下自己.....
只要程序中加入
QTextCodec::setCodecForCStrings(QTextCodec::codecForLocale()); QTextCodec::setCodecForTr(QTextCodec::codecForName("utf8"));
当然要看情况来说,你的环境可能是GBK或者其他的...
相关文章推荐
- QT之GUI学习笔记(十四) -----事件(event)
- MCSE 2003学习笔记 十四 群集-应用程序服务器
- Qt Model/View 学习笔记 (三)
- cortex_m3_stm32嵌入式学习笔记(十四):RTC实时时钟(秒中断)
- Delphi的学习笔记十四——接口
- Windows环境下QT学习笔记02:QT Creator下新建项目
- Android开发学习笔记十四 异步线程之Handler
- QT之GUI学习笔记(十九)----QPainter
- Qt学习笔记--访问磁盘文件
- Qt Model/View 学习笔记 (二)
- Qt学习笔记之一,登录界面。
- JavaScript学习笔记(十四) 立即执行函数
- QT学习笔记10资源文件
- OpenGL_Qt学习笔记之_02(绘制简单平面几何图形)
- 【Qt学习笔记】禁止改变窗口大小
- Qt学习笔记:无边框窗体的移动
- Qt210时钟学习笔记
- HTML学习笔记(十四)HTML 框架
- Qt 学习笔记 TreeWidget 增删改
- 设计模式C++学习笔记之十四(Iterator迭代器模式)