Results 1 to 6 of 6

Thread: Create pixmap image from string

  1. #1
    Join Date
    Feb 2006
    Posts
    209
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Create pixmap image from string

    Hi.
    I have a string and would like to create a QPixmap from the string. Each letter should correspond to a specific colour.
    Each letter in the string should be turned into a 20x20 rect filled with the colour.
    So a chess board could be created from the string bwbwbwbwbwbwbw...
    Now, how do I do this?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Create pixmap image from string

    Just create a pixmap of the right size and use QPainter:
    Qt Code:
    1. QPainter p( &pixmap );
    2. QRect rect(...);
    3. for each letter:
    4. rect.moveTo( ... );
    5. p.fillRect( rect, ... );
    6. p.end();
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to jacek for this useful post:

    Morea (17th November 2006)

  4. #3
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Create pixmap image from string

    Hi,
    Are you sure taht you can use QPixmap? You only can create the image with XPM format.

    If you use QImage you would be able to acces directly to the image pointer stored in uchar* by using "bits()". If the image would be a RGB you have to define that the QImage will be a RGB image.

    Then you could read the string. For every character you could take the letter and use the proper RGB value (use the macro QRbg(int,int,int)). For example:

    You read "b"(black), so the color is QRgb(255,255,255).
    You read "w"(white), so the color is QRgb(0,0,0).
    I don't remeber if I inverted the values. Black = 0,0,0 and Withe = 255,255,255 ? Try it.

    Now, you have wich color is the character that you readed, you have to insert it 20 times in the "bits()" pointer. It is a pointer to an array, so you could index it like "data[i]".

    You have to create the image with a width*20 and height*20.

    It probably be more easy to program if you index the image with setPixel that you are able to index the 20*20 rectangle putting the QRgb value.
    Òscar Llarch i Galán

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Create pixmap image from string

    Quote Originally Posted by ^NyAw^ View Post
    Are you sure taht you can use QPixmap?
    Yes, I'm sure.

    Quote Originally Posted by ^NyAw^ View Post
    You only can create the image with XPM format.
    No, that's only one out of 6 QPixmap constructors. You can always create an empty pixmap of given size and paint on it using QPainter.

  6. #5
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Create pixmap image from string

    Ups, sorry for confusing.

    The QPainter solution is easyier.
    Òscar Llarch i Galán

  7. #6
    Join Date
    Oct 2006
    Posts
    279
    Thanks
    6
    Thanked 40 Times in 39 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Create pixmap image from string

    I would also suggest working with a QImage, which allows pixel-precise manipulation. You can always convert it to a QPixmap later.
    i.e.
    Qt Code:
    1. QImage image(20,20, QImage::Format_ARGB32);
    2. for(int i=0;i<400;i++)
    3. {
    4. QRgb rgb = string[i]=='b' ? qRgb(0,0,0) : qRgb(255,255,255);
    5. image.setPixel(i%20, i/20, rgb);
    6. }
    7. QPixmap pixmap = QPixmap::fromImage(image);
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Fast image drawing/scaling in Qt 3.3
    By eriwik in forum Qt Programming
    Replies: 1
    Last Post: 21st June 2006, 11:45
  2. problem with the back ground image
    By Seema Rao in forum Qt Programming
    Replies: 1
    Last Post: 17th April 2006, 22:34
  3. How and when to repaint a widget ?
    By yellowmat in forum Newbie
    Replies: 7
    Last Post: 3rd April 2006, 17:36
  4. Question about updating an image on screen
    By SkripT in forum Qt Programming
    Replies: 1
    Last Post: 24th February 2006, 20:01
  5. How to paint a selection rectangle on a pixmap?
    By SkripT in forum Qt Programming
    Replies: 6
    Last Post: 8th January 2006, 20:52

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.