Results 1 to 6 of 6

Thread: Subclassed QLabel won't update the model

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2017
    Posts
    19
    Thanks
    5
    Qt products
    Qt5

    Default 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:
    Qt Code:
    1. #ifndef IMAGELABEL_H
    2. #define IMAGELABEL_H
    3.  
    4. #include <QWidget>
    5. #include <QLabel>
    6. #include <qdebug.h>
    7. #include <QDragEnterEvent>
    8. #include <QDropEvent>
    9. #include <QMimeData>
    10. #include <QDebug>
    11. #include <QPixmap>
    12.  
    13. class ImageLabel : public QLabel
    14. {
    15. Q_OBJECT
    16. Q_PROPERTY(QString Imagetext READ imagetext WRITE setImagetext NOTIFY imagetextChanged)
    17.  
    18. public:
    19. ImageLabel(QWidget *parent = 0);
    20.  
    21. QString Imagetext;
    22.  
    23. void setImagetext(QString newValue);
    24. QString imagetext();
    25.  
    26.  
    27. signals:
    28. void imageDropped(QString file);
    29. void imagetextChanged(QString file);
    30.  
    31. public slots:
    32.  
    33.  
    34. protected:
    35. void dropEvent(QDropEvent *event) override;
    36. void dragEnterEvent(QDragEnterEvent *event) override;
    37. void dragMoveEvent(QDragMoveEvent *event) override;
    38. void dragLeaveEvent(QDragLeaveEvent *event) override;
    39.  
    40. private:
    41. QString m_imagetext;
    42. };
    43.  
    44. #endif // IMAGELABEL_H
    To copy to clipboard, switch view to plain text mode 

    This is the source:
    Qt Code:
    1. #include "imagelabel.h"
    2.  
    3. ImageLabel::ImageLabel(QWidget *parent) :
    4. QLabel(parent)
    5. {
    6. setAcceptDrops(true);
    7. m_imagetext="";
    8. }
    9.  
    10.  
    11. void ImageLabel::dropEvent(QDropEvent *event)
    12. {
    13. const QMimeData *mimeData = event->mimeData();
    14.  
    15. if (mimeData->hasImage()) {
    16. setPixmap(qvariant_cast<QPixmap>(mimeData->imageData()));
    17. } else if (mimeData->hasHtml()) {
    18. setText(mimeData->html());
    19. setTextFormat(Qt::RichText);
    20. } else if (mimeData->hasText()) {
    21. setText(mimeData->text());
    22. setTextFormat(Qt::PlainText);
    23.  
    24. // remove string "file:///" from mimeData->text()
    25. QString subStr = QLatin1String("file:///");
    26. QString mimeDataText = QString(mimeData->text().replace(mimeData->text().indexOf(subStr), subStr.size(), newStr));
    27.  
    28. setImagetext(mimeDataText);
    29. emit imageDropped(mimeDataText);
    30.  
    31. } else if (mimeData->hasUrls()) {
    32. QList<QUrl> urlList = mimeData->urls();
    33. QString text;
    34. for (int i = 0; i < urlList.size() && i < 32; ++i)
    35. text += urlList.at(i).path() + QLatin1Char('\n');
    36. setText(text);
    37. } else {
    38. setText(tr("Cannot display data"));
    39. }
    40.  
    41. setBackgroundRole(QPalette::Dark);
    42. event->acceptProposedAction();
    43.  
    44. }
    45.  
    46. void ImageLabel::dragEnterEvent(QDragEnterEvent *event)
    47. {
    48. setBackgroundRole(QPalette::Highlight);
    49. event->acceptProposedAction();
    50. }
    51.  
    52. void ImageLabel::dragMoveEvent(QDragMoveEvent *event)
    53. {
    54. event->acceptProposedAction();
    55. }
    56.  
    57. void ImageLabel::dragLeaveEvent(QDragLeaveEvent *event)
    58. {
    59. clear();
    60. event->accept();
    61. }
    62.  
    63. void ImageLabel::setImagetext(QString newValue)
    64. {
    65. m_imagetext = newValue;
    66.  
    67. if(!m_imagetext.isEmpty()){
    68. QPixmap pixmap(m_imagetext);
    69. pixmap = pixmap.scaled(width(), height(), Qt::KeepAspectRatio);
    70. setPixmap(pixmap);
    71. }else{
    72. clear();
    73. }
    74. emit imagetextChanged(m_imagetext);
    75. }
    76. QString ImageLabel::imagetext()
    77. {
    78. return m_imagetext;
    79. }
    To copy to clipboard, switch view to plain text mode 

    It's mapped through my custom property "Imagetext":
    Qt Code:
    1. 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:

    Qt Code:
    1. connect(ui.photo_lbl,SIGNAL(imageDropped(QString)),this, SLOT(setModelPhoto(QString)));
    2. .....................
    3. void RepairDevices::setModelPhoto(QString file)
    4. {
    5. model->setData(model->index(mapper->currentIndex(), photoIndex), file);
    6. }
    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?
    Last edited by panoss; 20th February 2017 at 15:50.

Similar Threads

  1. Replies: 4
    Last Post: 21st October 2013, 21:24
  2. Qlabel position update
    By robotics in forum Qt Programming
    Replies: 5
    Last Post: 30th May 2011, 16:08
  3. Replies: 19
    Last Post: 25th November 2010, 08:52
  4. dynamic QLabel update
    By satanowicz in forum Newbie
    Replies: 8
    Last Post: 25th May 2010, 23:20
  5. Subclassed QLabel and enterEvent
    By Lykurg in forum Qt Programming
    Replies: 2
    Last Post: 29th August 2006, 06:44

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.