MultipleModels/oneView



  • Qt has provided a Quick way to display data in a grid or tree using model/view architecture, but they assumed that one view can display one model using one datasource like SQL table, I need away to display multiple Models at the same time in one tableView .

    the problem come from the need of doing many DML operations visually by the end user.



  • Could you be more specific about your problem?

    BTW you could maybe unite your data before viewing it, or making up your model...



  • @ QSqlTableModel *model = new QSqlTableModel();
    model->setTable("person");

    QTableView *view = new QTableView;
    view->setModel(model);@
    

    what if I need to insert data like this :
    person: name,lastname,job,;
    contactInfo: personId,means,type

    the above data have to be in the same view.



  • What about the QSqlRelationalTableModel? I think for this kind of relation you can use that model.



  • In general, you have proxy modles where you can combine data from different sources, you could create your own model that collects data from different souces...

    As it seams, you want to use SQL models, I would try using a proxy model on top of the original ones to combine the data.



  • Ok Vcsala,
    but this is the maximum number of tables you can use at the same time ,you can use two tables.

    it is common to display all cells as one row from different tables to be updated by the user at the same time.



  • Yes, Gerof is right.

    (but you can set more colums and so on in an QSqlTableModel)



  • syrianzoro, I think that custom model will be the solution for your problem. It will take some efforts to make it, but it will cover all your neads.



  • Denis Kormalev, I think you are right, but I try to check if any one have other way , that will be annoying to make ModelList :) , and I will be obliged to make a custom QTableView .



  • You shall not create a view, you shall create a model. And creating models is not so dificult, I did that many times now.

    You just have to decide, whether you create your own model or a proxy model. If you don't need the single base models stand alone, creating one model is easier than creating a proxy model.

    More about creating xutsom model can be found "here":http://doc.qt.nokia.com/4.7/model-view-programming.html

    Afaik there are no model inside Qt where you can combine different sources into one model. Perhaps there is some thirt party solution, but that I don't know.



  • [quote]and I will be obliged to make a custom QTableView [/quote]
    Why? If you have custom model it doesn't force you to create your own view as long as your model inherits QAbstractItemModel.



  • It does not make any sense that a (Table)View works on more than one model. From both a programmers and a users view this is not intuitive and error prone.

    As Gerolf already suggested, it's the best to make either your own ItemViewModel or try to combine the existing ones (you can add more than one table to QSqlRelationalTableModel).



  • It seems that you are gathering data from a database. If that is the case and you just need a read-only view you can just combine the data sources (tables) on your sql statement and use a QSqlQueryModel



  • [quote author="syrianzoro" date="1293645179"]but this is the maximum number of tables you can use at the same time ,you can use two tables.[/quote]

    Actually it is not true, the number of the tables is not limited. You have a master table which has several foreign keys to other tables and you can set all the relations to those tables. If you want to do a read/write model it is recommended to use the QSqlRelationalDelegate.



  • I have same question. I have tableview and I want to display multiple tables from MYSQL on same view at a time. Is there any solution exist without creating custom model/view??



  • No Solutions..!!!



  • I agree with fcrochik. Create a view in SQL server with all of you related data, test it from the SQL sandbox and use it.



  • [quote author="nasit.bhavin" date="1372075788"]I have same question. I have tableview and I want to display multiple tables from MYSQL on same view at a time. Is there any solution exist without creating custom model/view??[/quote]

    For the people who have multiple tables, are they the same set of columns and such? If so, why are they separate tables? And can you do an SQL query which presents all of your tables as a view? Because joining completely arbitrary tables in a single view would tend to be a non sensical thing to do. I am trying to understand the use case that people are looking for. I mean you can't sort a column on half bool's and half datestamps in a way that is particularly intuitive. Likewise, how do you define rowCount() in a model that has models with differing rowCounts. I can't see any general purpose way to do it that would make everybody happy. Merging models into a single model seems like a very application specific sort of task.



  • Hey Volker,
    I don't understand why you said it's confusing and not usual! Let's say I have a table for customer and other one for address and the address table has some fields like 'zip_code', 'city','state' and so on. What should I do if I want to show a TableView with the complete information about customers? The QSqlRelationalTableModel just allow me to link a foreign key to a field on other table but in fact I want to link one foreign key to other table and show all fields of that table. Does someone have a solution?



  • @plinioandrade said in MultipleModels/oneView:

    Hey Volker,
    I don't understand why you said it's confusing and not usual! Let's say I have a table for customer and other one for address and the address table has some fields like 'zip_code', 'city','state' and so on. What should I do if I want to show a TableView with the complete information about customers? The QSqlRelationalTableModel just allow me to link a foreign key to a field on other table but in fact I want to link one foreign key to other table and show all fields of that table. Does someone have a solution?

    Any solution?



  • @seyed Write the SQL query with all the relations you need and run it using QSqlQuery then populate a QStandardItemModel with the results


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.