Results 1 to 5 of 5

Thread: Searching with QRefExp, Wildcards

  1. #1
    Join Date
    Jan 2011
    Location
    Sri Lanaka
    Posts
    64
    Thanks
    39
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Post 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..

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Searching with QRefExp, Wildcards

    maybe you could first post your code you are using. Using wildcards is just a parameter for your QRegExp.

  3. The following user says thank you to Lykurg for this useful post:

    deepal_de (7th June 2011)

  4. #3
    Join Date
    Jan 2011
    Location
    Sri Lanaka
    Posts
    64
    Thanks
    39
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Question Re: Searching with QRefExp, Wildcards

    sorry for taking a long time to reply

    I have something like this

    .cpp
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. QStringList lst_ListItems;
    5. MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
    6. {
    7. ui->setupUi(this);
    8.  
    9. lst_ListItems.append("QtBlog");
    10. lst_ListItems.append("QtCenter");
    11. lst_ListItems.append("QtCreator");
    12. lst_ListItems.append("QtDesigner");
    13. lst_ListItems.append("Wildcard");
    14.  
    15. for(int iIndex = 0; iIndex < lst_ListItems.count(); ++iIndex)
    16. {
    17. pItem->setText(lst_ListItems.at(iIndex));
    18. ui->lstNamesControl->addItem(pItem);// lstNamesControl is a QListWidget
    19. }
    20. }
    21.  
    22. MainWindow::~MainWindow()
    23. {
    24. delete ui;
    25. }
    26.  
    27.  
    28. void MainWindow::on_txtSearch_textChanged(QString )
    29. {
    30. QString sSearch = ui->txtSearch->text(); // txtSearch is a QLineEdit
    31. ui->lstNamesControl->clear();
    32. for(int iIndex = 0; iIndex < lst_ListItems.count(); ++iIndex)
    33. {
    34. pItem->setText(lst_ListItems.at(iIndex));
    35. if(pItem->text().contains(sSearch, Qt::CaseInsensitive))
    36. ui->lstNamesControl->addItem(pItem);
    37. }
    38. }
    To copy to clipboard, switch view to plain text mode 

    How can i add WildCards to something like this??

  5. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default 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.

  6. The following user says thank you to Lykurg for this useful post:

    deepal_de (7th June 2011)

  7. #5
    Join Date
    Jan 2011
    Location
    Sri Lanaka
    Posts
    64
    Thanks
    39
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: Searching with QRefExp, Wildcards

    can you please post a example code...

    i used it like this,but its not working
    Qt Code:
    1. QString sSearch = ui->txtSearch->text();
    2. ui->lstNamesControl->clear();
    3.  
    4. QRegExp regExp;
    5. regExp.setPatternSyntax(QRegExp::Wildcard);
    6. for(int iIndex = 0; iIndex < lst_ListItems.count(); ++iIndex)
    7. {
    8. pItem->setText(lst_ListItems.at(iIndex));
    9. if(regExp.exactMatch(pItem->text()))
    10. ui->lstNamesControl->addItem(pItem);
    To copy to clipboard, switch view to plain text mode 


    Added after 8 minutes:


    ohh. my mistake... got it
    Thanks Lykurg
    Qt Code:
    1. QRegExp regExp(sSearch );
    To copy to clipboard, switch view to plain text mode 
    Last edited by deepal_de; 7th June 2011 at 10:14.

Similar Threads

  1. Searching a QListWidget
    By bl1nk in forum Newbie
    Replies: 11
    Last Post: 9th August 2010, 21:54
  2. QAbstractListModel searching.
    By ComaWhite in forum Qt Programming
    Replies: 1
    Last Post: 15th June 2009, 18:41
  3. Searching in a list.
    By kaushal_gaurav in forum Qt Programming
    Replies: 4
    Last Post: 15th October 2008, 08:00
  4. searching in a TableWidget
    By peace_comp in forum Qt Programming
    Replies: 1
    Last Post: 17th May 2008, 16:17
  5. Searching a QTable
    By nategoofs in forum Qt Programming
    Replies: 1
    Last Post: 8th August 2007, 22:15

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.