[Qt-interest] "%" characters in QStrings create havoc

KC Jones kc at asperasoft.com
Fri Feb 26 18:27:08 CET 2010


Thanks for the tip.  It helps, but not enough.  If anyone can dig up a doc
reference it might help me figure this out.  The HTML 4.6 docs for QString
do not appear to cover this nuance.  It does explain some of the internal
handling of parameter substitution operations which is good to know.  But
nothing about how to restrain it in this case.

Using "%%" does indeed appear to solve the parameter substitution problem.
In my test app the substitutions are predictable and correct.  But it leaves
me with double %% where I just want %.  I suppose there is no effective
difference in terms of SQL results between a (b LIKE '%%x%%') and (b LIKE
'%x%') operation, but I'm not entirely sure about that, and besides, it just
seems wrong to live with this mess.

It also does not cure my crash in my main application -- which does not
involve my logging classes as I previously claimed.  In my app, this line
causes a crash all by itself:

QString clause = QString(" WHERE (name LIKE '%1') OR (email LIKE '%2') ")
                    .arg(exp1).arg(exp2);

My stack (Mac) looks like:

0    QString::operator=        0    0x005136da
1    Cargo::Database::AddressCompletionModel::onFilterChanged
addresscompletionmodel.cpp    32    0x000d3eb6
2    Cargo::AddressListEditor::onTextChanged    addresslisteditor.cpp
121    0x00069475
3    Cargo::AddressListEditor::qt_metacall    moc_addresslisteditor.cpp
82    0x0006fa2f
4    QMetaObject::activate        0    0x005cd18c
5    QTextEdit::textChanged        0    0x00f5c682
6    QTextEdit::qt_metacall        0    0x00f60d7f
7    Cargo::AddressListEditor::qt_metacall    moc_addresslisteditor.cpp
75    0x0006f990
8    QMetaObject::activate        0    0x005cd18c
9    QTextControl::textChanged        0    0x00ce1042
10    QTextControl::qt_metacall        0    0x00ceaf3a
11    QMetaObject::activate        0    0x005cd18c
12    QTextDocument::contentsChanged        0    0x011350a2
13    QTextDocumentPrivate::endEditBlock        0    0x00d21f29
14    QTextCursor::insertText        0    0x00d4c63f
15    QTextCursor::insertText        0    0x00d4d0e4
16    QTextControlPrivate::keyPressEvent        0    0x00cec44a
17    QTextControl::processEvent        0    0x00ceec84
18    QTextControl::processEvent        0    0x00ce1967
19    QTextEdit::keyPressEvent        0    0x00f6252f
20    QWidget::event        0    0x00b5d63b
...    <More>


On Thu, Feb 25, 2010 at 6:01 PM, Scott Aron Bloom
<Scott.Bloom at sabgroup.com>wrote:

> I know its in the docs somewhere...
>
> But %% is what you are looking for.
> Scott
>
>
> -----Original Message-----
> From: qt-interest-bounces at trolltech.com
> [mailto:qt-interest-bounces at trolltech.com] On Behalf Of KC Jones
> Sent: Thursday, February 25, 2010 17:54
> To: Qt-interest
> Subject: [Qt-interest] "%" characters in QStrings create havoc
>
> What is the correct way to place percent characters in QStrings?
>
> I find that QStrings containing '%' characters act erratically and
> cause crashes.  I am able to reproduce some erratic behaviours, but so
> far I cannot isolate the crashes that I suffer in my application's
> logging classes.  What I think is the root of the problem comes from
> unwanted, unexpected parameter substitution.
>
> The following code is taken from a test app I put together.  I can
> share that as needed. I've fiddled around with escaping the percent
> characters, replacing "%" with "\%" and "\\\%" without much success.
>
> When the input string contains '1' or '2', the results in 'clause' are
> unpredictable and wrong.  But other characters in the 'str' are also
> problematic.
>
> In my application I am constructing SQL WHERE clauses that perform
> LIKE searches in which the '%' characters are essential.  The search
> can be based on arbitrary user input, so I need to find a bulletproof
> way to construct these SQL queries for all possible text input.
>
> void percent::showFilter(const QString &str)
> {
>    QString exp1 = "%";
>    for (int i = 0, im = str.size(); i < im; i++) {
>        exp1.append(str[i]).append("%");
>    }
>    QString exp2 = exp1 + "@%";
>    QString clause = QString(" WHERE (name LIKE '%1') OR (email LIKE
> '%2') ")
>                     .arg(exp1).arg(exp2);
>
>    ui->exp1->setText(exp1);
>    ui->exp2->setText(exp2);
>    ui->result->setText(clause);
> }
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20100226/e0329473/attachment.html 


More information about the Qt-interest-old mailing list