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:
#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
#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:
ImageLabel(QWidget *parent = 0);
QString Imagetext;
void setImagetext(QString newValue);
QString imagetext();
signals:
void imageDropped(QString file);
void imagetextChanged(QString file);
public slots:
protected:
void dropEvent(QDropEvent *event) override;
void dragEnterEvent(QDragEnterEvent *event) override;
void dragMoveEvent(QDragMoveEvent *event) override;
void dragLeaveEvent(QDragLeaveEvent *event) override;
private:
QString m_imagetext;
};
#endif // IMAGELABEL_H
To copy to clipboard, switch view to plain text mode
This is the source:
#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;
}
#include "imagelabel.h"
ImageLabel::ImageLabel(QWidget *parent) :
QLabel(parent)
{
setAcceptDrops(true);
m_imagetext="";
}
void ImageLabel::dropEvent(QDropEvent *event)
{
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 subStr = QLatin1String("file:///");
QLatin1String newStr = QLatin1String("");
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();
QString text;
for (int i = 0; i < urlList.size() && i < 32; ++i)
text += urlList.at(i).path() + QLatin1Char('\n');
setText(text);
} else {
setText(tr("Cannot display data"));
}
setBackgroundRole(QPalette::Dark);
event->acceptProposedAction();
}
void ImageLabel::dragEnterEvent(QDragEnterEvent *event)
{
setBackgroundRole(QPalette::Highlight);
event->acceptProposedAction();
}
void ImageLabel::dragMoveEvent(QDragMoveEvent *event)
{
event->acceptProposedAction();
}
void ImageLabel::dragLeaveEvent(QDragLeaveEvent *event)
{
clear();
event->accept();
}
void ImageLabel::setImagetext(QString newValue)
{
m_imagetext = newValue;
if(!m_imagetext.isEmpty()){
QPixmap pixmap(m_imagetext);
pixmap = pixmap.scaled(width(), height(), Qt::KeepAspectRatio);
setPixmap(pixmap);
}else{
clear();
}
emit imagetextChanged(m_imagetext);
}
QString ImageLabel::imagetext()
{
return m_imagetext;
}
To copy to clipboard, switch view to plain text mode
It's mapped through my custom property "Imagetext":
mapper->addMapping(ui.photo_lbl, photoIndex, "Imagetext");
mapper->addMapping(ui.photo_lbl, photoIndex, "Imagetext");
To copy to clipboard, switch view to plain text mode
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:
connect(ui.
photo_lbl,
SIGNAL(imageDropped
(QString)),
this,
SLOT(setModelPhoto
(QString)));
.....................
void RepairDevices
::setModelPhoto(QString file) {
model->setData(model->index(mapper->currentIndex(), photoIndex), file);
}
connect(ui.photo_lbl,SIGNAL(imageDropped(QString)),this, SLOT(setModelPhoto(QString)));
.....................
void RepairDevices::setModelPhoto(QString file)
{
model->setData(model->index(mapper->currentIndex(), photoIndex), file);
}
To copy to clipboard, switch view to plain text mode
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?
Bookmarks