Subclassed QLabel won't update the model
I have subclassed a QLabel in order to add the ability to connect to a database field through a QDataWidgetMapper.
This is my class' header:
Code:
#ifndef IMAGELABEL_H
#define IMAGELABEL_H
#include <QWidget>
#include <QLabel>
#include <qdebug.h>
#include <QDragEnterEvent>
#include <QDropEvent>
#include <QMimeData>
#include <QDebug>
#include <QPixmap>
class ImageLabel
: public QLabel{
Q_OBJECT
Q_PROPERTY(QString Imagetext READ imagetext WRITE setImagetext NOTIFY imagetextChanged
)
public:
void setImagetext
(QString newValue
);
signals:
void imagetextChanged
(QString file);
public slots:
protected:
private:
};
#endif // IMAGELABEL_H
This is the source:
Code:
#include "imagelabel.h"
ImageLabel
::ImageLabel(QWidget *parent
) :{
setAcceptDrops(true);
m_imagetext="";
}
{
const QMimeData *mimeData
= event
->mimeData
();
if (mimeData->hasImage()) {
setPixmap(qvariant_cast<QPixmap>(mimeData->imageData()));
} else if (mimeData->hasHtml()) {
setText(mimeData->html());
setTextFormat(Qt::RichText);
} else if (mimeData->hasText()) {
setText(mimeData->text());
setTextFormat(Qt::PlainText);
// remove string "file:///" from mimeData->text()
QString mimeDataText
= QString(mimeData
->text
().
replace(mimeData
->text
().
indexOf(subStr
), subStr.
size(), newStr
));
setImagetext(mimeDataText);
emit imageDropped(mimeDataText);
} else if (mimeData->hasUrls()) {
QList<QUrl> urlList = mimeData->urls();
for (int i = 0; i < urlList.size() && i < 32; ++i)
setText(text);
} else {
setText(tr("Cannot display data"));
}
event->acceptProposedAction();
}
{
event->acceptProposedAction();
}
{
event->acceptProposedAction();
}
{
clear();
event->accept();
}
void ImageLabel
::setImagetext(QString newValue
) {
m_imagetext = newValue;
if(!m_imagetext.isEmpty()){
pixmap = pixmap.scaled(width(), height(), Qt::KeepAspectRatio);
setPixmap(pixmap);
}else{
clear();
}
emit imagetextChanged(m_imagetext);
}
{
return m_imagetext;
}
It's mapped through my custom property "Imagetext":
Code:
mapper->addMapping(ui.photo_lbl, photoIndex, "Imagetext");
I works normally, it displays the images as expected.
But, in order to be saved in the model, I have to do it 'manually':
When an image is dropped, the label sends a signal named 'imageDropped'.
I use this signal to save the new value of the image path in the model:
Code:
connect(ui.
photo_lbl,
SIGNAL(imageDropped
(QString)),
this,
SLOT(setModelPhoto
(QString)));
.....................
void RepairDevices
::setModelPhoto(QString file) {
model->setData(model->index(mapper->currentIndex(), photoIndex), file);
}
So I set the model 's data this way.
But, shouldn't, automatically, the new value of the field to be saved in the model? Like every other field?
Obviously, I've done something wrong, that's why it's not auto-saved in the model.
How can I fix this?
Re: Subclassed QLabel won't update the model
I don't understand your problem. A QLabel is just a label. It doesn't know anything about models; likewise, a model knows nothing about where (or how) its content is displayed if anywhere. So if you want a change in the label that occurs through some user interaction to be saved in the model, you have to connect up the proper signals and slots (probably by writing your own, as you have done) to do that. Likewise, if your model is changed and you want to display a new photo on the label, you have to do the same thing from the other direction.
Re: Subclassed QLabel won't update the model
Quote:
Originally Posted by
d_stranz
I don't understand your problem. A QLabel is just a label.
And a QPlainTextEdit is just a textbox.
But, when it's text changes, it's new content is saved in the model, without having to use model->setData.
This same behavior I want for my subclassed QLabel.
Re: Subclassed QLabel won't update the model
Quote:
And a QPlainTextEdit is just a textbox.
No it isn't. It is a QWidget-based class designed for displaying and editing the contents of a QTextDocument. The interaction between the widget and its document are in the implementation of the class, and it is that implementation which results in the document's content being updated as it is being edited and the text edit's display likewise being updated if the document is changed programmatically.
Have you looked at the QLabel documentation? Do you see anything like a "QLabelDocument" or "QLabelModel" which serves the same purpose? Of course not, because there is no such thing. If you want a relationship between a QLabel and some QAbstractItemModel-based class, you have to write it yourself as well as write all the code needed to keep the model and label synchronized as you wish. From what you posted, it looks like you have pretty much done that.
Re: Subclassed QLabel won't update the model
Re: Subclassed QLabel won't update the model
I see that I misunderstood the OP's use of the QDataWidgetMapper. Thanks for pointing out this bug report so I could learn yet another new thing about Qt.