1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
| #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 = new QStringListModel(this); sourceModel->setStringList(QColor::colorNames()); proxyModel = new QSortFilterProxyModel(this); proxyModel->setSourceModel(sourceModel); proxyModel->setFilterKeyColumn(0); listView = new QListView; listView->setModel(proxyModel); listView->setEditTriggers(QAbstractItemView::NoEditTriggers); filterLabel = new QLabel(tr("要匹配的字符串")); filterLineEdit = new QLineEdit; filterLabel->setBuddy(filterLineEdit); syntaxLabel = new QLabel("匹配方式选择"); syntaxComboBox = new QComboBox; syntaxComboBox->addItem("正则表达式",QRegExp::RegExp); syntaxComboBox->addItem("通配符",QRegExp::Wildcard); syntaxComboBox->addItem("完全匹配",QRegExp::FixedString); 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); }
|