Results 1 to 8 of 8

Thread: QDataWidgetMapper & QLabel

  1. #1
    Join Date
    Jul 2006
    Posts
    25
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QDataWidgetMapper & QLabel

    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:

    Qt Code:
    1. if (ui.qLnFitxerSel->text().length()>0)
    2. {
    3. QPixmap pixmap(ui.qLnFitxerSel->text(), "PNG" );
    4. QByteArray bytes;
    5. QBuffer buffer(&bytes);
    6. buffer.open(QIODevice::WriteOnly);
    7. pixmap.save(&buffer, "PNG");
    8.  
    9. qSqlTableModel->setData(q->getAKSqlTableModel()->index(row,
    10. akFrontData->getAKSqlTableModel()->fieldIndex("bandera")), bytes);
    11. }
    To copy to clipboard, switch view to plain text mode 

    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.



    Qt Code:
    1. void AKEntitat::mapFields(QSqlQueryModel * model)
    2. {
    3. QSqlRecord qSqlRecord = model->record();
    4.  
    5. int count = qSqlRecord.count();
    6.  
    7. for (int i=0; i<=count; i++)
    8. {
    9. QSqlField qSqlCamp = qSqlRecord.field(i);
    10.  
    11. akCampBd = searchAKCampBd(qSqlCamp.name());
    12. qLabel = searchLabel(qSqlCamp.name());
    13.  
    14. if (akCampBd!=0)
    15. {
    16. qDataWidgetMapper->addMapping(akCampBd->akCamp, i);
    17. }
    18.  
    19. if (qLabel!=0)
    20. {
    21. qDataWidgetMapper->addMapping(qLabel, i);
    22.  
    23. }
    24. }
    25.  
    26. qDataWidgetMapper->toFirst();
    27.  
    28. connect(akTaula->selectionModel(), SIGNAL(currentRowChanged(QModelIndex,QModelIndex)),
    29. qDataWidgetMapper, SLOT(setCurrentModelIndex(QModelIndex)));
    30.  
    31. }
    To copy to clipboard, switch view to plain text mode 

    Thank you
    Last edited by wysota; 9th November 2007 at 09:55. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QDataWidgetMapper & QLabel

    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:
    Qt Code:
    1. MyDelegate::setEditorData(QWidget *editor, const QModelIndex &index){
    2. QLabel *lab = qobject_cast<QLabel*>(editor);
    3. if(!lab || index.column()!=4){
    4. QItemDelegate::setEditorData(editor, index);
    5. return;
    6. }
    7. QImage img;
    8. img.loadFromData(index.data(Qt::ImageRole).toByteArray());
    9. QPixmap px = QPixmap::fromImage(img);
    10. lab->setPixmap(px);
    11. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jul 2006
    Posts
    25
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QDataWidgetMapper & QLabel

    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 ?

    Qt Code:
    1. AKImg::AKImg(const QString & text, QWidget * parent, Qt::WindowFlags f)
    2. : QLabel(parent, f)
    3. {
    4. QPixmap qPixmap;
    5. qPixmap.loadFromData(text.toLocal8Bit());
    6.  
    7. setWordWrap(true);
    8. setAlignment(Qt::AlignCenter);
    9. setPixmap(qPixmap);
    10. }
    11.  
    12.  
    13. AKImg::~AKImg()
    14. {
    15. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 11th November 2007 at 15:06. Reason: missing [code] tags

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QDataWidgetMapper & QLabel

    Quote Originally Posted by fpujol View Post
    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?
    J-P Nurmi

  5. #5
    Join Date
    Jul 2006
    Posts
    25
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QDataWidgetMapper & QLabel

    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.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QDataWidgetMapper & QLabel

    What do you use the widget mapper for then?

  7. #7
    Join Date
    Jul 2006
    Posts
    25
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QDataWidgetMapper & QLabel

    I use QDataWidgetMapper to get information of QSqlQueryModel and represent it in my QDialog, is faster and no need very code.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QDataWidgetMapper & QLabel

    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.

Similar Threads

  1. Problem with QLabel and setText
    By jambrek in forum Qt Programming
    Replies: 7
    Last Post: 31st October 2007, 16:02
  2. Dynamically changing QLabel background colour
    By T4ng10r in forum Qt Programming
    Replies: 19
    Last Post: 19th April 2007, 12:47
  3. QLabel links?
    By gfunk in forum Qt Programming
    Replies: 3
    Last Post: 23rd December 2006, 00:42
  4. QScrollArea display custom QLabel
    By spawnwj in forum Qt Programming
    Replies: 6
    Last Post: 6th December 2006, 03:38
  5. QT4 layout of complex dialog is very slow
    By cboles in forum Qt Programming
    Replies: 15
    Last Post: 28th April 2006, 19:57

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
  •  
Qt is a trademark of The Qt Company.