Qt World Summit: Register Today!

[SOLVED]Qvector Problem instansiate Thanks ALL

  • I got a little confusing problem with one of my Vectors.

    In my headerfile I set it up like this.
    @QVector <ClPresetLoadsTestValues> presetVector;

    later when I want to use it i try to add some Object with append like this
    @if (presetVector.isEmpty())
    qDebug() << "Empty";
    qDebug() << "SIZE " << presetVector.size() ;



    but my program crashes

    I have tried to do like this also
    @QVector <ClPresetLoadsTestValues>* presetVector;

    and then i my constructor using new but with the same result;

  • At what point your program actually crashes? When you create the QVector or whent you try to append to it?

  • on isEmpty()

  • That needs more code to evaluate. From what I see, it is correct. In which function do you call isEmpty? Where do you create it? where is the constructor, ...?

  • Do other containers or QVector with other type cause crash?

  • My h.file
    @#ifndef GLOBALPARAMS_H
    #define GLOBALPARAMS_H
    #include <QVector>
    #include <QList>
    #include <QTime>
    #include <clpresetloadstestvalues.h>

    class GlobalParams
    enum Tester {Starttest, Loadtest, Batterytest, ParallellToMains, MainsFailureTEst, PresetLoadsTest , Notest};

    void setTest(int);
    int getTest();
    void testRunning(bool );
    bool getTestRunning();
    void setNBState(int s);
    void setGBState(int s);
    void setRunningState(int s);
    void setRunningMode(int s);
    int getNBState();
    int getGBState();
    int getRunningState();
    int getRunningMode();
    void addValuesToLoadsTestTemp(int,int,int);
     double getRandomNumbers(float max, float min);
     QString roundnumbers(double dNumber);

    int testTyp;

    int NBState;
    int GBState;
    int RunningState;
    int RunningMode;
    bool TestRunning;
    QVector <ClPresetLoadsTestValues> presetVector;


    in my cpp file
    @void GlobalParams::addValuesToLoadsTestTemp(int kw, int min, int sec)
    ClPresetLoadsTestValues p;

    if (presetVector.isEmpty())
        qDebug() << "Empty";
        qDebug() << "SIZE " << presetVector.size() ;
     qDebug() << "appendad";



    My h.file to the object I want to save

    @class ClPresetLoadsTestValues
    ClPresetLoadsTestValues(int k, int m, int s);

    void setKW(int k);
    void setMin(int k);
    void setSec(int k);

    int getKw();
    int getMin();
    int getSec();

    int kw;
    int min;
    int sec;

    I tried with a Struct also but same crash

  • I'm using a vector in a different place in my application and there is no problem to use.

  • Check for that: you declare your presetVector like a value, so that constructor of it is called before the constructor of GlobalParams. So may be the object of GlobalParams haven't been instantiated yet?

  • I have tried to do like this also.

    @QVector <.theClass> *myVector;
    and in the constructor of globalparams

    @myVector = new QVector<theClass>();
    but same crash ;

  • Ok, once again, check if constructor of GlobalParams is called, add there qDebug()<<"anything";

  • It's the first thing I do in my program.

    I got a lot of parameters in that object that I use in other places so thats no problem.

  • Please provide a small, complete, compilable and runnable sample program. We can have a look if and where it crashes ourselves then.

  • Now I'm back to my Work and will send a small application with this Vector problem.
    I have put some QDebug lines in the code to:

    But where could I upload the program

  • I think that any file sharing hosting will be suitable. For example try http://rghost.net/

  • look for a file hoster (e.g. gitorious.org, but you need a logon) or any other

  • I have discovered a problem know that is a little confusing.

    When I try to append a Object or a struct thats not in the class where the vector is the program crashes but when I put a struct inside the class and append this to the vector it works fine

  • Here is not the TAR file to test

  • Your code works without any crashes. I didn't do any changes though.

  • Try again with this code
    I had comment the problem spots in the last code.
    The new one is here


  • toho71 either you have to read more about C++ or you did horrible typo. Here it is:
    delete this;

  • copy and paste is the to magic words
    Ok now it works again.

    I have to look in my header prj to and see if this is the magic case.

  • in my testcase i do like this in my constructor


    qDebug() << presetVector.isEmpty();
    qDebug() << presetVector.size() << " vectorsize  and kapacitet =" << presetVector.capacity();
    //Create a tempobject for test
    ClPresetLoadsTestValues p(10,10,10);
    qDebug() << "create  object ";
    qDebug() << "appendat obj";
    qDebug() << presetVector.size() << " vectorsize  and kapacitet =" << presetVector.capacity();


    and I got this text in my consol

    0 vectorsize and kapacitet = 0
    create ClPresetLoadsTestValues
    create object
    delete ClPresetLoadsTestValues
    appendat obj
    1 vectorsize and kapacitet = 1
    delete ClPresetLoadsTestValues

    And later when I try to add a ClPresetLoadsTestValues my program crashes and NO i don't delete the object.

    here is my object

    qDebug() << "create ClPresetLoadsTestValues";

    ClPresetLoadsTestValues::ClPresetLoadsTestValues(int k, int m, int s)
    this->kw = k;
    this->min = m;
    this->sec = s;
    qDebug() << "create ClPresetLoadsTestValues";

    qDebug() << "delete ClPresetLoadsTestValues";

    //set and getters

    and my func to add the obj

    @void GlobalParams::addValuesToLoadsTestTemp(int kw, int min, int sec)
    ClPresetLoadsTestValues p(kw,min,sec);

    qDebug() << "try to append";
     qDebug() << this->presetVector.size() << " vektorstorlek"; //IT crashes here
    if (this->presetVector.isEmpty())
        qDebug() << "Empty";
        qDebug() << "SIZE " << presetVector.size() ;
     qDebug() << "appendad";



  • Can you make another tarball?

  • it gonna be big with many files but maybe only the files in the spec region and it will not compile.
    I'll try

  • You should provide only source files. Do not include object files (and autogenerated moc_, ui_, qrc_*) in the tarball.

  • Here it is

    the two files with this problem

  • Please provide a

    • complete
    • compilable
    • minimal

    test case.

    leave out

    • intermediate object file (*.o)
    • autogenerated stuff from moc and uic (moc_.cpp, ui_.h)
    • Qt Creator user data (*.pro.user)
    • the final executable

    You're more likely to get an answer if the fellows trying to help here in the forums do not need to guess how to set up a correct test case and a project file.

  • Here is a full tested version with compiling.

    I uploaded a small ex earlier

  • it works very well to append objects when I instantiates the object globalParam but later when I try to append some new objects later in the program it seems like the vector isn't there.

  • Unfortunately I can spend some time on it right now, but the archive is no longer available. Do you mind uploading it again, please?

  • Volker
    I got it solved and it had a Nullpointer in another place so this worked fine but some dependent functions made some errors to this.

Log in to reply