Results 1 to 15 of 15

Thread: draw a point

  1. #1
    Join Date
    Jul 2010
    Posts
    30
    Thanks
    6
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default draw a point

    hello everybody...

    I have a screen of map of the world, and I want to draw a small point somewhere on the map, my parameters are the coordinates of this location (longitude and latitude).
    May I do something like this in QT, and may I make this point clickable??

    Please in details, I want to know how to draw a point firstly.


    thank you.

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: draw a point

    Two solutions:

    1. A subclass widget, displaying the map and then painting a point on the correct position (see subclassing widgets and painting widgets in the documentation)
    You'll have some work with implementing the mouse events though.

    2. Using QGraphicsview. Mouse handling is already done for you. You just need to connect it all. Use a pixmap or svg item for the map and one of the polygon, path, svg, ... items for the point. See the graphics view documentation.

  3. #3
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: draw a point

    Whatever you choose, you will basically need to override paintEvent and mousePressEvent of the QWidget or QGraphicsView / QGraphicsScene

  4. #4
    Join Date
    Jul 2010
    Posts
    30
    Thanks
    6
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: draw a point

    Hello tbscope..
    Hello aamer4yu..

    Thank you for your reply.

    I firstly want to know how to draw a point.Just point.
    I have read some parts from the documents but I failed on practice.

    e.g. this code witch is from doc does no work with me.There is no errors but mr.QT tell me that Painter not active

    Qt Code:
    1. scene.addRect(QRectF(0, 0, 100, 200), QPen(Qt::black), QBrush(Qt::green));
    2.  
    3. QPixmap pixmap;
    4. QPainter painter(&pixmap);
    5. painter.setRenderHint(QPainter::Antialiasing);
    6. scene.render(&painter);
    7. painter.end();
    8.  
    9. pixmap.save("scene.png");
    To copy to clipboard, switch view to plain text mode 

    I know that this code is not for drawing a point ,but it is same concept.

    Thank you for your interest.

  5. #5
    Join Date
    Jul 2010
    Posts
    30
    Thanks
    6
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: draw a point

    May any one help me.
    I just want to draw a point now.

    Regards,

  6. #6
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: draw a point

    Look at the QPainter documentation. There are several good tutorials on painting in the Qt package, as well.

  7. #7
    Join Date
    Jul 2010
    Posts
    30
    Thanks
    6
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: draw a point

    I will read it.
    Thank you.

  8. #8
    Join Date
    Nov 2009
    Posts
    29
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: draw a point

    Hi,

    Maybe this can help you. This is a test program I made. It draws a circle on screen following spinbuttons coordinates:

    main.cpp

    Qt Code:
    1. #include <QApplication>
    2. #include <QGridLayout>
    3. #include <QPushButton>
    4. #include <QLabel>
    5. #include <QHBoxLayout>
    6. #include <QVBoxLayout>
    7. #include <QSpinBox>
    8. #include "areadibujo.h"
    9.  
    10. class MyWidget : public QWidget
    11. {
    12. public:
    13. MyWidget(QWidget *parent = 0);
    14. };
    15.  
    16. MyWidget::MyWidget(QWidget *parent):QWidget(parent)
    17. {
    18. AreaDibujo *area = new AreaDibujo;
    19.  
    20. QPushButton *salir = new QPushButton("Salir");
    21. QPushButton *dibujar = new QPushButton("Dibujar");
    22. QSpinBox *valorX = new QSpinBox;
    23. QSpinBox *valorY = new QSpinBox;
    24.  
    25.  
    26. valorX->setRange(0, 1000);
    27. valorY->setRange(0, 1000);
    28. valorX->setValue(100);
    29. valorY->setValue(100);
    30.  
    31. connect(salir, SIGNAL(clicked()), qApp, SLOT(quit()));
    32. connect(dibujar, SIGNAL(clicked()), area, SLOT(Dibuja()));
    33. connect(valorX, SIGNAL(valueChanged(int)), area, SLOT(ColocaX(int)));
    34. connect(valorY, SIGNAL(valueChanged(int)), area, SLOT(ColocaY(int)));
    35.  
    36. QVBoxLayout *dibujo = new QVBoxLayout;
    37. QHBoxLayout *valores = new QHBoxLayout;
    38.  
    39. valores->addWidget(valorX);
    40. valores->addWidget(valorY);
    41. dibujo->addWidget(area);
    42. dibujo->addLayout(valores);
    43. dibujo->addWidget(dibujar);
    44. dibujo->addWidget(salir);
    45.  
    46. setLayout(dibujo);
    47. }
    48.  
    49. int main(int argc, char *argv[])
    50. {
    51. QApplication app(argc, argv);
    52. MyWidget widget;
    53. widget.setGeometry(100, 100, 500, 500);
    54. widget.show();
    55. return app.exec();
    56. }
    To copy to clipboard, switch view to plain text mode 

    areadibujo.h

    Qt Code:
    1. #ifndef __AREADIBUJO_H__
    2. #define __AREADIBUJO_H__
    3.  
    4. #include <QWidget>
    5.  
    6. class AreaDibujo : public QWidget
    7. {
    8. Q_OBJECT
    9.  
    10. public:
    11. AreaDibujo(QWidget *parent=0);
    12.  
    13. protected:
    14. void paintEvent(QPaintEvent *event);
    15.  
    16. public slots:
    17. void ColocaX(int colocaX);
    18. void ColocaY(int colocaY);
    19. void Dibuja();
    20.  
    21. signals:
    22. void cambioX(int nuevoX);
    23. void cambioY(int nuevoY);
    24.  
    25. private:
    26. void pintaCirculo(QPainter &painter);
    27. int Xactual;
    28. int Yactual;
    29. bool pulsador;
    30. };
    31. #endif
    To copy to clipboard, switch view to plain text mode 

    areadibujo.cpp
    Qt Code:
    1. #include "areadibujo.h"
    2. #include <QPainter>
    3.  
    4. AreaDibujo::AreaDibujo(QWidget *parent):QWidget(parent)
    5. {
    6. setPalette(QPalette(QColor(255, 255, 255)));
    7. setAutoFillBackground(true);
    8. Xactual=100;
    9. Yactual=100;
    10. pulsador=false;
    11. }
    12.  
    13. void AreaDibujo::paintEvent(QPaintEvent *)
    14. {
    15. QPainter painter(this);
    16. if(pulsador)
    17. {
    18. pintaCirculo(painter);
    19. }
    20. }
    21.  
    22. void AreaDibujo::Dibuja()
    23. {
    24. pulsador=true;
    25. update();
    26. }
    27.  
    28. void AreaDibujo::pintaCirculo(QPainter &painter)
    29. {
    30. painter.setPen(Qt::blue);
    31. painter.setBrush(Qt::blue);
    32. painter.setRenderHint(QPainter::Antialiasing);
    33. painter.drawEllipse(Xactual-25,Yactual-25,50,50);
    34. }
    35.  
    36. void AreaDibujo::ColocaX(int colocaX)
    37. {
    38. Xactual=colocaX;
    39. emit cambioX(Xactual);
    40. }
    41.  
    42. void AreaDibujo::ColocaY(int colocaY)
    43. {
    44. Yactual=colocaY;
    45. emit cambioY(Yactual);
    46. }
    To copy to clipboard, switch view to plain text mode 

    Sorry for the spanish words in the code, but I think you can follow.

  9. #9
    Join Date
    Jul 2010
    Posts
    30
    Thanks
    6
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: draw a point

    Thanks rdelgado.
    There is a differance between Beginner and another Beginner.

    Your code is nice , but I can't deal with longitude and latitude as an x-y plane.

    Thanks for your interest.

  10. #10
    Join Date
    May 2010
    Posts
    30
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: draw a point

    If you can convert your (Lat, Lon) to (X, Y), then I suggest using QImage rather than QPixmap, because it is built for drawing Pixels on the image it holds. They load the same way, and can both be viewed and manipulated with the QGraphicsFramework.

  11. #11
    Join Date
    Jul 2010
    Posts
    30
    Thanks
    6
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: draw a point

    Thanks grabalon..
    Actually I can't convert (Lat, Lon) to (X, Y).If you have any Idea ,Pleas tell me.

    Anyway ...
    while I try to draw this great point this message appear at run time:

    Starting /home/MALEK/soso-build-desktop/soso...
    QPainter::begin: Widget painting can only begin as a result of a paintEvent
    QPainter::beginNativePainting: Painter not active
    QPainter::setPen: Painter not active
    QPainter::setBrush: Painter not active
    QPainter::setRenderHint: Painter must be active to set rendering hints

    My code is:
    Qt Code:
    1. void MainWindow::on_pushButton_clicked()
    2. {
    3. scene->addLine(10,10,20,20);
    4. QPixmap m("/home/MALEK/QT-Creator/PROJECTs/soso/bomb.jpeg");
    5. scene->setBackgroundBrush(m.scaled(100,100,Qt::IgnoreAspectRatio,Qt::SmoothTransformation));
    6.  
    7. QPainter p(this);
    8. p.beginNativePainting();
    9. MainWindow::alm(p);
    10.  
    11. ui->graphicsView->setScene(scene);
    12. //QPolygonF *poly;
    13. //scene->addPolygon(poly,QPen(Qt::blue),QBrush(Qt::red));
    14. }
    15.  
    16. void MainWindow::alm(QPainter &painter)
    17. {
    18. //if(painter.isActive())
    19. {
    20. painter.setPen(Qt::blue);
    21. painter.setBrush(Qt::blue);
    22. painter.setRenderHint(QPainter::Antialiasing);
    23. painter.drawEllipse(100-25,100-25,50,50);
    24. }
    25. }
    To copy to clipboard, switch view to plain text mode 


    Iwant to make painter active . Can Any one help me!!

  12. #12
    Join Date
    Jul 2010
    Posts
    30
    Thanks
    6
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: draw a point

    Starting /home/MALEK/soso-build-desktop/soso...
    QPainter::begin: Paint device returned engine == 0, type: 2
    QPainter::setRenderHint: Painter must be active to set rendering hints
    QPainter::save: Painter not active
    QPainter::setClipRect: Painter not active
    QPainter::setWorldTransform: Painter not active
    QPainter::worldTransform: Painter not active
    QPainter::save: Painter not active
    QPainter::setWorldTransform: Painter not active
    QPainter::setOpacity: Painter not active
    QPainter::setPen: Painter not active
    QPainter::setBrush: Painter not active
    QPainter::drawRects: Painter not active
    QPainter::restore: Unbalanced save/restore
    QPainter::setWorldTransform: Painter not active
    QPainter::restore: Unbalanced save/restore
    QPainter::end: Painter not active, aborted
    /home/MALEK/soso-build-desktop/soso exited with code 0
    Even When I use the code of the tutorial this msgs appears !!

    I use this code
    Qt Code:
    1. scene.addRect(QRectF(0, 0, 100, 200), QPen(Qt::black), QBrush(Qt::green));
    2.  
    3. QPixmap pixmap;
    4. QPainter painter(&pixmap);
    5. painter.setRenderHint(QPainter::Antialiasing);
    6. scene.render(&painter);
    7. painter.end();
    8.  
    9. pixmap.save("scene.png");
    To copy to clipboard, switch view to plain text mode 

    from this link:
    http://doc.qt.nokia.com/4.6/graphicsview.html

  13. #13
    Join Date
    Jul 2010
    Posts
    30
    Thanks
    6
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: draw a point

    It is very simple issue.I just draw it like this

    Qt Code:
    1. QPen pen;
    2. pen.color().blue();
    3. scene->addEllipse(QRectF(0,0 ,10 ,10 ), pen, QBrush(Qt::red));
    4. ui->graphicsView->setScene(scene);
    To copy to clipboard, switch view to plain text mode 

    NOW !!!
    You MUST help me in this.

    1) I want to change the point location. It is always in the same location even when I replace (0,0 ,10,10) by (168,324,10,10) for example.

    2) How can I change the location of the point as (Lat, Lon),Since my background will be the map of the WORLD.


    THANKS FOR ALL.

  14. #14
    Join Date
    Jul 2010
    Posts
    30
    Thanks
    6
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: draw a point

    911 !!
    Help me please !!

  15. #15
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: draw a point

    Quote Originally Posted by Malek View Post
    1) I want to change the point location. It is always in the same location even when I replace (0,0 ,10,10) by (168,324,10,10) for example.
    From the docs: "Note that the item's geometry is provided in item coordinates, and its position is initialized to (0, 0)." If you only have one item on your scene it will always be in the same location. Try adding two ellipse items.

    2) How can I change the location of the point as (Lat, Lon),Since my background will be the map of the WORLD.
    Try something like this:
    Qt Code:
    1. QPixmap pixmap("/path/to/worldmap.jpg");
    2. scene.addPixmap(pixmap);
    3. scene.addEllipse(QRectF(168,324,10,10),QPen(Qt::red),QBrush(Qt::red));
    4. QGraphicsView view(&scene);
    5. view.show();
    To copy to clipboard, switch view to plain text mode 
    You'll have to handle translating lat/long to scene coordinates.

Similar Threads

  1. Replies: 1
    Last Post: 3rd December 2009, 14:23
  2. Replies: 6
    Last Post: 21st September 2009, 10:55
  3. display a plot point by point
    By oswalidos in forum Newbie
    Replies: 32
    Last Post: 13th March 2009, 15:37
  4. draw point with mouseevent
    By konvex in forum Qt Programming
    Replies: 3
    Last Post: 12th November 2008, 04:49
  5. how to paint scene point by point
    By elessaar in forum Qt Programming
    Replies: 8
    Last Post: 4th September 2008, 20:00

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.