Cannot use QStringList defined in header file
-
wrote on 31 Oct 2024, 20:28 last edited by
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 bellowitem.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);
-
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 bellowitem.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);
wrote on 31 Oct 2024, 20:46 last edited by JonB@mono
l_name
is a member variable ofclass Item
.
lbl_name = new QLabel(l_name[0], this);
is insidewindow::window()
, a constructor for somewindow
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" onl_name
.
If you had an instance ofItem
you might accessitem.l_name
oritem->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 classItem
) which is visible towindow::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.
-
@mono
l_name
is a member variable ofclass Item
.
lbl_name = new QLabel(l_name[0], this);
is insidewindow::window()
, a constructor for somewindow
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" onl_name
.
If you had an instance ofItem
you might accessitem.l_name
oritem->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 classItem
) which is visible towindow::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.
-
-
@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
wrote on 1 Nov 2024, 18:45 last edited by@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 ;-)
1/4