Trouble with QRegExp

  • I tested this pattern:
    QRegExp rx = QRegExp("^(?!ТЕСТ-М[^\S*\d*]).*$");
    With these 3 words:
    qDebug() << rx.indexIn("привет") << rx.indexIn("ТЕСТ-М60") << rx.indexIn("ТЕСТ-М");
    And all of them were matched, but, for example here:
    and at all other platforms pattern has another behavior, third word match to it. Why it happends? What i'm doing wrong ?

    1. you are not escaping the \. Either use double slash (\\) or use raw string literals
    2. All 3 of those strings match your pattern, if you tell us what you are trying to do maybe we can fix it. By guessing I'd say you need to replace the [^\S*\d*] with a negative look-ahead
    3. QRegExp is deprecated in Qt5, you should use QRegularExpression if you can

  • Hi. Thanks for your reply. I can't use QRegularExpression because i need to filter column with QSortFilterProxyModel and it doesn't support this class. Ok, maybe you can help me. For example, i have 4 words "TEST", "TEST-M60", "TEST МF", "TEST-M60/1000" and i need to write excluding pattern, which allows me, for example, hide rows with "TEST-M60" and see all others and so on.

  • subclass QSortFilterProxyModel like below

    class NegativeSortFilterProxyModel : public QSortFilterProxyModel{
        explicit NegativeSortFilterProxyModel(QObject* parent = Q_NULLPTR) : QSortFilterProxyModel(parent) {}
        bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const Q_DECL_OVERRIDE{
            return !QSortFilterProxyModel::filterAcceptsRow(source_row, source_parent);

    now use NegativeSortFilterProxyModel::setFilterRegExp("TEST-M60"); to hide all the TEST-M60

Log in to reply