PDA

View Full Version : Displaying complex info when using QSqlRelationalTableModel



scarleton
17th October 2010, 16:04
have two tables: Session and Image. The Session table has a foreign key (FK) to the Image table, Image_Id. The Image table has a number of fields, the two I am interested in is the blob that is the image and the field that is the name of the image.

I want to do something like:

sessionModel->setRelation(2, QSqlRelation("Image", "Image_Id", "<both image and name>"));

So the combo box shows both the image and the name. How is this done?

Sam

tbscope
17th October 2010, 17:25
If your question is only about displaying the image and the name in a line in a combobox:

Use a standard item model.
For each item in the model, set the data to the correct item role
Qt::DisplayRole -> the text being displayed: name of the image
Qt::DecorationRole -> the icon of the image

scarleton
17th October 2010, 18:07
part of my objective is to get as much of the Qt framework doing as much of the database interaction as I can, so I can code less and implement more:)

What I have so far is:



_photoSessionModel = new QSqlRelationalTableModel(this);
_photoSessionModel->setTable("PHOTO_SESSION");
_photoSessionModel->setEditStrategy(QSqlTableModel::OnRowChange);

connect(_photoSessionModel, SIGNAL(beforeUpdate(QSqlRecord&)), this, SLOT(on_photoSessionModel_beforeInsert(QSqlRecord&)));
connect(_photoSessionModel, SIGNAL(beforeUpdate(int,QSqlRecord&)), this, SLOT(on_photoSessionModel_beforeUpdate(int,QSqlRec ord&)));

_photoSessionMapper = new QDataWidgetMapper(this);
_photoSessionMapper->setModel(_photoSessionModel);
_photoSessionMapper->setItemDelegate(new QSqlRelationalDelegate(this));

_photoSessionMapper->addMapping(ui.directoryImageComboBox, _photoSessionModel->fieldIndex("PHOTO_SESSION_IMAGE_ID"));


The goal here is to add a QSqlRelation to the _photoSessionModel so that when the filter is changed on the model, the comboBox will do two things:

1: Change the list of images that show up in the combo box
2: Select the correct image, based on the id in the model.

What I do NOT want to have to do is update the model of the images, I am looking for Qt to do that for me. I am OK with creating the the image model, I am just looking for the Qt framework to do the updating when the _photoSessionModel.

The development I am doing is incremental right now, step one is getting this basic functionality working. Later I am going to want to come in and really fine tune how things are displayed, position, and what get highlighted, aka not the image itself, but just the name. So ideally I would like to create either a model or a delegate to display the images. I am not seeing anything on the QSqlRelation to allow me access to change the model. Can I simply use the default behavior of the QSqlRelation and then change the delegate on the comboBox to achieve what I am after?

Sam

the filter on the _photoSessionModel to the target session and have the model