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. problem creating QAbstractItemModel
Qt 6.11 is out! See what's new in the release blog

problem creating QAbstractItemModel

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 4 Posters 1.3k Views 2 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.
  • mzimmersM Offline
    mzimmersM Offline
    mzimmers
    wrote on last edited by
    #1

    Hi all -

    This is only my 2nd time working in the model/view world. I was trying to use the code from my 1st time as a, well, you know...model.

    I want to populate a model with data I receive from a TCP message. I have a class containing the model:

    model_ap_scan.h

    class ModelApScan : public QObject
    {
        Q_OBJECT
    private:
        QAbstractItemModel *m_model;    // the main model
    public:
        explicit ModelApScan(QObject *parent = Q_NULLPTR);
        ~ModelApScan();
    public slots:
        void update(Message *msg, vector<string> &keys);
    

    model_ap_scan.cpp:

    ModelApScan::ModelApScan(QObject *parent) : QObject(parent), m_model(new QStandardItemModel(this))
    {
        m_model->insertColumns(0, AP_TABLE_NBR_COLUMNS);
        m_model->setHeaderData(AP_TABLE_COLUMN_MACADDR, Qt::Horizontal, tr("MAC Address"), Qt::DisplayRole);
        m_model->setHeaderData(AP_TABLE_COLUMN_SSID, Qt::Horizontal, tr("SSID"), Qt::DisplayRole);
        m_model->setHeaderData(AP_TABLE_COLUMN_RSSI, Qt::Horizontal, tr("RSSI"), Qt::DisplayRole);
    }
    
    void ModelApScan::update(Message *msg, vector<string> &keys)
    {
        int row;
        int rowCnt = m_model->rowCount();
    ...
    

    This is, as far as I can see, verbatim from my 1st model. But, I get a segfault on the call to rowCount() in update(). Can anyone see what I'm doing wrong?

    Thanks...

    KillerSmathK jsulmJ 2 Replies Last reply
    0
    • mzimmersM mzimmers

      Hi all -

      This is only my 2nd time working in the model/view world. I was trying to use the code from my 1st time as a, well, you know...model.

      I want to populate a model with data I receive from a TCP message. I have a class containing the model:

      model_ap_scan.h

      class ModelApScan : public QObject
      {
          Q_OBJECT
      private:
          QAbstractItemModel *m_model;    // the main model
      public:
          explicit ModelApScan(QObject *parent = Q_NULLPTR);
          ~ModelApScan();
      public slots:
          void update(Message *msg, vector<string> &keys);
      

      model_ap_scan.cpp:

      ModelApScan::ModelApScan(QObject *parent) : QObject(parent), m_model(new QStandardItemModel(this))
      {
          m_model->insertColumns(0, AP_TABLE_NBR_COLUMNS);
          m_model->setHeaderData(AP_TABLE_COLUMN_MACADDR, Qt::Horizontal, tr("MAC Address"), Qt::DisplayRole);
          m_model->setHeaderData(AP_TABLE_COLUMN_SSID, Qt::Horizontal, tr("SSID"), Qt::DisplayRole);
          m_model->setHeaderData(AP_TABLE_COLUMN_RSSI, Qt::Horizontal, tr("RSSI"), Qt::DisplayRole);
      }
      
      void ModelApScan::update(Message *msg, vector<string> &keys)
      {
          int row;
          int rowCnt = m_model->rowCount();
      ...
      

      This is, as far as I can see, verbatim from my 1st model. But, I get a segfault on the call to rowCount() in update(). Can anyone see what I'm doing wrong?

      Thanks...

      KillerSmathK Offline
      KillerSmathK Offline
      KillerSmath
      wrote on last edited by
      #2

      @mzimmers
      I've implemented your snippet and everything is working fine.

      Can you share with us an extended snippet ? Maybe, the problem is in way that you insert the Items in the Model.

      @Computer Science Student - Brazil
      Web Developer and Researcher
      “Sometimes it’s the people no one imagines anything of who do the things that no one can imagine.” - Alan Turing

      mzimmersM 1 Reply Last reply
      5
      • mzimmersM mzimmers

        Hi all -

        This is only my 2nd time working in the model/view world. I was trying to use the code from my 1st time as a, well, you know...model.

        I want to populate a model with data I receive from a TCP message. I have a class containing the model:

        model_ap_scan.h

        class ModelApScan : public QObject
        {
            Q_OBJECT
        private:
            QAbstractItemModel *m_model;    // the main model
        public:
            explicit ModelApScan(QObject *parent = Q_NULLPTR);
            ~ModelApScan();
        public slots:
            void update(Message *msg, vector<string> &keys);
        

        model_ap_scan.cpp:

        ModelApScan::ModelApScan(QObject *parent) : QObject(parent), m_model(new QStandardItemModel(this))
        {
            m_model->insertColumns(0, AP_TABLE_NBR_COLUMNS);
            m_model->setHeaderData(AP_TABLE_COLUMN_MACADDR, Qt::Horizontal, tr("MAC Address"), Qt::DisplayRole);
            m_model->setHeaderData(AP_TABLE_COLUMN_SSID, Qt::Horizontal, tr("SSID"), Qt::DisplayRole);
            m_model->setHeaderData(AP_TABLE_COLUMN_RSSI, Qt::Horizontal, tr("RSSI"), Qt::DisplayRole);
        }
        
        void ModelApScan::update(Message *msg, vector<string> &keys)
        {
            int row;
            int rowCnt = m_model->rowCount();
        ...
        

        This is, as far as I can see, verbatim from my 1st model. But, I get a segfault on the call to rowCount() in update(). Can anyone see what I'm doing wrong?

        Thanks...

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @mzimmers said in problem creating QAbstractItemModel:

        I get a segfault on the call to rowCount()

        If you get a segfault there then it means m_model is invalid pointer.
        Do you delete m_model somewhere?

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        5
        • KillerSmathK KillerSmath

          @mzimmers
          I've implemented your snippet and everything is working fine.

          Can you share with us an extended snippet ? Maybe, the problem is in way that you insert the Items in the Model.

          mzimmersM Offline
          mzimmersM Offline
          mzimmers
          wrote on last edited by
          #4

          @KillerSmath hi - I'm not inserting any items yet. My model is empty when I make that call.

          @jsulm no, I'm not deleting it, but...in stepping through the debugger, I just realized that my c'tor isn't being called. That's really odd...don't know what to make of that, though it does explain why the call to rowCount() blows up.

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Hi,

            Where are you creating the instance of ModelApScan ?
            Are you sure it's still valid when calling update ?

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            1
            • mzimmersM Offline
              mzimmersM Offline
              mzimmers
              wrote on last edited by mzimmers
              #6

              Bingo...I wasn't creating it. I created the pointer but never invoked the c'tor from the parent. Now corrected:

              EditDialog::EditDialog(DeviceModel *d, QModelIndex *qmi, Worker *pWorker, QWidget *parent) :
                  QDialog(parent),
                  ui(new Ui::EditDialog),
                  m_d(d),
                  m_qmi(qmi),
                  m_worker(pWorker)
              {   
                  ui->setupUi(this);
              
                     // create the model that holds the results of the AP scan.
                  m_modelApScan = new ModelApScan();
              

              Amazing the brain-dead mistakes one can make when distracted...thanks for the help.

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Side note (partly aesthetics), you can keep m_modelApScan in the initialiser list of your EditDialog class constructor.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                1

                • Login

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