Wednesday, 15 April 2015

c++ - QRelationalTable category with relation -



c++ - QRelationalTable category with relation -

i have little problem in qt. have 2 tables, 1 products, 1 categories. want display list of products in qtableview. no big deal. it's not big deal assign relation , display name of category instead of id in 1 column.

but, in categories have internal relation, can create subcategories. it's done simple creating additional column parentid.

and here problem... how can display relation in qtableview? mean instead of displaying name of category products belong to, display parent categories also, illustration way:

have 3 categories:

food (with no parent) fruits (with parent food) citrus(with parent fruits)

product:

orange assigned category citrus.

in tableview in column category should be:

food/fruits/citrus

or

food->fruits->citrus

or that...

how it? subclass qrelationaltablemodel , create additional relation? or create delegate column? or maybe both?

arleady did it...

what i've done: table categories

create table categories (id integer primary key autoincrement, name varchar(100), parent numeric)

created table categories_closure (all sql statements sqlite) :

create table categories_closure (parent numeric, kid numeric, depth numeric)

next, created triggers automate closure table

create trigger categories_delete after delete on categories begin delete categories_closure child=old.id; update categories set parent=old.parent parent=old.id; end; create trigger categories_insert after insert on categories begin insert categories_closure (parent, child, depth) values (new.id, new.id, 0); insert categories_closure (parent, child, depth) select p.parent, c.child, p.depth+c.depth+1 categories_closure p, categories_closure c p.child=new.parent , c.parent=new.id; end; create trigger categories_update after update of id,parent on categories begin delete categories_closure child=old.id; insert categories_closure (parent, child, depth) values (new.id, new.id, 0); insert categories_closure (parent, child, depth) select p.parent, c.child, p.depth+c.depth+1 categories_closure p, categories_closure c p.child=new.parent , c.parent=new.id; update categories set parent=new.id parent=old.id; end;

now can string of parent categories simple query, co created view

create view categories_resolved select id, group_concat(name, ' ') fullname, group_concat(parent) branch ( select c.id, cc.parent, c2.name categories c left bring together categories_closure cc on c.id=cc.child left bring together categories c2 on c2.id=cc.parent order cc.depth desc ) grouping id order id asc

and used view on relation in qsqlrelationaltablemodel instead of categories table. simple, , no need creating model, view, proxy, delegate or modify code of qt classes.

c++ sql qt

No comments:

Post a Comment