Important: Please read the Qt Code of Conduct -

QListView prefilled with items on call?

  • 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:


    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();

    The constructor for the dialog "Produktauswahl" looks like this

    Produktauswahl::Produktauswahl(QWidget *parent)	: QDialog(parent)

    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

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

  • 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!

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

  • @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:


    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'


    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:


    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)
                    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?

  • Qt Champions 2019

    @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).

  • @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!

  • @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?)

    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!

  • @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.

  • Moderators

    @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.

Log in to reply