Results 1 to 4 of 4

Thread: QGraphicsPixmapItem as an array?

  1. #1
    Join Date
    Mar 2014
    Posts
    27
    Thanks
    9
    Qt products
    Qt5
    Platforms
    Windows

    Default QGraphicsPixmapItem as an array?

    Hi, I need to save images in an array, but I tried some things, but still not working, need help.
    I'm trying to use and set values to a QGraphicsPixmapItem Array (using C++ arrays) and using QList too.

    My code here:
    Qt Code:
    1. QGraphicsPixmapItem pixmapArray[n*m];
    2. for(int i=0;i<n;i++){
    3. for(int j=0;j<m;j++){
    4. QPixmap *icon = new QPixmap(list.at(biDiArray[i][j])); //list of a QString
    5. image.setPixmap(*icon);
    6. pixmapArray[i*10+j] = image;
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 

    I tried to use it as QList too, but didn't work.

    Qt Code:
    1. for(int i=0;i<this->n;i++){
    2. for(int j=0;j<this->m;j++){
    3. QPixmap *icon = new QPixmap(list.at(mat[i][j]));
    4. this->pixmapItemList.insert(10*i+j,*image);
    5. }
    6. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsPixmapItem as an array?

    If you have an array of items, then you need to call setPixmap on those items, not on local instances.

    If you want to create items in the loop, then use an array or list of the item pointer type.

    Cheers,
    _

    P.S.: don't create QPixmap on the heap unless you really need to, currently you are leaking memory.

  3. #3
    Join Date
    Mar 2014
    Posts
    27
    Thanks
    9
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QGraphicsPixmapItem as an array?

    Quote Originally Posted by anda_skoa View Post
    If you have an array of items, then you need to call setPixmap on those items, not on local instances.

    If you want to create items in the loop, then use an array or list of the item pointer type.

    Cheers,
    _

    P.S.: don't create QPixmap on the heap unless you really need to, currently you are leaking memory.
    Thanks it works, by the way, in your last sentence, any suggestions? I really need to do that, but don't know other way/

    I figure it out, sorry, I used:
    pixmapArray[i*10+j].setPixmap(QPixmap(list.at(mat[i][j])));

    its ok?

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsPixmapItem as an array?

    Yes, that's ok.

    QPixmap is a "value" it can be copied when needed. So the code you had earlier would have worked without "new":
    Qt Code:
    1. QPixmap icon = QPixmap(list.at(mat[i][j]));
    To copy to clipboard, switch view to plain text mode 

    The constructor of QGraphicsPixmapItem takes the QPixmap as "const QPixmap&", i.e. as a constant reference. This is a common indicator that the thing will either be only used locally in the function (here constructor) or copied.

    Cheers,
    _

  5. The following user says thank you to anda_skoa for this useful post:

    vitaR (14th April 2014)

Similar Threads

  1. QGraphicsPixmapItem
    By erfan in forum Qt Programming
    Replies: 1
    Last Post: 13th June 2012, 22:54
  2. Cast QString array to std::string array
    By Ishtar in forum Newbie
    Replies: 4
    Last Post: 15th July 2011, 09:28
  3. Replies: 2
    Last Post: 12th November 2010, 15:42
  4. declare an array of QSemaphore and array of slot functions
    By radeberger in forum Qt Programming
    Replies: 11
    Last Post: 2nd May 2010, 14:24
  5. Shape of QGraphicsPixmapItem
    By StefanHirche in forum Newbie
    Replies: 4
    Last Post: 5th September 2007, 16:14

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.