Results 1 to 10 of 10

Thread: Image display using QT

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1

    Default Re: Image display using QT

    Hello wysota,

    Thank you very much for the quick help.

    So can I do my task just with a view or on MainWindow directly with "QPainter".

    Can you please suggest me the right way for my task.

    That is at least the road map.

    Thanks in advance
    ankireddy.n

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,372
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Image display using QT

    Quote Originally Posted by ankireddy View Post
    So can I do my task just with a view or on MainWindow directly with "QPainter".
    Yes. Well... you'll need some kind of widget of course besized the main window for the central widget.

    Can you please suggest me the right way for my task.
    The base is to render your pixmap in the paint event. If you want to draw something over it then do it after rendering the pixmap. Store everything you need for rendering in member variables of the class and use it in the paint event. So basically you do your logic in your custom methods (probably slots) and call update() to trigger a paint event that will redraw everything.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3

    Default Re: Image display using QT

    Hello wysota,

    Thanks a lot for the help.

    With your help i made an example. I am drawing image in an Widget and mainwindow for slots.

    Now I have a problem.

    I am painting on the image. For this I implemented "mouseMoveEvent". Here I am changing the color of the image pixel at x, y location(getting from mouseMoveEvent) and calling update().

    If the image size is small say 100X100 the things are OK. If the image size is more say 1828X778 then only a few points are setting color. All pixels along the mouse move path are not getting painted.

    I observed this is because of calling "paintEvent" for every pixel. And paint event has the statement ( "QPixmap::fromImage(image) and painter.drawPixmap()") to display image.

    This only for the cursor of size 1 pixel width. If the cursor size say 50 (50X50 rectangle in shape ) then it may not paint all pixels

    I found two solutions for this problem

    1) In paintEvent instead of calling "fromImage" method, just I am painting that x,y coordinate of the device with "painter.drawPoint()" method . But in this case as the pixmap has the old data and my drawing is not appear on the screen.

    The code is as follows
    mouseMoveEvent(QMouseEvent *event)
    {
    int x=mouseEvent->x();
    int y=mouseEvent->y();
    end.setX(x);
    end.setY(y);
    QRgb color=qRgb(255,0,0);
    penColor=color;
    image.setPixel(x,y,color);
    getting=true;
    updateImage=false;
    update();
    }

    PaintEvent

    QPainter paint(this);
    if(readImage) //Do only after read image
    {
    if(updateImage)
    {
    pxmap=QPixmap::fromImage(image);
    paint.drawPixmap(0,0,pxmap.width(),pxmap.height(), pxmap);
    }
    else
    {
    paint.setPen(penColor);
    paint.drawPoint(end.x(),end.y());
    paint.drawPixmap(0,0,pxmap.width(),pxmap.height(), pxmap);
    }
    }

    2) After loading image, I scaled it to half of the image size and displaying the image for each and every pixel. In this case also if the image is zoom-in then again i am having the same problem

    Another way is
    A) pxmap=QPixmap::fromImage(image);
    B) painter.drawPixmap(....);
    D) QPainter painter(this);
    E) then for every pixel update the pixel color only on device with out doing A and B steps. That is get QPainter painter(this) and painter.setPixel(x,y,color).

    But I did not found any method like QPixmap::setPixel() which will update the device in such a way.

    Can you please guide me how to solve this problem?

    Thanks in advance
    ankireddy.n
    Attached Files Attached Files

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,372
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Image display using QT

    What is the purpose of this updateImage variable?

    In general it should all be more or less like this:

    Qt Code:
    1. void X::mousePressEvent(QMouseEvent *me) {
    2. m_pt = me->pos();
    3. }
    4.  
    5. void X::mouseReleaseEvent(QMouseEvent *me) {
    6. addLineToPixmap(m_pt, me->pos());
    7. m_pt = QPoint();
    8. }
    9.  
    10. void X::mouseMoveEvent(QMouseEvent *me) {
    11. addLineToPixmap(m_pt, me->pos());
    12. m_pt = me->pos();
    13. }
    14.  
    15. void X::addLineToPixmap(QPoint from, QPoint to) {
    16. QPainter p(&m_pixmap);
    17. p.drawLine(from, to);
    18. update();
    19. }
    20.  
    21. void X::paintEvent(...) {
    22. QPainter p(this);
    23. p.drawPixmap(m_pixmap);
    24. }
    25.  
    26. private:
    27. QPoint m_pt;
    28. QPixmap m_pixmap;
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5

    Default Re: Image display using QT

    Hello wysota,

    Thank you very much for your great help.

    I did it.

    Thanks
    ankireddy.n

  6. #6
    Join Date
    Jul 2013
    Posts
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Image display using QT

    hi
    i am new to Qt i want to display the uploaded image in the form2 .
    i have created 2forms , where one form i have included all the fields like line edit , text edit etc and i have uploaded the image. the problem i am facing is i am unable to display the data of form 1 to form 2, and please help in displaying the image which is uploaded in form 1 to form 2.

    u can find the attachements.

    where regform is my main form(form1)
    and message is my 2nd form(form 2)

    Can you please guide me how to start Qt project which satisfies the above needs.

    thankuregform.cppregform.hmessage.cppmessage.hmain.cpp

Similar Threads

  1. Qt Image Display...
    By mahe2310 in forum Qt Programming
    Replies: 9
    Last Post: 22nd July 2013, 09:06
  2. How to display .svg image in Qt
    By GT Development in forum Qt Programming
    Replies: 4
    Last Post: 28th September 2011, 06:17
  3. Replies: 1
    Last Post: 24th August 2011, 14:09
  4. How to Display the image in Qt
    By sdastagirmca in forum Qt Programming
    Replies: 1
    Last Post: 10th February 2009, 06:17
  5. Display image
    By Pesho in forum Qt Programming
    Replies: 1
    Last Post: 14th September 2007, 00:21

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
  •  
Qt is a trademark of The Qt Company.