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. [SOLVED] Can't create a QStandardItem of a C defined struct

[SOLVED] Can't create a QStandardItem of a C defined struct

Scheduled Pinned Locked Moved General and Desktop
qstandarditemstructqstandarditemmo
5 Posts 2 Posters 2.2k Views
  • 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.
  • A Offline
    A Offline
    andsun
    wrote on 4 May 2015, 08:22 last edited by andsun 5 Apr 2015, 12:04
    #1

    Hi all,
    I'm a newby to Qt. I'm trying to learn and use QStandardItem together with my old code.
    The old code is in C, hence contains structs instead of classes.
    I want to present the data from the struct in a QTableView using QStandardItemModel but when I loop through and try to create each item and add to the model the compiler says "no matching function for call to 'QStandardItem::QStandardItem(mystruct&)'

    So what am I missing to create an item?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mcosta
      wrote on 4 May 2015, 10:45 last edited by
      #2

      Hi,

      could you post the code where you get this error?
      The idea behind QStandardItemModel is to create a QStandardItem for each field of your struct and add a row to the model with QStandardItemModel::insertRow()

      For instance:

      mystruct data[ARRAY_SIZE];
      
      for (const mystruct& item: data) {
          QList<QStandardItem> row;
      
          row.append(QStandardItem(item.field1));
          ....
          row.append(QStandardItem(item.fieldn));
      
          model->addRow(row);
      }
      

      You have to manually convert your fields is Strings

      Once your problem is solved don't forget to:

      • Mark the thread as SOLVED using the Topic Tool menu
      • Vote up the answer(s) that helped you to solve the issue

      You can embed images using (http://imgur.com/) or (http://postimage.org/)

      A 1 Reply Last reply 4 May 2015, 11:29
      0
      • M mcosta
        4 May 2015, 10:45

        Hi,

        could you post the code where you get this error?
        The idea behind QStandardItemModel is to create a QStandardItem for each field of your struct and add a row to the model with QStandardItemModel::insertRow()

        For instance:

        mystruct data[ARRAY_SIZE];
        
        for (const mystruct& item: data) {
            QList<QStandardItem> row;
        
            row.append(QStandardItem(item.field1));
            ....
            row.append(QStandardItem(item.fieldn));
        
            model->addRow(row);
        }
        

        You have to manually convert your fields is Strings

        A Offline
        A Offline
        andsun
        wrote on 4 May 2015, 11:29 last edited by
        #3

        @mcosta Hi
        OK, so there is no way to create an item from a struct, without separating the struct into field data per item!? Like in your code sample

        Below is the struct, slightly modified an reduced in size. I want to use a combobox to select index and present elements in the tableview to be edited.
        The elementnames are specified, like 50 of them in a separate ini file, so they arre filled into a delegate combobox as in QSpinBox example to select from into the cell
        The limits are equally presented and limited to input type and size

        struct data {
        int index; /* index to display /
        int group; /
        group index 1=> 3 with 4 dataelements in each group ... /
        struct elements {
        int elemno; /
        elementnumber, 1- 5 /
        char elemname[8]; /
        its name /
        float limit 1; /
        low limit /
        float limit 2; /
        upper limit */
        } elem [5];
        };

        M 1 Reply Last reply 4 May 2015, 11:36
        0
        • A andsun
          4 May 2015, 11:29

          @mcosta Hi
          OK, so there is no way to create an item from a struct, without separating the struct into field data per item!? Like in your code sample

          Below is the struct, slightly modified an reduced in size. I want to use a combobox to select index and present elements in the tableview to be edited.
          The elementnames are specified, like 50 of them in a separate ini file, so they arre filled into a delegate combobox as in QSpinBox example to select from into the cell
          The limits are equally presented and limited to input type and size

          struct data {
          int index; /* index to display /
          int group; /
          group index 1=> 3 with 4 dataelements in each group ... /
          struct elements {
          int elemno; /
          elementnumber, 1- 5 /
          char elemname[8]; /
          its name /
          float limit 1; /
          low limit /
          float limit 2; /
          upper limit */
          } elem [5];
          };

          M Offline
          M Offline
          mcosta
          wrote on 4 May 2015, 11:36 last edited by
          #4

          @andsun said:

          OK, so there is no way to create an item from a struct, without separating the struct into field data per item!?

          QStandardItemModel is for simple models; if you want to use complex models you have to subclass QAbstractItemModel

          Once your problem is solved don't forget to:

          • Mark the thread as SOLVED using the Topic Tool menu
          • Vote up the answer(s) that helped you to solve the issue

          You can embed images using (http://imgur.com/) or (http://postimage.org/)

          1 Reply Last reply
          1
          • A Offline
            A Offline
            andsun
            wrote on 4 May 2015, 12:04 last edited by
            #5

            Hmm OK,

            Think I get it.
            What you say is I have to create my own class inherrited from QAbstractItemModel
            And in the class implementation modify to my usage

            OK, I'll give it a try and see where it leads me.

            Thanks for the reply, hope its the answer to my problem.
            Close the thread for now... somehow

            1 Reply Last reply
            0

            5/5

            4 May 2015, 12:04

            • Login

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