Results 1 to 6 of 6

Thread: Subclassed QLabel won't update the model

  1. #1
    Join Date
    Feb 2017
    Posts
    17
    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.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,689
    Thanks
    257
    Thanked 750 Times in 740 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default 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.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Feb 2017
    Posts
    17
    Thanks
    5
    Qt products
    Qt5

    Default Re: Subclassed QLabel won't update the model

    Quote Originally Posted by d_stranz View Post
    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.

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,689
    Thanks
    257
    Thanked 750 Times in 740 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Subclassed QLabel won't update the model

    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.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  5. The following user says thank you to d_stranz for this useful post:

    panoss (21st February 2017)

  6. #5
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Subclassed QLabel won't update the model

    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  7. The following 2 users say thank you to Santosh Reddy for this useful post:

    d_stranz (21st February 2017), panoss (21st February 2017)

  8. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,689
    Thanks
    257
    Thanked 750 Times in 740 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default 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.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

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.