Recently I've been working on a PyQt program. In the beginning I used python re module to process the regex, but the transformation between python string and QString makes me confused. So I tried to change QRegExp.
However, I want to use the IGNORECASE, MULTILINE, DOTALL of the python re. I've found the QRegExp.setCaseSensitivity() to replace the re.I, but I can't find the rest two features. Can anybody help?
Or tell me how to transform the QString to python string? Both the regex pattern and data are input by the user, so their types are QString.
Qt's QRegExp class is perfectly okay, but python's re module is much more flexible. And the same goes for string handling in general - if you need to do a lot of string processing in a PyQt program, it's often best to do it all in python if you can.
Converting a QString is quite easy. In python2, you just need to do:
And there's usually no need to convert it back: any Qt api that requires a QString will be automatically converted from a python string by PyQt.
A more general solution for QString conversion is to do it on a global basis using the sip module (which should already be available wherever PyQt is installed). This needs to be done at the beginning of your PyQt program, before any of the PyQt modules are imported, like this:
After doing this, any Qt API that normally returns a QString will return a python unicode object instead. For more in this feature, see Selecting Incompatible APIs in the PyQt docs.
Although it may initially require a little more work, taking this route is probably best in the long-run. For one thing, it will make porting to python3 easier if you should ever need to do so. For python3, the version 2 API is selected by default, so the above
setapi
step would not be needed.