Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QList Help



  • Hi everyone,
    here's my issue, i'm trying to get QList data from a class to my main.
    My code is suppose to get a joystick event then i put it in a QList which i then return,see the code below.
    here is my code :

    
    QList <int> Rjoystick::datAxis(int i)
    {
        QList <int>axis;
    
        m_joystick = SDL_JoystickOpen(i);
        SDL_JoystickEventState(SDL_ENABLE);
        SDL_Event event;
    
        SDL_PollEvent(&event);
    
        switch (event.type) {
        case SDL_JOYAXISMOTION:
        axis.insert(event.jaxis.axis,event.jaxis.value);
          break;
    
        default:
            break;
        }
    
        SDL_JoystickClose(m_joystick);
        return axis;
    }
    

    and here's the code fto get some data from the QList on the main.

    QList <int> g;
    
       g = r.datAxis(0);
       qDebug() <<g.at(0);
    

    i'm getting this error : "ASSERT failure in QList<T>::at: "index out of range"

    thamk you foryour help.


  • Lifetime Qt Champion

    Hi,

    There's no garanti that datAxis returns a non empty list. You should check that before using that function return value.



  • So i modify the code and it's seems that you are right, i think my problem maybe coming from the function "insert". At this point i don't know how will i get the Joystick data into my QList, if you got any advice, i will take it.


  • Lifetime Qt Champion

    @RCtaf
    I was wondering something else
    what is
    event.jaxis.axis,event.jaxis.value
    ?
    What values do they have?
    As if event.jaxis.axis is not zero, you ask it to insert event.jaxis.value at some
    index which list might not have (yet) -> giving "index out of range"
    You could do
    axis.append(event.jaxis.value); to simply store the values but
    you might need event.jaxis.axis for something also ?



  • Well, event.jaxis.axis hold a integer it's the axis being moved, can be 0 or 4 depends of what axis of the joystick is being moved.
    And event.jaxis.value hold the value of the which the joystick is moved can be anything between roughly -32000 to 32000.


  • Lifetime Qt Champion

    @RCtaf
    Hi
    In that case, you could do
    QList<int> axis{0,0,0,0,0};
    then
    axis.insert(event.jaxis.axis,event.jaxis.value);
    will work as expected.
    (as list has 5 ints)
    I assume then event.jaxis.axis can have value 0-4 ?
    When you read it back you can check which index has non zero to see
    which axes was changed.



  • Did that, And it is working just perfectly.

    Thank you for help.


Log in to reply