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. Crash - Testing Model/View Tutorial
Qt 6.11 is out! See what's new in the release blog

Crash - Testing Model/View Tutorial

Scheduled Pinned Locked Moved General and Desktop
11 Posts 4 Posters 4.0k Views 1 Watching
  • 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    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!

    1 Reply Last reply
    0
    • jerome_isAviableJ Offline
      jerome_isAviableJ Offline
      jerome_isAviable
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        @
        #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
        @

        1 Reply Last reply
        0
        • jerome_isAviableJ Offline
          jerome_isAviableJ Offline
          jerome_isAviable
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • ? Offline
            ? Offline
            A Former User
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • dheerendraD Offline
              dheerendraD Offline
              dheerendra
              Moderators Qt Champions 2024 Qt Champions 2022 Qt Champions 2017
              wrote on last edited by
              #6

              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.

              Dheerendra
              @Community Service
              Certified Qt Specialist
              https://www.pthinks.com

              1 Reply Last reply
              1
              • jerome_isAviableJ Offline
                jerome_isAviableJ Offline
                jerome_isAviable
                wrote on last edited by
                #7

                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.

                1 Reply Last reply
                0
                • ? Offline
                  ? Offline
                  A Former User
                  wrote on last edited by
                  #8

                  [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...

                  1 Reply Last reply
                  0
                  • jerome_isAviableJ Offline
                    jerome_isAviableJ Offline
                    jerome_isAviable
                    wrote on last edited by
                    #9

                    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++

                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      andre
                      wrote on last edited by
                      #10

                      [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.

                      1 Reply Last reply
                      0
                      • jerome_isAviableJ Offline
                        jerome_isAviableJ Offline
                        jerome_isAviable
                        wrote on last edited by
                        #11

                        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.

                        1 Reply Last reply
                        0

                        • Login

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