Results 1 to 8 of 8

Thread: setPixmap and Paint Event

  1. #1
    Join Date
    Oct 2009
    Posts
    9
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default setPixmap and Paint Event

    Hi all,

    I´m trying to do some simple Qt experiments... like load and manipulate pixmaps.

    Why this piece of code does not work?

    Qt Code:
    1. image = QImage(256, 256, QImage::Format_RGB32);
    2. QRgb value;
    3. value = qRgb(189, 149, 39); // 0xffbd9527
    4. image.setPixel(1, 1, value);
    5. value = qRgb(122, 163, 39); // 0xff7aa327
    6. image.setPixel(0, 1, value);
    7. image.setPixel(1, 0, value);
    8. value = qRgb(237, 187, 51); // 0xffedba31
    9. image.setPixel(2, 1, value);
    10. pixmap.fromImage(image,Qt::ColorOnly);
    11. ui.label->setPixmap(pixmap);
    To copy to clipboard, switch view to plain text mode 

    As you can see, is a part of one example from Qt Image documentation. This code is placed in a slot, and it is connected to a button (or not, it is not important).
    I only want to load and show a pixmap when the button has been pressed, at least as a first approx.
    Now i´m reading something related to paint Events, and maybe this is the way to manipulate pixmap, but i have no much time.

    Thanks

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: setPixmap and Paint Event

    Why this piece of code does not work?
    What do you mean by "not work"?
    What do you see?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Oct 2009
    Posts
    9
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: setPixmap and Paint Event

    Thanks for your answer,

    When the button is pressed, it seems that nothings occurs. However, if i load a bmp file, or jpg.. the file is loaded and shown when i signal the slot.

    I think that i didn´t understand anything about Qimage, and maybe i need to read more about Qpainter?

    Sorry about my english, but I ´ll try to explain what is supposed I want to do:

    I need to show in a user interface several data patterns. These patterns are 2-Dim arrays of 2 bytes words. Like a PAL video frame, more or less. First of all, i want to show these patterns and make some changes in them, pixel by pixel. (like a inversion operation).
    Also, these 16bits words need to be converted to an apropiate color, something like first possible value (0x0000) will be a dark blue color, and the last (0xFFFF) will be dark red.

    What is the best way to do this?

    Thanks again

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: setPixmap and Paint Event

    When the button is pressed, it seems that nothings occurs.
    can you show the connect() line you are using to connect the button to the slot?
    Also, when you start the program from a console or a debugger, look at the output, see if any signal/slot connection warnings come up.
    Once we solved the signal/slot connection we can look in to the rest.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Oct 2009
    Posts
    9
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: setPixmap and Paint Event

    Its a simple (i thing) connect:

    Qt Code:
    1. connect (ui.paintButton, SIGNAL (clicked()), this, SLOT (initPixmap()));
    To copy to clipboard, switch view to plain text mode 
    .

    Interface has been created with Qt Designer, so "ui" represents that object.
    initPixmap() function contains the code I posted before.

    Same line works if i load a bmp file:

    Qt Code:
    1. pixmap.load("xxxx.bmp",const char *,Qt::Flags);
    2. ui.label->setPixmap (pixmap);
    To copy to clipboard, switch view to plain text mode 

    , and there aren´t console message indicating some like "No such slot/signal...."

    I think i´m missing some important concept about Qt graphics framework.

    thanks again

  6. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: setPixmap and Paint Event

    Same line works if i load a bmp file:
    So if you put these two lines in the slot instead of the QImage conversion code, you get an image when the button is clicked?

    I see no problems in the code you posted so far.

    I think i´m missing some important concept about Qt graphics framework.
    You are not doing any painting your self, so it plays no role, at least for the code you posted.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  7. #7
    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: setPixmap and Paint Event

    The problem is the following line:
    Qt Code:
    1. pixmap.fromImage(image,Qt::ColorOnly);
    To copy to clipboard, switch view to plain text mode 
    It should be:
    Qt Code:
    1. pixmap = QPixmap::fromImage(image,Qt::ColorOnly);
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: setPixmap and Paint Event

    I fall in that pit every time! :-)
    Well spotted!
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. Timer event & paint event, priority
    By Teuniz in forum Qt Programming
    Replies: 0
    Last Post: 2nd February 2010, 13:33
  2. Paint event function in key press event
    By soumya in forum Qt Programming
    Replies: 6
    Last Post: 2nd February 2010, 12:40
  3. Reimplementig paint event
    By ale301168 in forum Newbie
    Replies: 4
    Last Post: 13th November 2009, 11:11
  4. QGraphicsTextItem and paint event
    By prashant in forum Qt Programming
    Replies: 0
    Last Post: 6th November 2009, 07:52
  5. Drag the paint event
    By jsmith in forum Qt Programming
    Replies: 2
    Last Post: 22nd July 2009, 14:51

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.