Results 1 to 9 of 9

Thread: not able to load images to qtablewidget

  1. #1
    Join Date
    Nov 2010
    Posts
    29
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Question not able to load images to qtablewidget

    dear all,

    i have couple of questions

    1)i have simple program tat loads icons form "images" directory(located in current directory) to 3X3 qtablewidget. code is given below. but when i run the program i cant see any icons on qtablewidget.

    2) how do i set the iconsize


    for (int r = 0; r < t.rowCount(); ++r)
    {
    for (int c = 0; c < t.columnCount(); ++c)
    {
    QTableWidgetItem* item = new QTableWidgetItem;
    item->setBackgroundColor(Qt::black);
    item->setIcon(QIcon(QPixmap(":/images/*.png")));
    t.setItem(r, c, item);
    }
    }

    i have included the snapshot of output i get to make things much clear

    qt version= 4.7.0

    thanks

    regards
    rashmi
    Attached Images Attached Images

  2. #2
    Join Date
    Feb 2010
    Location
    Wokingham, United Kingdom
    Posts
    36
    Thanks
    7
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: not able to load images to qtablewidget

    try omitting the QPixmap() call you don't need it
    eg:
    Qt Code:
    1. item->setIcon(QIcon(":/images/*.png"));
    To copy to clipboard, switch view to plain text mode 
    also you can use the resource browser in designer to make sure your resource path is correct.
    I'm assuming that the "*" wildcard is just for the question and not hardwired into your code as this would also cause a problem...

    hope that helps

  3. #3
    Join Date
    Nov 2010
    Posts
    29
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: not able to load images to qtablewidget

    item->setIcon(QIcon(":/images/*.png"));

    i even tried this but still output remains same.

    images directory has 9 images with .png extension i tried loading one particular image example say
    item->setIcon(QIcon(":/images/img4.png"));

    actually i wanted other way round i wanted all 9 images to fit in to 3X3 table mean to say one image per cell.

    can you suggest me where i am going wrong.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: not able to load images to qtablewidget

    As janorcutt alluded to, ":/images/*.png" is not the name of an image file so you end up with a null QIcon. You want to load a single image file into each cell. Assuming your files are named img0.png through img8.png, then:
    Qt Code:
    1. for (int r = 0; r < t.rowCount(); ++r)
    2. {
    3. for (int c = 0; c < t.columnCount(); ++c)
    4. {
    5. item->setBackgroundColor(Qt::black);
    6. item->setIcon(
    7. QString(":/images/img%1.png").arg(r * t.columnCount() + c)
    8. )
    9. );
    10. t.setItem(r, c, item);
    11. }
    12. }
    To copy to clipboard, switch view to plain text mode 
    should be closer to what you thought you were doing.

    BTW: Your initial screen shot shows a 4x3 grid not a 3x3 grid

  5. #5
    Join Date
    Nov 2010
    Posts
    29
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Exclamation Re: not able to load images to qtablewidget

    hey ChrisW67,

    firstly i would like to thank you for your help. and i am sorry as i had mentioned 3X3 instead of 3X4.
    i tried doing what you had suggested. but still not able to load images.
    i am posting my complete code could you please tell me where i am going wrong. i am not able to make out what is going wrong.

    i wanted to mention one more thing is that icon size is 160X110

    here is my code. if i am wrong please correct me.
    -----------------------------------------------------------------------------------------------------------------
    Qt Code:
    1. #include <QApplication>
    2. #include <QWidget>
    3. #include <QTableWidget>
    4. #include <QHeaderView>
    5. #include <QString>
    6.  
    7.  
    8. class TableWidget : public QTableWidget
    9. {
    10.  
    11. };
    12.  
    13. int main(int argc, char *argv[])
    14. {
    15. QApplication app(argc, argv);
    16.  
    17. //instance of table
    18.  
    19. TableWidget t;
    20. // specifies size of tablewidget (window size)
    21.  
    22. int w=640,h=480;
    23. // specifies column and row count
    24.  
    25. int row=3,col=4;
    26.  
    27. //specifies each cell size of table
    28.  
    29. int cellr,cellc;
    30.  
    31. t.resize(w,h); //setting window size to width and height specified by user
    32.  
    33. //sets background color
    34.  
    35. QPalette palet;
    36. palet.setColor( t.backgroundRole(), Qt::black );
    37. t.setPalette(palet);
    38.  
    39. //sets selection color i.e, changes color when item is focused.
    40.  
    41. t.setStyleSheet("selection-background-color:cyan");
    42.  
    43. //hides headers
    44.  
    45. t.verticalHeader()->hide();
    46. t.horizontalHeader()->hide();
    47.  
    48.  
    49. //calculating cell size based on number of row and column using width and height
    50.  
    51. cellr=h/row;
    52. cellc=w/col;
    53.  
    54.  
    55. t.verticalHeader()->setDefaultSectionSize(cellr);
    56. t.horizontalHeader()->setDefaultSectionSize(cellc);
    57.  
    58. // set the resize mode to fixed, so the user cannot change the height/width
    59.  
    60. t.horizontalHeader()->setResizeMode(QHeaderView::Fixed);
    61. t.verticalHeader()->setResizeMode(QHeaderView::Fixed);
    62.  
    63.  
    64. t.setRowCount(row);
    65. t.setColumnCount(col);
    66.  
    67. //sets focus policy;
    68.  
    69. t.setFocusPolicy(Qt::StrongFocus);
    70.  
    71.  
    72.  
    73. for (int r = 0; r < t.rowCount(); ++r)
    74. {
    75. for (int c = 0; c < t.columnCount(); ++c)
    76. {
    77. item->setBackgroundColor(Qt::black);
    78. item->setIcon(QIcon(QString(":/images/img%1.png").arg(r * t.columnCount() + c)));
    79. t.setItem(r, c, item);
    80. }
    81. }
    82. t.show();
    83. return app.exec();
    84. }
    To copy to clipboard, switch view to plain text mode 
    ---------------------------------------------------------------------------------------------------------------

    thanks for help.
    Last edited by rashmi; 3rd December 2010 at 05:28. Reason: changed [qtclass] to [code]

  6. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: not able to load images to qtablewidget

    What doesn't work? The icons load and display in the cells fine here.

    The icons are displayed at a fixed size regardless of their original size if you use the default item delegate. If you want an alternate behaviour then you will need to supply a QStyledItemDelegate subclass of your own.

  7. #7
    Join Date
    Nov 2010
    Posts
    29
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: not able to load images to qtablewidget

    i dont see icons in the output. i can just see 3X4 table with empty cell (black background) as i have included snapshot of output i get in my previous post.

    i really cant make out what is going wrong that is the reason i posted my complete code.

    any suggestions. please help me.

  8. #8
    Join Date
    Nov 2010
    Posts
    29
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Smile Re: not able to load images to qtablewidget

    can any one tell me how do i load images to qtablewidget from resource file.
    as i have already mentioned in previous post loading images from directory(code is given in previuos post) is working. so want to try loading from resource to see if that makes difference.


    Added after 1 39 minutes:


    hey ChrisW67,

    thanks alot for your help. i could display icons in window. but icons look really very small in display how do i increase the size of icons to fit cell size. i have included the snapshot have look.

    thank you.

    please help.
    Attached Images Attached Images
    Last edited by rashmi; 3rd December 2010 at 12:06.

  9. #9
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: not able to load images to qtablewidget

    You will need to write a custom delegate and attach it to the QTableWidget. The default item delegate renders the item icon as a the small icon you see here and leaves space for the main item content (the Qt::DisplayRole) and optionally the item checkbox as well. Take a look at QStyledItemDelegate and associated examples. You want to provide an alternate paint() method when a valid icon is present.

Similar Threads

  1. Load images in a sequence in QML
    By anupama in forum Qt Quick
    Replies: 1
    Last Post: 1st December 2010, 08:24
  2. Replies: 1
    Last Post: 16th November 2010, 17:45
  3. Three images to load and set transparency set for each
    By augusbas in forum Qt Programming
    Replies: 0
    Last Post: 29th October 2010, 11:42
  4. Load Images in a dir into a QScrollArea
    By nmuntz in forum Qt Programming
    Replies: 3
    Last Post: 20th January 2009, 21:03
  5. WekKit won't load JPEG images.
    By andrem in forum Qt Programming
    Replies: 4
    Last Post: 10th December 2008, 00:24

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.