Results 1 to 15 of 15

Thread: update grid of pixmap labels based on 2d char arrray value

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #2
    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: update grid of pixmap labels based on 2d char arrray value

    One approach would to use the grid layout that contains the (anonymous) widgets to allow access by row and column. This makes mapping from underlying 2D array to widget trivial. For example:
    Qt Code:
    1. #include <QtGui>
    2. #include <QDebug>
    3.  
    4. class MainWindow: public QMainWindow {
    5. Q_OBJECT
    6. public:
    7. MainWindow(QWidget *p = 0):
    8. m_blankPixmap("blank.png"),
    9. m_otherPixmap("other.png")
    10. {
    11. QWidget *central = new QWidget(this);
    12.  
    13. m_layout = new QGridLayout(central);
    14. for(int r = 0; r < 10; ++r) {
    15. for(int c = 0; c < 10; ++c) {
    16. QLabel *label = new QLabel(central);
    17. label->setPixmap(m_blankPixmap);
    18. m_layout->addWidget(label, r, c, Qt::AlignCenter);
    19. }
    20. }
    21.  
    22. central->setLayout(m_layout);
    23. setCentralWidget(central);
    24.  
    25. setCellPixmap(3, 3, m_otherPixmap);
    26. }
    27.  
    28. void setCellPixmap(int row, int col, const QPixmap &pixmap) {
    29. QLabel *label = qobject_cast<QLabel*>( m_layout->itemAtPosition(row, col)->widget() );
    30. if (label) {
    31. label->setPixmap(pixmap);
    32. }
    33. }
    34. private:
    35. QGridLayout *m_layout;
    36. const QPixmap m_blankPixmap;
    37. const QPixmap m_otherPixmap;
    38. };
    39.  
    40. int main(int argc, char *argv[])
    41. {
    42. QApplication app(argc, argv);
    43.  
    44. MainWindow m;
    45. m.show();
    46. return app.exec();
    47. }
    48. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

  2. The following user says thank you to ChrisW67 for this useful post:

    wuts.the.martyr (6th December 2011)

Similar Threads

  1. Creating a pixmap from unsigned char*
    By winder in forum Qt Programming
    Replies: 9
    Last Post: 6th July 2021, 13:56
  2. Replies: 8
    Last Post: 5th May 2021, 16:41
  3. Replies: 1
    Last Post: 19th April 2011, 11:17
  4. Replies: 0
    Last Post: 14th October 2010, 18:45
  5. Rapid Update of Pixmap
    By dbrmik in forum Qt Programming
    Replies: 5
    Last Post: 23rd April 2009, 10:49

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.