Results 1 to 10 of 10

Thread: Image display using QT

  1. #1

    Default Image display using QT

    Hello Exports,

    I am new to QT programming. I want to develop a small image processing application using QT.

    My application displayes animage on screen and need to do some modifications on image pixels. So I need to do some painting (king of masking) on the image (Selecting a region on image with mouse like lines, rectangles ect., ). For example, if I draw a rectangle on the image then I want to modifiy the pixels of the image in that rectangle.

    I gone through some of the QT meterial and examples. But I am not able to find the starting point where to start. That is whether should I use GrapicsView or only with QMainWindow.

    I saw an example which is using ui::View to display image. But i did not get any help in Qt meterial about Ui.

    Can I use GraphicsView in my applications for drawing and to display images. Or should I use some other?

    I am planning to use ImageMagick because it will support > 100 image formates.

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

    Thanks in Advance
    Wizards.

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

    Default Re: Image display using QT

    You can use Graphics View but you don't have to. If you are operating pixel by pixel, it is probably better not to use it. If, instead, you wanted to apply a kind of "filters" on the image (like Photoshop (or GIMP with layers) does), it is likely that using Graphics View would be a better idea to have a object-by-object semantics.
    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,

    Thank you very much for the help.

    Now I made a small example to show what I want to do. To do this I followed "Diagram Scene Example" of QGraphicsView.

    I overwrite the "QGraphicsScene" class as "gView" to do my image processing related tasks and drawing related tasks. I will do all image processing tasks in this class and I will use "MainWindow" to transfer signals. (like Setting line color, text size ect in "Diagram Scene Example").

    I loaded image into the graphics view in "MainWindow" constructor (scene->openSceneImage("").

    After I displayed image on the screen then my work will start. I will select an area of the image with the mouse (say the face on the logo). Now I want to change the color of these selected pixels of the image and then I should display the image on the screen with the changed pixels.

    In this attached example in "gView" class, in button pressed event I took the start and last points in mouse pressed and released events. Now I should draw a rectangle on the image from start point to last point (with the mouse dragging) and I should change all the pixels to green. I did this on "mouseReleaseEvent" in "gView" class and displayed the image on screen with changed (green) pixels.

    (I took start and last points as static because I am unable to map co-ordinates from Scene to GrapicsView).

    My application need to do lot of mouse clicks on the screen (image). Can i add image (like pix=QPixmap::fromImage(image);addPixmap( pix ) to Scene every time when I click mouse like as I did in mouse release event. If this is wrong how can I display image every time with the effected pixels.

    I choose QGraphicsView because some times I need to show the drowned shapes (like lines, rectangles, circles etc.,) over the image (To show the user, the area which he selected).

    I tried with "QPainter" class but it is not working everywhere except in "paintEvent" (I want to run my application in all platforms ). I think it is a bit difficult to show shapes with image on the screen for every .

    (My task is like quick masking tool in Photoshop).

    Can you please help me whether my approach is right or wrong for my task.

    Thanks in Advance
    ankireddy


    code:-

    MainWindow.h

    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5.  
    6. #include<QHBoxLayout>
    7.  
    8. #include "gscene.h"
    9. namespace Ui {
    10. class MainWindow;
    11. }
    12.  
    13. class MainWindow : public QMainWindow
    14. {
    15. Q_OBJECT
    16.  
    17. public:
    18. explicit MainWindow(QWidget *parent = 0);
    19. ~MainWindow();
    20.  
    21. signals:
    22. void openImage(QString path);
    23.  
    24. private slots:
    25. void on_actionOpen_triggered();
    26.  
    27. private:
    28. Ui::MainWindow *ui;
    29.  
    30. gScene *scene;
    31.  
    32. };
    33.  
    34. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 


    MainWindow.CPP


    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. ui->setupUi(this);
    6.  
    7. scene = new gScene(this);
    8. scene->setSceneRect(QRectF(0, 0, 900, 900));
    9. QHBoxLayout *layout = new QHBoxLayout;
    10. view = new QGraphicsView(scene);
    11. layout->addWidget(view);
    12.  
    13. scene->setgView(view);
    14. QWidget *widget = new QWidget;
    15. widget->setLayout(layout);
    16.  
    17. connect(this,SIGNAL(openImage(QString)),scene,SLOT(openSceneImage(QString)));
    18.  
    19. //connect(scene, SIGNAL(itemInserted(DiagramItem*)),this, SLOT(itemInserted(DiagramItem*)));
    20. setCentralWidget(widget);
    21. setWindowTitle(tr("gViewTest"));
    22. setUnifiedTitleAndToolBarOnMac(true);
    23.  
    24.  
    25. scene->openSceneImage("");
    26.  
    27. }
    28.  
    29. MainWindow::~MainWindow()
    30. {
    31. delete ui;
    32. }
    To copy to clipboard, switch view to plain text mode 


    gView.h

    Qt Code:
    1. #ifndef GSCENE_H
    2. #define GSCENE_H
    3.  
    4. #include <QGraphicsScene>
    5. #include<QMessageBox>
    6. #include<QGraphicsView>
    7. #include<QGraphicsLineItem>
    8. #include<QPixmap>
    9. #include<QImage>
    10. //#include <Magick++.h>
    11. #include<QPointF>
    12. #include<QGraphicsSceneMouseEvent>
    13.  
    14.  
    15. //using namespace Magick;
    16.  
    17. class gScene : public QGraphicsScene
    18. {
    19. public:
    20. //gScene();
    21. gScene( QObject *parent);
    22. void setgView(QGraphicsView *view)
    23. {
    24. gView=view;
    25. }
    26. public slots:
    27. void openSceneImage(QString path);
    28. protected:
    29. void mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent);
    30. // void mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent);
    31. void mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent);
    32.  
    33. private:
    34. QGraphicsView *gView;
    35. QByteArray imgData;
    36. QPixmap pixmap;
    37. //Image magickImage;
    38. // Blob blob;
    39.  
    40. QImage image;
    41. QPixmap pix;
    42. QPointF first,last;
    43.  
    44. };
    45.  
    46. #endif // GSCENE_H
    To copy to clipboard, switch view to plain text mode 


    gView.cpp

    Qt Code:
    1. #include "gscene.h"
    2.  
    3.  
    4. gScene::gScene( QObject *parent = 0): QGraphicsScene(parent)
    5. {
    6.  
    7. }
    8. void gScene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
    9. {
    10. //first=mouseEvent->pos();
    11.  
    12. first.setX(10);
    13. first.setY(10);
    14.  
    15. }
    16. void gScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent)
    17. {
    18.  
    19. last.setX(100);
    20. last.setY(100);
    21.  
    22. int x1=first.x();
    23. int y1=first.y();
    24. int x2=last.x();
    25. int y2=last.y();
    26.  
    27.  
    28. QRgb color=qRgb(0,255,0);
    29.  
    30. for(int y=y1;y<=y2;y++)
    31. {
    32. for(int x=x1;x<=x2;x++)
    33. {
    34. image.setPixel(x,y,color);
    35. }
    36. }
    37.  
    38. pix=QPixmap::fromImage(image);
    39. addPixmap( pix );
    40.  
    41.  
    42. }
    43. void gScene::openSceneImage(QString path)
    44. {
    45.  
    46.  
    47.  
    48. image.load("logo.png");
    49. pix=QPixmap::fromImage(image);
    50. addPixmap( pix );
    51.  
    52. /* magickImage.read("C:\\Test\\logo.png");
    53.   //magickImage.scale(Geometry(800, 600));
    54.   magickImage.magick("RGBA");
    55.  
    56.   magickImage.write(&blob);
    57.  
    58.   imgData = ((char*)(blob.data()));
    59.  
    60.   // STEP 3 - Display
    61.   pixmap.loadFromData(imgData,"RGBA");
    62.   addPixmap( pixmap );*/
    63.  
    64. }
    To copy to clipboard, switch view to plain text mode 


    main.cpp

    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "mainwindow.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. MainWindow w;
    8. w.show();
    9.  
    10. return a.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 
    Attached Files Attached Files

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 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
    After I displayed image on the screen then my work will start. I will select an area of the image with the mouse (say the face on the logo). Now I want to change the color of these selected pixels of the image and then I should display the image on the screen with the changed pixels.
    I don't think that's a good candidate for using Graphics View.

    In this attached example in "gView" class, in button pressed event I took the start and last points in mouse pressed and released events. Now I should draw a rectangle on the image from start point to last point (with the mouse dragging) and I should change all the pixels to green. I did this on "mouseReleaseEvent" in "gView" class and displayed the image on screen with changed (green) pixels.
    Actually you are placing one image over the other. If you keep it that way, sooner or later you will run out of memory.

    I choose QGraphicsView because some times I need to show the drowned shapes (like lines, rectangles, circles etc.,) over the image (To show the user, the area which he selected).
    Since you are probably showing just one area at a time, using Graphics View is likely an overkill here.

    I tried with "QPainter" class but it is not working everywhere except in "paintEvent" (I want to run my application in all platforms ). I think it is a bit difficult to show shapes with image on the screen for every .
    No, not really. It's quite easy, actually. If you use Graphics View, that's what is happening anyway, only that you are not manually writing code that does this.

    Can you please help me whether my approach is right or wrong for my task.
    I would say it is "wrong-ish". It will work but you could do much better.
    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 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

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 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.


  7. #7

    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

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 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.


  9. #9

    Default Re: Image display using QT

    Hello wysota,

    Thank you very much for your great help.

    I did it.

    Thanks
    ankireddy.n

  10. #10
    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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.