Crash - Testing Model/View Tutorial



  • Hello,

    I am very new to Qt.
    I wanted to create a simple Model/View functionality as in "http://qt-project.org/doc/qt-4.8/modelview.html":http://qt-project.org/doc/qt-4.8/modelview.html .
    However, when I want to add the QTimer as in chapter 2.3, it crashes.

    If I uncomment the QTimer and instead define an int x = 0 in the constructor of MyModel, it also crashes.
    That's weird.

    Any ideas what could be the problem?

    Thanks in advance!



  • like that it is difficult to said (for me). Could you paste your code (use gist github maybe for paste it) ? the .cpp file of the model and his .h file, and this one with the view.



  • @
    #ifndef GAMEMODEL_H
    #define GAMEMODEL_H

    #include <QAbstractTableModel>
    #include <QTimer>

    class GameModel : public QAbstractTableModel
    {
    Q_OBJECT
    int x;

    public:
    GameModel(QObject *parent);
    int rowCount(const QModelIndex &parent = QModelIndex()) const ;
    int columnCount(const QModelIndex &parent = QModelIndex()) const;
    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
    //QTimer *timer;

    private slots:
    //void timerHit();
    };

    #endif // GAMEMODEL_H
    @



  • why define "int x" here ?

    where is your class destructor ?

    do you run a QMake after put the Q_OBJECT macro ? (because if you want to use slots with Q_OBJECT, you also need to call a qmake after write the Q_OBJECT in your h file)

    could you show also the cpp file (you not need to write it here, because the place for write is stretch (except if it is really short code), you can use "gist github" for show the code and paste a link adress to this code.



  • Then the source:

    @
    GameModel::GameModel(QObject *parent)
    :QAbstractTableModel(parent)
    {
    x = 0;
    //selectedCell = 0;
    //timer = new QTimer(this);
    // timer->setInterval(1000);
    // connect(timer, SIGNAL(timeout()) , this, SLOT(timerHit()));
    // timer->start();
    }
    @

    It doesn't crash without the x = 0.



  • Hope you are allocating the QTimer object. Also try to re-arrange QTImer and Int vars in Private section separately. Clean, Run QMAKe and build it. It should work.



  • you absolutly need to:

    define your x var in private (or public if you want to access it directly from outside... but i think not)

    create a destructor for your class

    and do the Qmake for the compilation work with your Q_OBJECT to be able to use signal/slot by this way

    x=0 make your code crash because your define it out of the public or private part of your header file.



  • [quote author="Dheerendra" date="1420685575"]Hope you are allocating the QTimer object. Also try to re-arrange QTImer and Int vars in Private section separately. Clean, Run QMAKe and build it. It should work.[/quote]

    Yeah, I cleaned it and 're-qmade' it. This worked. Thanks!

    All the other stuff you guys adviced,... I dunno. int x is per se private since this is a class and not a struct, but it shouldn't alter the program's runtime behavior either way, etc...



  • for a struct (when you will need it), you have to define it first out of the class definition, and then define a link inside the class at public or private.

    like that: // this is a classeur.h file

    @
    struct classeur_infos {
    int id;
    int id_parent;
    QString name;
    QString comment;
    };

    class Classeur : public QObject
    {
    Q_OBJECT
    public:
    explicit Classeur(QObject *parent = 0, int id_parent = NULL);
    ~Classeur(); // this is your destructor part
    static QMap<int, int> Classeur_order; // this is a static QMap
    classeur_infos Classeur_Info; // this is the public struct
    .
    .
    .
    @

    hope this would help for clean code in C++



  • [quote author="jerome_isAviable?" date="1420685945"]you absolutly need to:

    define your x var in private (or public if you want to access it directly from outside... but i think not)

    create a destructor for your class

    and do the Qmake for the compilation work with your Q_OBJECT to be able to use signal/slot by this way

    x=0 make your code crash because your define it out of the public or private part of your header file.[/quote]

    Sorry, but no.
    The declaration of int x is fine where it is. The default for class is private, so anything above the public: line is private. I am used to the style to declare private variables as the very last section in the declaration under an explicit private: keyword, but the code as is is fine.

    Creating an explicit destructor also is not required. Your compiler will create one if needed. There are few cases where you need to make one explicitly, even if it is empty, but not here.

    Last: even that struct of your last post can be declared inside the scope of your class, if you so wish.



  • thanks Andre, i didn't know this. (you not have to be sorry also)
    i learned to do like that, but i'm also happy to learn more how this could be done to.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.