How to display content of multiple QSqlTableModels in one QTableView?

人盡茶涼 提交于 2019-12-19 04:58:32

问题


I have a MySql table, let's call it x:

CREATE TABLE x (
    Id int unsigned NOT NULL PRIMARY KEY AUTO_INCREMENT,
    A int unsigned NOT NULL,
    B int,
    FOREIGN KEY (A) REFERENCES y(Id)
);

And then I have another table, let's call it y:

CREATE TABLE y (
    Id int unsigned NOT NULL PRIMARY KEY AUTO_INCREMENT,
    First varchar(255),
    Last varchar(255)
);

I want to display table x in one QTableView and in place of column A from table x I want to display columns First and Last from table y from row whose Id is equal to A from table x.

Do you have any ideas? Let me know if my explanation is not clear enough.


回答1:


You can use QSqlQueryModel with sql join query:

QSqlQueryModel *model = new QSqlQueryModel;
model->setQuery("SELECT x.Id, y.First, y.Last, x.B FROM x "
                "LEFT JOIN y ON x.A = y.Id");

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


来源:https://stackoverflow.com/questions/17156286/how-to-display-content-of-multiple-qsqltablemodels-in-one-qtableview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!