2021年1月3日星期日

How QRegularExpression can be passed to Qt::MatchRegularExpression

I am trying this sample code that I found which is really really good. I am also trying to figure out the same thing to find an item and scroll to it, but this time I wanted to match the the string which has the EXACT WORD "cat".

Example matches:

  • cat

  • tom cat

  • dog and cat

  • super cat

I configured it & tried this code. I just did some minor changes like the QtCore.Qt.MatchRegExp into QtCore.Qt.MatchContains which scrolls me to the word that contains "cat".

from PyQt5 import QtCore,QtWidgets    app=QtWidgets.QApplication([])    def scroll():      #QtCore.QRegularExpression(r'\b'+'cat'+'\b')      item = listWidget.findItems('cat', QtCore.Qt.MatchContains)[0]      item.setSelected(True)    window = QtWidgets.QDialog()  window.setLayout(QtWidgets.QVBoxLayout())  listWidget = QtWidgets.QListWidget()  window.layout().addWidget(listWidget)      cats = ["thundercat","cat","tom cat","dogcat","dog and cat","super cat","lazycat"]    for i,cat in enumerate(cats):      QtWidgets.QListWidgetItem(f"{i}  {cat}", listWidget)    btn = QtWidgets.QPushButton('Scroll')  btn.clicked.connect(scroll)  window.layout().addWidget(btn)  window.show()  app.exec_()  

sample gui

Now I have read about this Qt::MatchRegularExpression & I was hoping to use this to achieve my goal which is scroll to the string with the EXACT WORD which contains "cat". Based on the documentation it says here.

Qt::MatchRegularExpression

Performs string-based matching using a regular expression as the search term. Uses QRegularExpression. When using this flag, a QRegularExpression object can be passed as parameter and will directly be used to perform the search. The case sensitivity flag will be ignored as the QRegularExpression object is expected to be fully configured. This enum value was added in Qt 5.15.

I can't seem to figure this out QRegularExpression object can be passed as parameter and will directly be used to perform the search I tried multiple solutions to what it meant by the object can be passed.

Things I Experimented

1.) I tried this, however it's giving me an IndexError: list index out of range error indicating that it has not found anything. I wonder why since the regex seems correct.

item = listWidget.findItems(r'\b'+'cat'+'\b',QtCore.Qt.MatchRegularExpression)[0]

2.) I tried this one still gives me this type of error.

File "finditems.py", line 7, in scroll      item = listWidget.findItems('cat',QtCore.Qt.MatchRegularExpression(QtCore.QRegularExpression(r'\b'+'cat'+'\b')))[0]  TypeError: 'MatchFlag' object is not callable  

3.) Again I tried this one but I think I got it wrong since the first parameter of the findItems function needs to be a str type.

File "finditems.py", line 7, in scroll      item = listWidget.findItems(QtCore.QRegularExpression(r'\b'+'cat'+'\b'),QtCore.Qt.MatchRegularExpression)[0]  TypeError: findItems(self, str, Union[Qt.MatchFlags, Qt.MatchFlag]): argument 1 has unexpected type 'QRegularExpression'  

How can I properly pass this QRegularExpression object as stated in the docs so that I can scroll to the string which has the EXACT WORD which is "cat"? Please help I am very much a beginner with PYQT5. Thank you! any help would be appreciated!

https://stackoverflow.com/questions/65557173/how-qregularexpression-can-be-passed-to-qtmatchregularexpression January 04, 2021 at 11:00AM

没有评论:

发表评论