Reply
Thread Tools
Posts: 52 | Thanked: 8 times | Joined on Apr 2010
#1
I am trying to see if a certain string starts with a string that is entered into a text box:

Code:
QStringList temp = allServices;
    QString current;

    for(int i = 0; i < temp.size(); ++i)
    {
        current = temp.at(i);

        if(!current.startsWith(text, Qt::CaseInsensitive))
        {
            QMessageBox msg;
            msg.setText("Removing " + temp.at(i));
            msg.exec();

            temp.removeAt(i);
        }
    }
'text' is the input string, which comes directly from the 'text_changed' slot for the input box and the for loop iterates through the QStringList and checks for matches (technically NON matches). However, a few examples of matches and non matches when the letter 'C' is compared with each item are as follows:

MATCH: ('org.freedesktop.Notifications' is compared with 'C')
NON-MATCH: ('org.freedesktop.DBus' is compared with 'C')

I cannot see how it thinks the first one starts with a 'C' and the second one doesn't.

I have retrieved a list of all of the DBUS services in string format and I am filtering those strings through a text box by a user-entered string. Is there some sort of format problem here?

(PS: I put the message box in to see which ones it was actually removing from the list)
 
Posts: 3,617 | Thanked: 2,412 times | Joined on Nov 2009 @ Cambridge, UK
#2
Removing elements from the array you're iterating through is never going to work well. I'm not sure that it's actually matching what you think it is.
 
Posts: 52 | Thanked: 8 times | Joined on Apr 2010
#3
i just tried it with a separate array to remove the non matches and removed them in another for loop, but its still giving me the same outcome, incorrect comparisons.
 
Posts: 52 | Thanked: 8 times | Joined on Apr 2010
#4
in fact, no matter what letter/word i enter, i get the same results every time...
 
Posts: 3,428 | Thanked: 2,856 times | Joined on Jul 2008
#5
Not sure if you tried making the messagebox say something along the lines of:

msg.setText(text + " == " + current);
msg.exec();

Just to see both text, and the string it's comparing side by side.
__________________
If I've helped you or you use any of my packages feel free to help me out.
-----------------------------------------------------------------------------------
Maintaining:
pyRadio - Pandora Radio on your N900, N810 or N800!
 
Posts: 52 | Thanked: 8 times | Joined on Apr 2010
#6
like i said, the messagebox is insignificant, the whole comparison is going wrong.
 
Posts: 3,617 | Thanked: 2,412 times | Joined on Nov 2009 @ Cambridge, UK
#7
Originally Posted by Jaso333 View Post
like i said, the messagebox is insignificant, the whole comparison is going wrong.
Yes, but fatalsaint's point is that you don't know the comparison is going wrong unless you're certain of what's actually being compared. Without outputting the two strings, you can't be 100% certain that it's comparing the strings you think it is.
 

The Following 2 Users Say Thank You to Rob1n For This Useful Post:
Posts: 3,428 | Thanked: 2,856 times | Joined on Jul 2008
#8
Originally Posted by Jaso333 View Post
like i said, the messagebox is insignificant, the whole comparison is going wrong.
right, I was offering a troubleshooting technique to actually see what it's comparing. Maybe "text" isn't what you think it is, etc.

We are missing code in your excerpt. My thoughts are the variables are not the right types, or are simply returning something you're not expecting.

I don't have access to coding QT in C right at the moment; so I did this in python:

Code:
text = QString("C")
temp = QStringList( [ "org.freedesktop.DBus", "org.freedesktop.Notifications", "cat" ] )
current = QString("")

for s in temp:
    current = QString(s)
    if not current.startsWith(text, Qt.CaseInsensitive):
         print "no match"
     else:
         print "match"
Code:
~/test $python temp.py
no match
no match
match
Works fine. So either Python fixed the problem, or there's something in your code we're missing - but that's about as direct a port from C to python of the code you have provided I can get.
__________________
If I've helped you or you use any of my packages feel free to help me out.
-----------------------------------------------------------------------------------
Maintaining:
pyRadio - Pandora Radio on your N900, N810 or N800!
 
Posts: 3,319 | Thanked: 5,610 times | Joined on Aug 2008 @ Finland
#9
Another suggestion is to avoid the classic C counter construct and at() or []. Use

foreach (QString s, temp) {
...
}

Faster, less error prone, easier to parallelize later on.
__________________
Blogging about mobile linux - The Penguin Moves!
Maintainer of PyQt (see introduction and docs), AppWatch, QuickBrownFox, etc
 

The Following User Says Thank You to attila77 For This Useful Post:
Posts: 52 | Thanked: 8 times | Joined on Apr 2010
#10
ah, i didnt realize you could use 'for each' in C++. i thought it was only a c# thing
 
Reply

Thread Tools

 
Forum Jump


All times are GMT. The time now is 02:34.