Searching with QRefExp, Wildcards
Hello
i have a QTableWidget and a QlistWidget..
im implementing a search function like i Qt Designer.
When the user types something in the textbox items in QTableWidget and QlistWidget will be filtered..
how can i add wildcard searching functionality to my search function...
can someone post a code sample
thanks..
Re: Searching with QRefExp, Wildcards
maybe you could first post your code you are using. Using wildcards is just a parameter for your QRegExp.
Re: Searching with QRefExp, Wildcards
sorry for taking a long time to reply :)
I have something like this
.cpp
Code:
#include "mainwindow.h"
#include "ui_mainwindow.h"
{
ui->setupUi(this);
lst_ListItems.append("QtBlog");
lst_ListItems.append("QtCenter");
lst_ListItems.append("QtCreator");
lst_ListItems.append("QtDesigner");
lst_ListItems.append("Wildcard");
for(int iIndex = 0; iIndex < lst_ListItems.count(); ++iIndex)
{
pItem->setText(lst_ListItems.at(iIndex));
ui->lstNamesControl->addItem(pItem);// lstNamesControl is a QListWidget
}
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow
::on_txtSearch_textChanged(QString ) {
QString sSearch
= ui
->txtSearch
->text
();
// txtSearch is a QLineEdit ui->lstNamesControl->clear();
for(int iIndex = 0; iIndex < lst_ListItems.count(); ++iIndex)
{
pItem->setText(lst_ListItems.at(iIndex));
if(pItem->text().contains(sSearch, Qt::CaseInsensitive))
ui->lstNamesControl->addItem(pItem);
}
}
How can i add WildCards to something like this??
Re: Searching with QRefExp, Wildcards
Hi, a more sophisticated way to filter a list would be to use a QSortFilterProxyModel with QStringListModel and QListView. But with your code: First the slot receives ui->txtSearch->text() as a parameter, so there is no need to query it again. Inside the loop do something have a look at: QRegExp in general and specific QRegExp::exactMatch(), QRegExp::setPatternSyntax() with QRegExp::Wildcard.
Re: Searching with QRefExp, Wildcards
can you please post a example code...
i used it like this,but its not working
Code:
QString sSearch
= ui
->txtSearch
->text
();
ui->lstNamesControl->clear();
regExp.
setPatternSyntax(QRegExp::Wildcard);
for(int iIndex = 0; iIndex < lst_ListItems.count(); ++iIndex)
{
pItem->setText(lst_ListItems.at(iIndex));
if(regExp.exactMatch(pItem->text()))
ui->lstNamesControl->addItem(pItem);
Added after 8 minutes:
ohh. my mistake... got it :)
Thanks Lykurg