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. QListView prefilled with items on call?
Forum Updated to NodeBB v4.3 + New Features

QListView prefilled with items on call?

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 6 Posters 2.1k 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.
  • B Offline
    B Offline
    becht
    wrote on last edited by VRonin
    #1

    Hello!
    Im pretty new to programming so please dont go too hard on me.
    Im working with visual studios 2017 and wanna create a small product list via a list widget.
    When I open the dialog window, i want the list to already contain a few objects the user can choose from
    (Its supposed to be a small online shopping list, user can choose which product, e.g. an applen he wants to put in his basket.)
    It should look like this:

    0_1519987370801_Unbenannt.PNG

    This window is just a dialog window, so here is the code to open this dialog.
    Basically ive got a Button (einkaufButton) that calls to open the window when it clicks.

    void Nahrungsmitteleinkauf::on_einkaufButton_clicked()
    {
    	Produktauswahl *dialog = new Produktauswahl();
    	
    	dialog->exec();
    }
    

    The constructor for the dialog "Produktauswahl" looks like this

    Produktauswahl::Produktauswahl(QWidget *parent)	: QDialog(parent)
    {
    	setupUi(this);	
    }
    

    so pretty empty.

    How can I implement a few basic products into this list widget now, so theyre already inside the window (shown in picture above, green writing) when I open the window via einkaufButton?

    Hopefully you can understand what I want and help me out!
    Bear in mind im new to qt, so be nice haha
    kind regards

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by VRonin
      #2

      After setupUi(this); add something like:
      ui->tableView()->setModel(new QStringListModel(QStringList() << "Apple" << "Pear",this));
      Or switch to QListWidget instead of QListView

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      1
      • B Offline
        B Offline
        becht
        wrote on last edited by becht
        #3

        0_1520234858779_fforum.PNG

        I only had to insert those short lines into the standardconstructor to insert new items when i call the dialog window.
        I can now open it via button click, and it already comes with those items!

        Taz742T 1 Reply Last reply
        1
        • B becht

          0_1520234858779_fforum.PNG

          I only had to insert those short lines into the standardconstructor to insert new items when i call the dialog window.
          I can now open it via button click, and it already comes with those items!

          Taz742T Offline
          Taz742T Offline
          Taz742
          wrote on last edited by
          #4

          @becht Hi!
          Where are these items loaded in your listWidget?

          Do what you want.

          B 1 Reply Last reply
          0
          • Taz742T Taz742

            @becht Hi!
            Where are these items loaded in your listWidget?

            B Offline
            B Offline
            becht
            wrote on last edited by
            #5

            @Taz742
            what do you mean loaded?
            Basically, I have this window popping up when I run the program: 0_1520256308039_1.PNG

            Upper button is supposed to open the window with the prefilled items.
            It does exactly that now:

            0_1520256332052_2.PNG

            Whenever I click the button, it opens the window this way.
            Now i can select my 'Products' , e.g. Item #1 'Apfel' and put it into my basket (List Widget on right side) when I click the Button 'Hinzufügen'

            0_1520256428401_3.PNG

            When I click at OK Button, the window closes back to main window.
            I can then select the upper button again to open the big window, and the items I have selected before are still in my basket (List Widget on right side).

            Its just a practise for me, making a kind of online store program.
            Now im at the part where I should add up the full price for all the products Ive put into my basket/cart.
            Is there a way to like, add more than one attributes to an item in the ListWidget?
            For example, first Item's name is 'Apfel', and now whenever I put an Apfel (item 1) into my cart/basket, I want to add the price for one Item1 onto a full pricelist (the bottom line edit, 'Gesamtpreis' is supposed to show the amount ull have too pay for all the products youve put into your cart).
            I could basically hardcode it into a member of the class I think.
            Whenever I put a product into my card, the code is looking like this:

            0_1520256771297_bb979430-c95f-4c16-9bc8-24f430c156ca-image.png

            Now I could for example create a double fullprice; `
            member in my class and after each case put a line in that adds up into member fullprice?
            such as

            if (ProdukteWidget->currentRow() == 0)
            	{
            		EinkaufswagenWidget->addItem("Apfel");
                            fullprice += 0.49;
            	}
            

            I guess this way would work to display the amount of full price to pay for my products, but the price still isnt really associated to my product if you know what i mean?

            For example if I decide to add in a feature that displays the price of one of the prefilled items/products, I would have to hardcore for each case again (imagine hardcoding for 200items again if its a big online store), which is lots of code again and I guess it can be solved way faster and easier?
            So can I associate like a price to one of those items so I can use one of QTs built in functions to return the price somehow?

            jsulmJ JonBJ 2 Replies Last reply
            0
            • B becht

              @Taz742
              what do you mean loaded?
              Basically, I have this window popping up when I run the program: 0_1520256308039_1.PNG

              Upper button is supposed to open the window with the prefilled items.
              It does exactly that now:

              0_1520256332052_2.PNG

              Whenever I click the button, it opens the window this way.
              Now i can select my 'Products' , e.g. Item #1 'Apfel' and put it into my basket (List Widget on right side) when I click the Button 'Hinzufügen'

              0_1520256428401_3.PNG

              When I click at OK Button, the window closes back to main window.
              I can then select the upper button again to open the big window, and the items I have selected before are still in my basket (List Widget on right side).

              Its just a practise for me, making a kind of online store program.
              Now im at the part where I should add up the full price for all the products Ive put into my basket/cart.
              Is there a way to like, add more than one attributes to an item in the ListWidget?
              For example, first Item's name is 'Apfel', and now whenever I put an Apfel (item 1) into my cart/basket, I want to add the price for one Item1 onto a full pricelist (the bottom line edit, 'Gesamtpreis' is supposed to show the amount ull have too pay for all the products youve put into your cart).
              I could basically hardcode it into a member of the class I think.
              Whenever I put a product into my card, the code is looking like this:

              0_1520256771297_bb979430-c95f-4c16-9bc8-24f430c156ca-image.png

              Now I could for example create a double fullprice; `
              member in my class and after each case put a line in that adds up into member fullprice?
              such as

              if (ProdukteWidget->currentRow() == 0)
              	{
              		EinkaufswagenWidget->addItem("Apfel");
                              fullprice += 0.49;
              	}
              

              I guess this way would work to display the amount of full price to pay for my products, but the price still isnt really associated to my product if you know what i mean?

              For example if I decide to add in a feature that displays the price of one of the prefilled items/products, I would have to hardcore for each case again (imagine hardcoding for 200items again if its a big online store), which is lots of code again and I guess it can be solved way faster and easier?
              So can I associate like a price to one of those items so I can use one of QTs built in functions to return the price somehow?

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

              @becht Well, you need to have a mapping between products and prices somewhere. For such application one would usually use a database to store all available products with all information related to them. So, I would suggest to take a look at SQL databases (you can simply use SQLite).

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

              1 Reply Last reply
              0
              • B becht

                @Taz742
                what do you mean loaded?
                Basically, I have this window popping up when I run the program: 0_1520256308039_1.PNG

                Upper button is supposed to open the window with the prefilled items.
                It does exactly that now:

                0_1520256332052_2.PNG

                Whenever I click the button, it opens the window this way.
                Now i can select my 'Products' , e.g. Item #1 'Apfel' and put it into my basket (List Widget on right side) when I click the Button 'Hinzufügen'

                0_1520256428401_3.PNG

                When I click at OK Button, the window closes back to main window.
                I can then select the upper button again to open the big window, and the items I have selected before are still in my basket (List Widget on right side).

                Its just a practise for me, making a kind of online store program.
                Now im at the part where I should add up the full price for all the products Ive put into my basket/cart.
                Is there a way to like, add more than one attributes to an item in the ListWidget?
                For example, first Item's name is 'Apfel', and now whenever I put an Apfel (item 1) into my cart/basket, I want to add the price for one Item1 onto a full pricelist (the bottom line edit, 'Gesamtpreis' is supposed to show the amount ull have too pay for all the products youve put into your cart).
                I could basically hardcode it into a member of the class I think.
                Whenever I put a product into my card, the code is looking like this:

                0_1520256771297_bb979430-c95f-4c16-9bc8-24f430c156ca-image.png

                Now I could for example create a double fullprice; `
                member in my class and after each case put a line in that adds up into member fullprice?
                such as

                if (ProdukteWidget->currentRow() == 0)
                	{
                		EinkaufswagenWidget->addItem("Apfel");
                                fullprice += 0.49;
                	}
                

                I guess this way would work to display the amount of full price to pay for my products, but the price still isnt really associated to my product if you know what i mean?

                For example if I decide to add in a feature that displays the price of one of the prefilled items/products, I would have to hardcore for each case again (imagine hardcoding for 200items again if its a big online store), which is lots of code again and I guess it can be solved way faster and easier?
                So can I associate like a price to one of those items so I can use one of QTs built in functions to return the price somehow?

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #7

                @becht

                but the price still isnt really associated to my product if you know what i mean?
                So can I associate like a price to one of those items

                In addition to what @jsulm has said about using a database, to "associate" lots of "attributes"/"properties" with an object, it's probably time you looked at C++ classes. A class can have lots of member variables for the "properties" of your objects. So for example (excuse my syntax, I'm not a C++ programmer), something like:

                class Product:
                    QString m_name;
                    double m_price;
                    int m_numStock;
                    ...
                
                    Product(QString name, double price, int numStock)
                    {
                        m_name = name;
                        m_price = price;
                        m_numStock = numStock;
                        ...
                    }
                

                Now when you create a Product instance, e.g. new Product("Apfel", 0.49, 200), you have "associated" the name, price & stock level with that instance.

                To calculate the total cost of the basket, you do not store that "running total" in these objects. You calculate it on the fly. For example, if (for the sake of example, not recommended) you had a 10 element array of chosen products:

                Product arrayOfproducts[10] =
                {
                    { "Apfel", 0.49, 200 },
                    { "Birne", 1.23, 100 },
                    ...
                };
                
                double totalCost = 0.0;
                for (int i = 0; i < 10; i++)
                    totalCost += arrayOfProducts[i].m_price;
                

                I'm just giving you a heads-up about the kind of approach one could take. I hope it helps, not confuses!

                B 1 Reply Last reply
                0
                • JonBJ JonB

                  @becht

                  but the price still isnt really associated to my product if you know what i mean?
                  So can I associate like a price to one of those items

                  In addition to what @jsulm has said about using a database, to "associate" lots of "attributes"/"properties" with an object, it's probably time you looked at C++ classes. A class can have lots of member variables for the "properties" of your objects. So for example (excuse my syntax, I'm not a C++ programmer), something like:

                  class Product:
                      QString m_name;
                      double m_price;
                      int m_numStock;
                      ...
                  
                      Product(QString name, double price, int numStock)
                      {
                          m_name = name;
                          m_price = price;
                          m_numStock = numStock;
                          ...
                      }
                  

                  Now when you create a Product instance, e.g. new Product("Apfel", 0.49, 200), you have "associated" the name, price & stock level with that instance.

                  To calculate the total cost of the basket, you do not store that "running total" in these objects. You calculate it on the fly. For example, if (for the sake of example, not recommended) you had a 10 element array of chosen products:

                  Product arrayOfproducts[10] =
                  {
                      { "Apfel", 0.49, 200 },
                      { "Birne", 1.23, 100 },
                      ...
                  };
                  
                  double totalCost = 0.0;
                  for (int i = 0; i < 10; i++)
                      totalCost += arrayOfProducts[i].m_price;
                  

                  I'm just giving you a heads-up about the kind of approach one could take. I hope it helps, not confuses!

                  B Offline
                  B Offline
                  becht
                  wrote on last edited by
                  #8

                  @JonB
                  thanks alot for your answer, using a class and creating instances/objects was my next approach, of course I am familiar with c++ classes already.
                  I just dont quite get where I should create said instances.
                  When I was working only with c++ without qt, I would mostly create my objects in the main.cpp of my projects, but with QT I guess this is not the case anymore.
                  So its a bit hard for me to understand
                  a) where to create the objects (in the .h file of my QT created ui?)
                  0_1520328232295_1.PNG

                  This is basically the base window, which leads to all the smaller dialog windows/functions (put products in basket, view product, pay products and so on) and I can imagine Ill have to create those objects/products in the .h or .cpp file of this base window.
                  Or do I have to create the instances in their own .cpp file?
                  E.G make class Products with name and price member, and in the standardconstructor fill in the names and prices of those products?

                  b) and if a) is the case, how to connect those objects in different QTDialog classes, so I can basically get the names/prices from those objects in different dialog windows.

                  Im really sorry for my long texts, but I'm trying to get you as many information about what I'm doing as possible and im very eager to learn this because its such an interesting but complex topic
                  very kind regards!

                  JonBJ J.HilkJ 2 Replies Last reply
                  0
                  • B becht

                    @JonB
                    thanks alot for your answer, using a class and creating instances/objects was my next approach, of course I am familiar with c++ classes already.
                    I just dont quite get where I should create said instances.
                    When I was working only with c++ without qt, I would mostly create my objects in the main.cpp of my projects, but with QT I guess this is not the case anymore.
                    So its a bit hard for me to understand
                    a) where to create the objects (in the .h file of my QT created ui?)
                    0_1520328232295_1.PNG

                    This is basically the base window, which leads to all the smaller dialog windows/functions (put products in basket, view product, pay products and so on) and I can imagine Ill have to create those objects/products in the .h or .cpp file of this base window.
                    Or do I have to create the instances in their own .cpp file?
                    E.G make class Products with name and price member, and in the standardconstructor fill in the names and prices of those products?

                    b) and if a) is the case, how to connect those objects in different QTDialog classes, so I can basically get the names/prices from those objects in different dialog windows.

                    Im really sorry for my long texts, but I'm trying to get you as many information about what I'm doing as possible and im very eager to learn this because its such an interesting but complex topic
                    very kind regards!

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by JonB
                    #9

                    @becht
                    Bearing in mind that I'm not a C++-er....

                    You do not want to create your product classes in any "Qt UI file". You want to keep UI & "back-end" separate. Products in themselves have nothing to do with how you might want to display/interact with them.

                    You would create them in product.cpp & product.h. Then you include the .h file in any other files you might have which also include Qt UI files (e.g. your main.cpp), just like you would in "plain" C++, so that UI code can access them. Qt is not relevant here, and does not stop you including whatever else you need.

                    1 Reply Last reply
                    0
                    • B becht

                      @JonB
                      thanks alot for your answer, using a class and creating instances/objects was my next approach, of course I am familiar with c++ classes already.
                      I just dont quite get where I should create said instances.
                      When I was working only with c++ without qt, I would mostly create my objects in the main.cpp of my projects, but with QT I guess this is not the case anymore.
                      So its a bit hard for me to understand
                      a) where to create the objects (in the .h file of my QT created ui?)
                      0_1520328232295_1.PNG

                      This is basically the base window, which leads to all the smaller dialog windows/functions (put products in basket, view product, pay products and so on) and I can imagine Ill have to create those objects/products in the .h or .cpp file of this base window.
                      Or do I have to create the instances in their own .cpp file?
                      E.G make class Products with name and price member, and in the standardconstructor fill in the names and prices of those products?

                      b) and if a) is the case, how to connect those objects in different QTDialog classes, so I can basically get the names/prices from those objects in different dialog windows.

                      Im really sorry for my long texts, but I'm trying to get you as many information about what I'm doing as possible and im very eager to learn this because its such an interesting but complex topic
                      very kind regards!

                      J.HilkJ Offline
                      J.HilkJ Offline
                      J.Hilk
                      Moderators
                      wrote on last edited by
                      #10

                      @becht hi,

                      first of you shouldn't modify the ui.h file. that one is generated on the fly each time you hit qmake!!!

                      For your class/struct you simply create it like any other cpp class and whereever you want to use it you include the header file.
                      That could be your main.cpp or any other class.

                      If your class does not have any UI-elements but you want to use Qt's MetaOjectSystem and/or use Signal and Slot let your calss inherit from QObject.
                      If your calss describes a part of your UI and you want to display it directly than inherit - generally speaking - from QWidget.

                      For a simple Data/Property storage, create a normal cpp class or use a struct - its quit litteraly the same.

                      Do decide what you should use, really depends on how you want to display and interact with the data.


                      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                      Q: What's that?
                      A: It's blue light.
                      Q: What does it do?
                      A: It turns blue.

                      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