Results 1 to 5 of 5

Thread: Reading of ascii-file to QPixMap

  1. #1
    Join Date
    Oct 2007
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question Reading of ascii-file to QPixMap

    Hi, I have a set of textfiles (ascii) that contains values. Each value represents a pixel that I somehow have to draw to screen, and each file contains thousands of lines which together represents an image. An example of a file:

    Qt Code:
    1. x y value
    2. ...
    3. 0.35040E+01 0.29280E+01 0.30385E+01
    4. 0.35040E+01 0.29760E+01 0.31362E+01
    5. 0.35040E+01 0.30240E+01 0.32857E+01
    6. //empty line that separates x-values
    7. 0.35520E+01 0.27840E+01 0.31625E+01
    8. 0.35520E+01 0.28320E+01 0.31206E+01
    9. 0.35520E+01 0.28800E+01 0.30607E+01
    10. ...
    To copy to clipboard, switch view to plain text mode 
    My question is, what would be the best approach to read a file of this type into a QPixMap (which then will be drawn to a QGraphicsView)? Or, should i preferably use something else than QPixMap?

    The x/y-values aren`t that important, the main point is that each value corresponds to a pixel. I have thought of reading them into an array, but I am quite uncertain of how I should use this array further.
    Last edited by fluxar; 5th October 2007 at 13:31.

  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: Reading of ascii-file to QPixMap

    If that's an image, then you should use QImage, which allows you to change particular pixels. You can use QTextStream to read data.

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

    fluxar (23rd October 2007)

  4. #3
    Join Date
    Oct 2007
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Reading of ascii-file to QPixMap

    I`ve managed to display the data(arrays of floats 100x100) as images(QImage) now, but I have some trouble with applying colors to the images in an appropriate way. Basically, each float corresponds to a color(pixel) in an image, so I have to map each float to a color and then apply this color to a specific pixel.

    I was thinking about using QLinearGradient as a lookup-table for colors, but it doesnt look like it suits my needs. Maybe I need to make my own, but I want to if anyone has any input before I do so.

    What I need is:
    A linear-gradient/lookup-table where I can add colors (between range A to B) and then get the right color by writing something like QRgb color = gradient.getColor(floatdata[x][y]).

    So what do you think, do I have to write my own gradient class, or is there some other approach I could use?

  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: Reading of ascii-file to QPixMap

    The linear gradient doesn't do any special magic. You only need this formula:

    x = s*x_a + (1-s)*x_b

    and apply it to every colour component. For s = 0 you'll get colour "a" and for s = 1 --- "b". Although such tranformation might not be linear (for example if you change s from 0.2 to 0.3 the perceptual change of colour might be different than the one when you change s from 0.7 to 0.8), but you can avoid this by using an uniform colour space.

    If you want a colour scale that goes from red to green, you can try this:
    Qt Code:
    1. qreal redHue = QColor( Qt::red ).hueF();
    2. qreal greenHue = QColor( Qt::green ).hueF();
    3. pixelColour = QColor::fromHsvF( redHue + (value / maxValue) * (greenHue - redHue), 1.0, 1.0 );
    To copy to clipboard, switch view to plain text mode 

    Also take a look at Qwt's Spectrogram plots.

  6. #5
    Join Date
    Oct 2007
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Reading of ascii-file to QPixMap

    Thanks, Qwt looks interesting and I might use it at later point. A spectrogram plot is basically what I have made now, but mine version is "lighter".

    I have a lot of these plots, each represented as a QGraphicsItem. These items are added to a scene.

    My problem now is that I need to click at some position within an item and display the value at this point for each of the items. So, for example if I click at the position (1,1) (in item coordinates), the value at this point is displayed at the bottom of the item. Then I somehow have to notify all the other items about the "clicked-position" (in item coordinates, since they all have the same size) so that they also can display their value at this specific point.

    I have managed to make this work only for a single item, (only the value within a single item is displayed). I did this by reimplementing the item's mousePressedEvent(...).

    My question is:
    If I click at an item, what is the best approach to send the clicked-position (in itemcoordinates) to all of the items in the scene?
    I have thought of reimplementing the scene's mousePressedEvent(..), but I'm not sure if it will work.
    Last edited by fluxar; 15th November 2007 at 15:55.

Similar Threads

  1. QWT 5, QT3, SuSE 10.2. Crash and burn
    By DrMcCleod in forum Qwt
    Replies: 8
    Last Post: 7th September 2007, 20:53
  2. reading from a file
    By mickey in forum General Programming
    Replies: 32
    Last Post: 19th July 2007, 01:04
  3. Replies: 4
    Last Post: 10th July 2007, 20:11
  4. qt-3.3.8 fail in scratchbox
    By nass in forum Installation and Deployment
    Replies: 0
    Last Post: 25th May 2007, 15:21
  5. Problem with reading a file
    By Buhmann in forum Qt Programming
    Replies: 11
    Last Post: 17th February 2006, 13:02

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.