Results 1 to 20 of 20

Thread: one model for several viewings

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: one model for several viewings

    I didn't say to modify the code, but to understand it. At least one of the examples provided uses custom roles.

    I can't write the complete code for you - you won't learn this way. Besides, I have given you an almost complete solution using QStandardItemModel, what more do you need?

  2. #2
    Join Date
    Dec 2006
    Posts
    31
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    2

    Default Re: one model for several viewings

    I really can't figure it out. The deadline is coming, and I still don't get it. And it is always easier to understand something using the example. If you can, just provide the approximate code, so I could get it clear. I really need it. I'd be very grateful.
    Thank you in advance.

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

    Default Re: one model for several viewings

    What's wrong with my example a few posts earlier? Or with the one before? If you assemble them both, you get a complete solution for your problem. The first example shows you how to retrieve data from the standard item model and the last shows how to add data to the model. What more do you need? Just wrap the methods I have written into a complete class and you're ready to go.

  4. The following user says thank you to wysota for this useful post:

    someralex (15th December 2006)

  5. #4
    Join Date
    Dec 2006
    Posts
    31
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    2

    Default Re: one model for several viewings

    I have figured everything out! Thanks so much.
    I just read it through, studied the code, read the assistent and got it all clear.
    I have done the following methods:
    Qt Code:
    1. QModelIndex myModel::addFolder(const QDir &foldername){
    2. int row = rowCount(); // number of folders already in the model
    3. insertRow(row); // add a new row
    4. QModelIndex ind = index(row, 0); // index of the newly created item
    5. insertColumn(0, ind); // add a column for future children of the item
    6. setData(ind, foldername.dirName(), Qt::DisplayRole);
    7. return ind;
    8. }
    9.  
    10. QModelIndex myModel::addFile(const QModelIndex &parent, const QFileInfo &file){
    11. int row = rowCount(parent); // get number of files
    12. insertRow(row, parent); // insert new file
    13. QModelIndex ind = index(row, 0, parent); // get index
    14. setData(ind, file.completeBaseName(), Qt::DisplayRole); // insert filename in
    15. setData(ind, file.absoluteFilePath(), Qt::UserRole); // insert full path in Qt
    16. return ind;
    17. }
    18. QModelIndex myModel::addImage(const QModelIndex &parent, const QPixmap &image){
    19. setData(parent, image, Qt::UserRole+1);
    20. return parent;
    21. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void MainWindow::changeItemInLV2(const QModelIndex &myIndex){
    2. imageLabel->setText(myIndex.sibling(myIndex.row(),0).data(Qt::UserRole).toString());
    3. }
    To copy to clipboard, switch view to plain text mode 
    And how to create a method to get an image from Qt::UserRole+1 I can't understand.
    QVariant doesn't have toPixmap method.
    I'm asking for your help. again.
    Last edited by someralex; 15th December 2006 at 20:12.

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

    Default Re: one model for several viewings

    And how to create a method to get an image from Qt::UserRole+1 I can't understand.
    QVariant doesn't have toPixmap method.
    Qt Code:
    1. QPixmap pix = qvariant_cast<QPixmap>(data(index, ImageRole));
    To copy to clipboard, switch view to plain text mode 

  7. #6
    Join Date
    Dec 2006
    Posts
    31
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    2

    Default Re: one model for several viewings

    QPixmap pix = qvariant_cast<QPixmap>(data(index, ImageRole));
    Qt Code:
    1. void MainWindow::changLV1Item(const QModelIndex &fld){
    2. lv2->setRootIndex(fld);
    3. imageLabel->setPixmap(qvariant_cast<QPixmap>(fld.sibling(fld.row(),0).data(Qt::UserRole+1)));
    4. }
    To copy to clipboard, switch view to plain text mode 
    However, it doesn't return anything - QPixmap doesn't return from the model. I think the problem is in Qt::UserRole+1. In my previous post I have written how I fill the model with images. Am I doing something wrong?

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

    Default Re: one model for several viewings

    Qt Code:
    1. QModelIndex myModel::addImage(const QModelIndex &parent, const QPixmap &image){
    2. setData(parent, image, Qt::UserRole+1);
    3. return parent;
    4. }
    To copy to clipboard, switch view to plain text mode 

    Here you add the image to the "parent". Maybe you're simply using a wrong index? Try using Qt:ecorationRole instead of Qt::UserRole+1 and display the model in QTableView or QTreeView. The pixmap should be there. If it's not, then maybe the pixmap is invalid?

  9. The following user says thank you to wysota for this useful post:

    someralex (15th December 2006)

  10. #8
    Join Date
    Dec 2006
    Posts
    31
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    2

    Default Re: one model for several viewings

    Qt Code:
    1. QModelIndex myModel::addImage(const QModelIndex &parent, const QPixmap &image){
    2. setData(parent, image, Qt::DecorationRole);
    3. return parent;
    4. }
    To copy to clipboard, switch view to plain text mode 
    Qt:ecorationRole doesn't work also

    QPixmap works. The code below displays the image:
    Qt Code:
    1. imageLabel = new QLabel;
    2. imageLabel->setPixmap(QPixmap("/docs/doc_1/1.BMP"));
    To copy to clipboard, switch view to plain text mode 

  11. #9
    Join Date
    Dec 2006
    Posts
    31
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    2

    Default Re: one model for several viewings

    Everything went well! It works. I had a little mistake in my code. Qt::UseRole+1 works. I'm really grateful for your help. Many thanks.

Similar Threads

  1. Sharing Selections between Model and ProxyModel
    By mentat in forum Qt Programming
    Replies: 14
    Last Post: 27th January 2010, 18:31
  2. hierarchical model in a flat view
    By gniking in forum Qt Programming
    Replies: 4
    Last Post: 10th November 2009, 21:17
  3. Modify model data in QTreeView
    By YuriyRusinov in forum Qt Programming
    Replies: 6
    Last Post: 26th October 2006, 18:28
  4. Model sorting vs. selection
    By VlJE in forum Qt Programming
    Replies: 2
    Last Post: 25th October 2006, 17:46
  5. Table model / view editing issue
    By Caius Aérobus in forum Qt Programming
    Replies: 9
    Last Post: 7th April 2006, 12:03

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.