Qt World Summit: Register Today!

Create new window with QTableView from ComboBox

  • Hello everyone, this is my first time here and my experience with Qt is about 2 months.

    My question is:

    I have an ui form which will be the main window and will contain a ComboBox. In this ComboBox, imagine I have two options ( Car1 and Car2 ). What I want to do seems to be very simple. If I select the first option ( Car1 ), there will be a new window, which will contain a table (QTableView) with two columns ("Variable" and "Value" in its headers), and three rows ("Car name", "Price" and "Power not in the headers, but under the column "Variable") and under the column "Value" there will be for example "Nissan GTR" , " 300.000,00 " and " 500 ". So:

    | Variable | Value |
    | Car name | Nissan GTR |
    | Price | 300.000,00 |
    | Power | 500 |

    Also, the information for filling the table cells will be stored in a header (.h) either in an array, or in a vector.

    Does anyone have an idea how can I implement this?

    Thank you,

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Sounds a bit like a school assignment ;)

    How would you store these value in the header file ?

    Do you have to use a database solution for that software ?

  • Hello SGaist, thank you for your attention.

    The goal of this software is to create a search mechanism but using a GUI that I want to design.

    Since there are numbers and letters probably I will have to store it in two vectors right?? (I accept suggestions for this)

    But what do you mean with database solution? I'm sorry, I cound't understand it but did I make it more clear now?

    Can you help me with some sort of codings?


  • I can see two different way to do for you (but maybe there is some more):

    Use a database (as SGaist tell you) for memorize your data (and also use some search queries you need)

    use a file (or some files) for store data (like XML files)

    But the "best" way, for sure, would be to use a database.

    So... you have to learn around "SQL database", like SQLite (i think in your case SQLite is good) or MySQL. It is really easy. you just have to install one (if there is not one installed allready), access the database (maybe with a generic GUI tool) create a user, a database, with his table(s). After that, you can stock and search datas from/to your database with some Qt Model designed for SQL databases datas.

    For your application, you have to acces a signal/slot from your comboBox (if you change the value, hang the slot...), and then, open your required DialogBox for print your datas. It is easy to do.
    There is some other design choices also... like have, in your mainWindow (or in a dialogBox), two widgets linked: a listView and a table view. Or (if you are more than cars) a listView linked with stackedWidget, and inside each stackContainer, what you need....

    I think that comboBox is good for write an option (like enum do), but for do a choice of selection... like a search engine could use it... (i think you want to do this) the easy way would be to link the result with a model/view (from data stored in a database) directly inside the same window you do the choice. But sure, you can also shox them in a kind of dialogBox.

    first of all, do the choice of use a technic for stock data (file or database), and then, learn around this and model for show datas in a view. Do the ui job and call dialogBox or link Widgets are the more easy part of this job.

  • Hello jerome_isAviable? I appreciate your help.

    I'll work on this today, and will do the choice of either using a database or files. But before that, I have to ask you if in case I choose the database, will I be able to convert my Project into na .exe later? Because this is the latest thing I want to do, and don't want to make it more difficult by the time I do it.

    So actually, what I was thinking is:

    if I use, the currentIndexChanged(int) then I would catch the index int number, for example (2), and it would be used emitted through a signal to this new dialogBox containing the QTableView, then, what will be written in the cells of the QTableView would be an information stored in na array for example and in this position. (example: array[2] ).

    I think the problem that I'm having right now is to create this signal that will either send the index number and also create a new dialog box containing the QTableView.

    This is what I have for now:


    @#include "telaprincipal.h"
    #include "ui_telaprincipal.h"
    #include <QtWidgets>

    TelaPrincipal::TelaPrincipal(QWidget *parent) :
    ui(new Ui::TelaPrincipal)



    delete ui;

    and two forms:


    !C:\Users\marcos.fernandes\Desktop\Capturar1(only contain a comboBox)!


    !C:\Users\marcos.fernandes\Desktop\Capturar2(only have a QTableView inside)!

    Repeating the question:

    How can I create a new dialogBox after changing the index of the ComboBox which contain the car.ui form, and when it opens, the index number is passed as na argument so it search inside an array with this index number and show it in the cells of the QTableView?

  • no problem, but you have to consider also that i'm not an expert in QT or C++... i just tell you my opinion, some other here would have better consideration for this... but ok... if i'm wrong or not give you a good idea, an other one will correct it quickly (peoples here are really quick and active, that's fine):

    for your first question around .exe file and database, there is two choice to be possible (or maybe more... but i know 2):

    embed a small database inside the package at install time... it exist some little db to be embeded i see on google... consider what you really need with it (big datas... powerfull SGBD or not?)

    at install time, ask for dependency and make your customer install the database (if this one is not allready installed inside the system... so you have to check your db to use with also this consideration... i think, because you want to create an exe file, that you want it for windows... so check wich db windows use allready in his system, and, if legaly you can use it too... and for microsoft windows, i really don't know)

    for index use, my way to use it is to catch signal from:
    @void my_treeView::on_yourTreeView_clicked(const QModelIndex &index)" {
    // here i can use directly the index argument provide by the event...
    for me, it is more easy like that. But it is a choice to do.

    for the other stuff...
    i think you can try to have a look on "model" use. This can, for exemple, be a great interface between your database or xml file and (your datas) or variable structure or other and your view.
    This could be great because:

    model/view is the convention of Qt style construction

    models objects have some nice tools ready for use (no need to do it again)

    and you can also implment it if you want something special or customized

    after, the objects you create is an other choice (i think a good choice) too... you can do it also with model/view (i use it i my code, and i'm happy with it).

    So... what i try to point is that:
    Qt is for object oriented code writing and use model/view (no controller) style. This style is powerfull. There is allready many tools for write code like that... but you are also free to do an other choice.
    My way is to follow the style of Qt for write with Qt, because this is more easy and this convention is also good for maintain a code and/or make it grow after. With this style, if you want to do more, you not have to change all your code... that is fine. Also, view and datas are more separate with model style, and then, you will be more free to change something without pain.

    Also, model/view is not complicate to learn and use (except the doc who is difficult for me... but maybe for you it will be ok...) and make you win lot of time.

    For databases, you have to take information around what you could use for your way, and also, consider that SQL langage is easy and offer powerfull tools for search datas... but sure... you also need to create good design tables for never have redondant datas inside your tables (this is also an advantage of database use). For me (but i don't know Windows), SQLite is a great choice for little job and not heavy to embed with... you can look on wikipedia the list of database who can exist... but if your project is simple and will stay "little", maybe file use could be breat too. Also, for exemple, i have to use file storage and database together... because question of access time for data-recording in live (with video in acquisition in parallel) with files could be better for me, but for stock datas others, database is needed. So i use it together.

    other ui consideration is up to your idea... we have all, in this world, different ideas. My ideas (but i don't really know all the works you have to do with your application) would be to let them in the same window and use a QListView linked with a QTableView and mount data by model. It would be powerfull and clear (for me).
    But if the list of choice is little, comboBox can do the job too... or maybe a group with many comboBox... but linked in the same window with a tableView (also, you can make it hide/show with a checkbox).
    I would use an other dialogBox only if i ask a question or do a configuration of the application, or use something need an answer.

    but all of this is some creative idea part... i think no one can really said what is the "best"... it is like the fashion way... this have to be different in the head of peoples by the time and we not see the world the same color too... but you can do what you want with Qt.

Log in to reply