Nominate our 2022 Qt Champions!

QTreeWidget + SQL

  • Hello! I need to work in qtreewidget(or qtreeview) with sql quieries. How to do that? Also, I need to make hierarchical model in this widget with main sql quieries and the requests of the second and third order.
    Like that:
    --- London
    ------ East Café
    --- New York

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    What do you mean by second and third order ?

  • @SGaist In this example this is London, New York, East Café. I mean, the other sql quieries. You click on England and then you see new list, I talk about that. Sorry, if I gave incorrect name for that :)

  • Lifetime Qt Champion

    So you'd like to re-run a query for each entry and populate the tree widget ?

    Sounds pretty expensive.

    What are the relation between your different entries ?

  • @SGaist No, I don't want anything about that, because I just want to solve the problem. Every variant will be good.
    In my database there is 3 tables: country, city, streets. I want that:
    In QTreeWidget you see the list of countries, then if you will click on some country, you will see cities, and if you'll click on city you will see streets. Typical situation in this widget. But I don't know how to do that.

  • Lifetime Qt Champion

    Ok, then the most straight forward is to build your own model with its content based on the result of your queries.

  • @SGaist well, thank you, But I don't know how to do that. Can you explain that? Or write some links about how to solve that problem. Please, I have dedline in 7 days.

  • Lifetime Qt Champion

    QStandardItemModel's documentation details show how to build a table or a tree model. Based on that you can build it using your query results. So you'll start by querying the content of your Country table, then while you loop on the result you'll make a query for the cities of that country and again in that loop get the places.

    Note that this is the quick, simple and not optimized way to extract your data.

Log in to reply