Results 1 to 5 of 5

Thread: Example code for QTableWidget with images

  1. #1
    Join Date
    Jan 2007
    Posts
    91
    Thanks
    21
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Example code for QTableWidget with images

    I want to have images in some of the columns of my table. I have the added icons, but they are too small. How do I scale the images to fill the cell?
    -- BRC

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Example code for QTableWidget with images

    Use QPixmap::scaled. You can choose to keep the aspect ratio.

    Regards

  3. #3
    Join Date
    Jan 2007
    Posts
    91
    Thanks
    21
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Example code for QTableWidget with images

    Thanks. That worked. Now, how do I center the image in the cell?
    - BRC

  4. #4
    Join Date
    May 2008
    Posts
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Example code for QTableWidget with images

    Hello,

    Did you find an answer to your previous question: how to center an image?
    It's exactly what I am looking for!

  5. #5
    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: Example code for QTableWidget with images

    Qt Code:
    1. #include <QtGui>
    2.  
    3. class TableWidget : public QTableWidget
    4. {
    5. protected:
    6. QStyleOptionViewItem viewOptions() const
    7. {
    8. QStyleOptionViewItem opt = QTableWidget::viewOptions();
    9. opt.decorationPosition = QStyleOptionViewItem::Bottom;
    10. opt.decorationAlignment = Qt::AlignCenter;
    11. return opt;
    12. }
    13. };
    14.  
    15. int main(int argc, char* argv[])
    16. {
    17. QApplication a(argc, argv);
    18. TableWidget t;
    19. t.setRowCount(4);
    20. t.setColumnCount(4);
    21. for (int r = 0; r < t.rowCount(); ++r)
    22. {
    23. for (int c = 0; c < t.columnCount(); ++c)
    24. {
    25. item->setIcon(QIcon(":/trolltech/styles/commonstyle/images/left-128.png"));
    26. t.setItem(r, c, item);
    27. }
    28. }
    29. t.show();
    30. return a.exec();
    31. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

Similar Threads

  1. Pasting code from code tag in emacs
    By Gopala Krishna in forum General Discussion
    Replies: 0
    Last Post: 16th February 2007, 05:47
  2. Qt Cryptographic Architecture
    By vermarajeev in forum Qt Programming
    Replies: 6
    Last Post: 9th February 2007, 13:15
  3. Problem closing a QMainWindow in Qt4.2
    By ian in forum Qt Programming
    Replies: 11
    Last Post: 17th October 2006, 00:49
  4. problem with linking
    By mickey in forum Qt Programming
    Replies: 49
    Last Post: 12th August 2006, 21:41

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.