Results 1 to 6 of 6

Thread: Creating variable name in Qt by a for loop and finding numbers in a QString object

  1. #1
    Join Date
    Nov 2015
    Posts
    128
    Thanks
    70
    Qt products
    Qt5
    Platforms
    Windows

    Default Creating variable name in Qt by a for loop and finding numbers in a QString object

    Hi
    I have some questions about QString.

    1- I want to create a variable name (QString type) in a for loop. how can i do this. like this:
    Qt Code:
    1. QString name[15];
    2. for(int i=0; i<10; ++i)
    3. name(i) = "name i" //I need "i" in the "name i" be effected
    To copy to clipboard, switch view to plain text mode 

    2- In a QString like str (str is a type of QString), I want to find numbers. is there any function that search number in a QString? for example:

    Qt Code:
    1. QString str;
    2. str="Hi4Hello78good5"
    3. str.isThereSearchNumberFunction(); // I need this returns 4,7,8,5
    To copy to clipboard, switch view to plain text mode 

    Thanks for any help.
    Last edited by Alex22; 4th December 2015 at 20:41.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Creating variable name in Qt by a for loop

    Qt Code:
    1. QString name[15];
    2. for ( int i = 0; i < 10; ++i)
    3. name[i] = QString("name %1").arg(i);
    To copy to clipboard, switch view to plain text mode 
    You should consider a QStringList instead of a C array.

    QString::contains() with QRegExp()

  3. The following user says thank you to ChrisW67 for this useful post:

    Alex22 (4th December 2015)

  4. #3
    Join Date
    Nov 2015
    Posts
    128
    Thanks
    70
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Creating variable name in Qt by a for loop

    Quote Originally Posted by ChrisW67 View Post
    Qt Code:
    1. QString name[15];
    2. for ( int i = 0; i < 10; ++i)
    3. name[i] = QString("name %1").arg(i);
    To copy to clipboard, switch view to plain text mode 
    You should consider a QStringList instead of a C array.

    QString::contains() with QRegExp()
    Realy thanks, ChrisW67
    For what must I consider QStringList? QString is not good class for this target?
    I need if str has any number, return that numbers, not true or false. QString::contains() with QRegExp() returns true or false. Is there any function for QString or must write it myself?

  5. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Creating variable name in Qt by a for loop

    A QStringList might be a good alternative to a fixed length array of strings.

    If you want the captured string(s) then just use QRegExp to capture the matches. There is a useful example in the docs.

  6. The following user says thank you to ChrisW67 for this useful post:

    Alex22 (4th December 2015)

  7. #5
    Join Date
    Nov 2015
    Posts
    128
    Thanks
    70
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Creating variable name in Qt by a for loop

    Quote Originally Posted by ChrisW67 View Post
    A QStringList might be a good alternative to a fixed length array of strings.

    If you want the captured string(s) then just use QRegExp to capture the matches. There is a useful example in the docs.
    Realy nice!!! this example is exactly that i want!
    Qt Code:
    1. QRegExp rx("(\\d+)");
    2. QString str = "Offsets: 12 14 99 231 7";
    3. int pos = 0;
    4.  
    5. while ((pos = rx.indexIn(str, pos)) != -1) {
    6. list << rx.cap(1);
    7. pos += rx.matchedLength();
    8. }
    9. // list: ["12", "14", "99", "231", "7"]
    To copy to clipboard, switch view to plain text mode 

    so many thanks ChrisW67!!!

  8. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Creating variable name in Qt by a for loop

    If you are on Qt5 you might want to use the new QRegularExpression class instead of QRegExp, to make your code more future proof

    Cheers,
    _

  9. The following user says thank you to anda_skoa for this useful post:

    Alex22 (5th December 2015)

Similar Threads

  1. Variable variable names
    By KeineAhnung in forum Newbie
    Replies: 2
    Last Post: 22nd June 2014, 19:00
  2. For loop and While loop handling.
    By kiboi in forum Newbie
    Replies: 7
    Last Post: 2nd January 2013, 13:43
  3. Replies: 4
    Last Post: 6th August 2011, 01:40
  4. Main loop thread loop communication
    By mcsahin in forum Qt Programming
    Replies: 7
    Last Post: 25th January 2011, 16:31
  5. Creating QFile Objects in a loop
    By sky in forum Newbie
    Replies: 1
    Last Post: 3rd December 2010, 08:27

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.