PDA

View Full Version : QDataWidgetMapper & QLabel



fpujol
9th November 2007, 10:28
Hello,

I'm using QTableView to consult records and a QDialog for modify the records. In QTableView I use QItemDelegate for see images, that are saved in bytea format in postgresql database. I have obtained put the image in database in byte format:


if (ui.qLnFitxerSel->text().length()>0)
{
QPixmap pixmap(ui.qLnFitxerSel->text(), "PNG" );
QByteArray bytes;
QBuffer buffer(&bytes);
buffer.open(QIODevice::WriteOnly);
pixmap.save(&buffer, "PNG");

qSqlTableModel->setData(q->getAKSqlTableModel()->index(row,
akFrontData->getAKSqlTableModel()->fieldIndex("bandera")), bytes);
}

In my Qdialog I want to see the Image trought QLabel and I use the QDataWidgetMapper but this returns me the Image in byte information
so I see the QLabel something like this: PNG..![]... how I could do that
when QDataWidgetMapper return's me the information of qlabel it converts
it as Image and not in text, setting qLabel as QPixmap.




void AKEntitat::mapFields(QSqlQueryModel * model)
{
QSqlRecord qSqlRecord = model->record();

int count = qSqlRecord.count();

for (int i=0; i<=count; i++)
{
QSqlField qSqlCamp = qSqlRecord.field(i);

akCampBd = searchAKCampBd(qSqlCamp.name());
qLabel = searchLabel(qSqlCamp.name());

if (akCampBd!=0)
{
qDataWidgetMapper->addMapping(akCampBd->akCamp, i);
}

if (qLabel!=0)
{
qDataWidgetMapper->addMapping(qLabel, i);

}
}

qDataWidgetMapper->toFirst();

connect(akTaula->selectionModel(), SIGNAL(currentRowChanged(QModelIndex,QModelIndex)) ,
qDataWidgetMapper, SLOT(setCurrentModelIndex(QModelIndex)));

}

Thank you

wysota
9th November 2007, 11:00
You need to apply a custom item delegate on the widget mapper with reimplemented setEditorData() method that will set the image on the label for the appropriate combination of model index and editor, something like:

MyDelegate::setEditorData(QWidget *editor, const QModelIndex &index){
QLabel *lab = qobject_cast<QLabel*>(editor);
if(!lab || index.column()!=4){
QItemDelegate::setEditorData(editor, index);
return;
}
QImage img;
img.loadFromData(index.data(Qt::ImageRole).toByteA rray());
QPixmap px = QPixmap::fromImage(img);
lab->setPixmap(px);
}

fpujol
9th November 2007, 21:47
Hello Wysota,

Interesting answer but I comment one issue, my QTableView use a QSqlQueryModel, because I need to consult several tables for display records in QTableView. I use QSqlTableView for modify, delete ... records with combination of QSqlQuery. Then I refresh data of QTableView throught his QSqlQueryModel so the problem is that you code is not called because is not an editable table directly. Are we agree with this ?
So I'm thinking with another solution what do you think If I create a new class that Inherits from QLabel and then I mapper It with this class. The next new class code would be possible ?


AKImg::AKImg(const QString & text, QWidget * parent, Qt::WindowFlags f)
: QLabel(parent, f)
{
QPixmap qPixmap;
qPixmap.loadFromData(text.toLocal8Bit());

setWordWrap(true);
setAlignment(Qt::AlignCenter);
setPixmap(qPixmap);
}


AKImg::~AKImg()
{
}

jpn
9th November 2007, 23:05
I use QSqlTableView for modify, delete ... records with combination of QSqlQuery.
Could you elaborate? Are you using some custom way to edit items?

Then I refresh data of QTableView throught his QSqlQueryModel so the problem is that you code is not called because is not an editable table directly.
And why can't you use the built-in mechanism for editing items?

fpujol
10th November 2007, 11:52
Sorry for modify records I use QSqlTableModel with a QDialog. I don't want that user edit my QTableView directly so I use a QDialog.

wysota
11th November 2007, 16:07
What do you use the widget mapper for then?

fpujol
12th November 2007, 11:22
I use QDataWidgetMapper to get information of QSqlQueryModel and represent it in my QDialog, is faster and no need very code.

wysota
12th November 2007, 12:30
You can do it without the mapper as well. The amount of code is practically the same. Using the mapper makes sense if you want to transfer the data back to the model (simply speaking when using it for editing your model). So I suggest you change your query model to some other model (like QSqlTableModel) and use the mapper for things it was designed for. You'll save yourself a lot of effort this way.