Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QStringList index out of range
Forum Updated to NodeBB v4.3 + New Features

QStringList index out of range

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 487 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    JohnSRV
    wrote on last edited by
    #1

    Hey Everyone,

    I'm trying to extract some informations from a QStringList. I keep getting an error that the index is out of range.

    Here's how the List is constructed.

    QString selectedTarget = ui->TargetList->currentItem()->text();
    QStringList List = selectedTarget.split(QRegExp("\\s+"));
    

    The String selected Target contains the Hostname and the Ip Adress of a target device I'm trying to connect to. I Split this String in two separate String (Hostname and Address). I then try to store each of them in a different string but I get an error. So I tried to display the content and size of the List with the following code:

    qDebug() << List.count();
    	for (int f = 0;  f <= List.size() ; f++)
    	{
    		qDebug() << List.takeAt(f);
    	}
    

    List size is 2 which is right. But only the first Item of the list gets displayed. Within the second iteration i get the error that index out of range. which makes no sense the size of my list is 2 and i only iterated once.

    why is this happening?

    J.HilkJ 1 Reply Last reply
    0
    • B Offline
      B Offline
      Bonnie
      wrote on last edited by Bonnie
      #2

      takeAt removes the item of index f.
      If you just want to print it, use at instead.
      Do note that all the take* functions in Qt will remove something from the container / parent.

      1 Reply Last reply
      6
      • J JohnSRV

        Hey Everyone,

        I'm trying to extract some informations from a QStringList. I keep getting an error that the index is out of range.

        Here's how the List is constructed.

        QString selectedTarget = ui->TargetList->currentItem()->text();
        QStringList List = selectedTarget.split(QRegExp("\\s+"));
        

        The String selected Target contains the Hostname and the Ip Adress of a target device I'm trying to connect to. I Split this String in two separate String (Hostname and Address). I then try to store each of them in a different string but I get an error. So I tried to display the content and size of the List with the following code:

        qDebug() << List.count();
        	for (int f = 0;  f <= List.size() ; f++)
        	{
        		qDebug() << List.takeAt(f);
        	}
        

        List size is 2 which is right. But only the first Item of the list gets displayed. Within the second iteration i get the error that index out of range. which makes no sense the size of my list is 2 and i only iterated once.

        why is this happening?

        J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by
        #3

        @JohnSRV additionally to what @Bonnie said, your end condition is f <= List.size()that should be < otherwise it will make 3 try's to instead of the allowed 2


        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        1 Reply Last reply
        5

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved