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. Cannot use QStringList defined in header file
Forum Updated to NodeBB v4.3 + New Features

Cannot use QStringList defined in header file

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 360 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.
  • M Offline
    M Offline
    mono
    wrote on 31 Oct 2024, 20:28 last edited by
    #1

    Hi,
    I am very new to Qt, I've only done a few small apps so I'm sorry if this is a silly question.
    I wanted to make a small library of items from a videogame I play. I created a class "Item" in the "item.h" file and as you can see the item has 3 attributes (name, price, description). My idea was to create list for each of them, so I could then push back the values in the list each time the "Item" constructor is called. The reason I want to do that is so that I could later use these lists to pass is as an argument to QCompleter and other widgets. But for some reason whenever i call the list in my "mainwindow.cpp" file, it gives an error of "undefined variable" or "invalid use to non-static member". I've tried adding the prefix "Item::", it stopped the error but it didn't display anything in the widget that I passed it to. The header is included in the file and it is also included in the .pro file, so I am confused as to where I'm making a mistake. Thanks for any help, the code is bellow

    item.h

    #ifndef ITEM_H
    #define ITEM_H
    #include<QString>
    #include<QStringList>
    
    class Item{ 
    public:
        QStringList l_name;
        QStringList l_description;
        QStringList l_price;
    
        QString name;
        QString description;
        int price;
    
        Item(QString n, int p, QString d)
        {
            name = n;
            description = d;
            price = p;
            saveItem();
        }
    
        void saveItem()
        {
            l_name.append(name);
            l_description.append(description);
            l_price.append(QString::number(price));
        }
    
    };
    
    #endif // ITEM_H
    

    mainwindow.cpp

    #include "window.h"
    #include "item.h"
    //#include "item.cpp"
    
    Item ammo_scav("Ammo Scav", 500, "description of item");
    Item extra_charge("Extra Charge", 500, "Description of item");
    
    window::window(QWidget *parent)
        : QWidget{parent}
    {
        setFixedSize(700, 700);
        lbl_name = new QLabel(l_name[0], this);
    }
    

    If I change to this, it works perfectly fine

    lbl_name = new QLabel(ammo_scav.name, this);
    
    J 1 Reply Last reply 31 Oct 2024, 20:46
    0
    • M mono
      31 Oct 2024, 20:28

      Hi,
      I am very new to Qt, I've only done a few small apps so I'm sorry if this is a silly question.
      I wanted to make a small library of items from a videogame I play. I created a class "Item" in the "item.h" file and as you can see the item has 3 attributes (name, price, description). My idea was to create list for each of them, so I could then push back the values in the list each time the "Item" constructor is called. The reason I want to do that is so that I could later use these lists to pass is as an argument to QCompleter and other widgets. But for some reason whenever i call the list in my "mainwindow.cpp" file, it gives an error of "undefined variable" or "invalid use to non-static member". I've tried adding the prefix "Item::", it stopped the error but it didn't display anything in the widget that I passed it to. The header is included in the file and it is also included in the .pro file, so I am confused as to where I'm making a mistake. Thanks for any help, the code is bellow

      item.h

      #ifndef ITEM_H
      #define ITEM_H
      #include<QString>
      #include<QStringList>
      
      class Item{ 
      public:
          QStringList l_name;
          QStringList l_description;
          QStringList l_price;
      
          QString name;
          QString description;
          int price;
      
          Item(QString n, int p, QString d)
          {
              name = n;
              description = d;
              price = p;
              saveItem();
          }
      
          void saveItem()
          {
              l_name.append(name);
              l_description.append(description);
              l_price.append(QString::number(price));
          }
      
      };
      
      #endif // ITEM_H
      

      mainwindow.cpp

      #include "window.h"
      #include "item.h"
      //#include "item.cpp"
      
      Item ammo_scav("Ammo Scav", 500, "description of item");
      Item extra_charge("Extra Charge", 500, "Description of item");
      
      window::window(QWidget *parent)
          : QWidget{parent}
      {
          setFixedSize(700, 700);
          lbl_name = new QLabel(l_name[0], this);
      }
      

      If I change to this, it works perfectly fine

      lbl_name = new QLabel(ammo_scav.name, this);
      
      J Online
      J Online
      JonB
      wrote on 31 Oct 2024, 20:46 last edited by JonB
      #2

      @mono
      l_name is a member variable of class Item.
      lbl_name = new QLabel(l_name[0], this); is inside window::window(), a constructor for some window class.
      The two classes have nothing to do with one another (so far as C++ is concerned), you can't access a variable in one from the other. Hence the "undefined variable" on l_name.
      If you had an instance of Item you might access item.l_name or item->l_name, but that's another matter.
      ammo_scav.name works because you have a global variable (probably not a good idea in itself) ammo_scav (of class Item) which is visible to window::window().

      You need to have a fair working knowledge of C++ to write Qt applications with it, Qt also offers a binding for Python which some people find easier than C++. Both of them require an understanding of classes and instances.

      M 1 Reply Last reply 1 Nov 2024, 11:00
      2
      • J JonB
        31 Oct 2024, 20:46

        @mono
        l_name is a member variable of class Item.
        lbl_name = new QLabel(l_name[0], this); is inside window::window(), a constructor for some window class.
        The two classes have nothing to do with one another (so far as C++ is concerned), you can't access a variable in one from the other. Hence the "undefined variable" on l_name.
        If you had an instance of Item you might access item.l_name or item->l_name, but that's another matter.
        ammo_scav.name works because you have a global variable (probably not a good idea in itself) ammo_scav (of class Item) which is visible to window::window().

        You need to have a fair working knowledge of C++ to write Qt applications with it, Qt also offers a binding for Python which some people find easier than C++. Both of them require an understanding of classes and instances.

        M Offline
        M Offline
        mono
        wrote on 1 Nov 2024, 11:00 last edited by
        #3

        @JonB I am kind of learning C++ as I progress with my Qt knowledge. So sometimes I find myself unaware of some basic knowledge, thank you for telling me

        P 1 Reply Last reply 1 Nov 2024, 18:45
        0
        • M mono has marked this topic as solved on 1 Nov 2024, 17:09
        • M mono
          1 Nov 2024, 11:00

          @JonB I am kind of learning C++ as I progress with my Qt knowledge. So sometimes I find myself unaware of some basic knowledge, thank you for telling me

          P Offline
          P Offline
          Pl45m4
          wrote on 1 Nov 2024, 18:45 last edited by
          #4

          @mono said in Cannot use QStringList defined in header file:

          I am kind of learning C++ as I progress with my Qt knowledge

          You should learn Qt as your C++ knowledge progresses, not the other way :)
          Solid C++ fundamentals are essential to enjoy working with Qt.
          You see what happens when you struggle with C++ basics ;-)


          If debugging is the process of removing software bugs, then programming must be the process of putting them in.

          ~E. W. Dijkstra

          1 Reply Last reply
          0

          1/4

          31 Oct 2024, 20:28

          • Login

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