comparison src/filterwidget.cpp @ 4:e4748da7140b

Add filter functionalty to metadataview
author Andre Heinecke <andre.heinecke@intevation.de>
date Wed, 25 Mar 2015 14:23:46 +0100
parents
children 315e6988952a
comparison
equal deleted inserted replaced
3:248d5d1cdb38 4:e4748da7140b
1 #include "filterwidget.h"
2
3 #include <QComboBox>
4 #include <QHBoxLayout>
5 #include <QGroupBox>
6 #include <QSortFilterProxyModel>
7 #include <QAbstractItemModel>
8 #include <QLineEdit>
9 #include <QDebug>
10
11 FilterWidget::FilterWidget(QSortFilterProxyModel *model, QWidget *parent, Qt::WindowFlags f) :
12 QWidget(parent, f),
13 mModel(model) {
14 Q_ASSERT(model);
15 setupGUI();
16
17 connect(mModel, &QSortFilterProxyModel::sourceModelChanged,
18 this, &FilterWidget::headersChanged);
19 void (QComboBox:: *idxChanged)(int) = &QComboBox::currentIndexChanged;
20 connect(mCombo, idxChanged, this,
21 &FilterWidget::filterChanged);
22 //connect(mCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(filterChanged()));
23 connect(mEditLine, &QLineEdit::textChanged, this,
24 &FilterWidget::filterChanged);
25 }
26
27 void FilterWidget::headersChanged() {
28 QAbstractItemModel *newSource = mModel->sourceModel();
29 Q_ASSERT(newSource);
30 mCombo->clear();
31 for (int i=0; i < newSource->columnCount(); i++) {
32 mCombo->addItem(newSource->headerData(i, Qt::Horizontal).toString());
33 }
34 }
35
36 void FilterWidget::filterChanged() {
37 QString filterText = mEditLine->text();
38 if (filterText.size() < 1) {
39 return;
40 }
41 mModel->setFilterKeyColumn(mCombo->currentIndex());
42 mModel->setFilterWildcard(filterText);
43 }
44
45 void FilterWidget::setupGUI() {
46 QHBoxLayout *root = new QHBoxLayout;
47
48 QGroupBox *baseGroup = new QGroupBox(tr("Filter"));
49 root->addWidget(baseGroup);
50
51 QHBoxLayout *baseLayout = new QHBoxLayout;
52 baseGroup->setLayout(baseLayout);
53
54 mCombo = new QComboBox;
55 baseLayout->addWidget(mCombo);
56
57 mEditLine = new QLineEdit;
58 mEditLine->setPlaceholderText(tr("Filter expression"));
59 mEditLine->setClearButtonEnabled(true);
60 baseLayout->addWidget(mEditLine);
61
62 setLayout(root);
63 }
64
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)