PDA

View Full Version : draw a point



Malek
4th August 2010, 21:44
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.

tbscope
5th August 2010, 05:45
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.

aamer4yu
5th August 2010, 07:07
Whatever you choose, you will basically need to override paintEvent and mousePressEvent of the QWidget or QGraphicsView / QGraphicsScene

Malek
6th August 2010, 21:13
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


QGraphicsScene scene;
scene.addRect(QRectF(0, 0, 100, 200), QPen(Qt::black), QBrush(Qt::green));

QPixmap pixmap;
QPainter painter(&pixmap);
painter.setRenderHint(QPainter::Antialiasing);
scene.render(&painter);
painter.end();

pixmap.save("scene.png");

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

Thank you for your interest.

Malek
14th August 2010, 16:41
May any one help me.
I just want to draw a point now.

Regards,

SixDegrees
14th August 2010, 22:29
Look at the QPainter documentation. There are several good tutorials on painting in the Qt package, as well.

Malek
15th August 2010, 11:56
I will read it.
Thank you.

rdelgado
16th August 2010, 20:36
Hi,

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

main.cpp



#include <QApplication>
#include <QGridLayout>
#include <QPushButton>
#include <QLabel>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QSpinBox>
#include "areadibujo.h"

class MyWidget : public QWidget
{
public:
MyWidget(QWidget *parent = 0);
};

MyWidget::MyWidget(QWidget *parent):QWidget(parent)
{
AreaDibujo *area = new AreaDibujo;

QPushButton *salir = new QPushButton("Salir");
QPushButton *dibujar = new QPushButton("Dibujar");
QSpinBox *valorX = new QSpinBox;
QSpinBox *valorY = new QSpinBox;


valorX->setRange(0, 1000);
valorY->setRange(0, 1000);
valorX->setValue(100);
valorY->setValue(100);

connect(salir, SIGNAL(clicked()), qApp, SLOT(quit()));
connect(dibujar, SIGNAL(clicked()), area, SLOT(Dibuja()));
connect(valorX, SIGNAL(valueChanged(int)), area, SLOT(ColocaX(int)));
connect(valorY, SIGNAL(valueChanged(int)), area, SLOT(ColocaY(int)));

QVBoxLayout *dibujo = new QVBoxLayout;
QHBoxLayout *valores = new QHBoxLayout;

valores->addWidget(valorX);
valores->addWidget(valorY);
dibujo->addWidget(area);
dibujo->addLayout(valores);
dibujo->addWidget(dibujar);
dibujo->addWidget(salir);

setLayout(dibujo);
}

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MyWidget widget;
widget.setGeometry(100, 100, 500, 500);
widget.show();
return app.exec();
}


areadibujo.h



#ifndef __AREADIBUJO_H__
#define __AREADIBUJO_H__

#include <QWidget>

class AreaDibujo : public QWidget
{
Q_OBJECT

public:
AreaDibujo(QWidget *parent=0);

protected:
void paintEvent(QPaintEvent *event);

public slots:
void ColocaX(int colocaX);
void ColocaY(int colocaY);
void Dibuja();

signals:
void cambioX(int nuevoX);
void cambioY(int nuevoY);

private:
void pintaCirculo(QPainter &painter);
int Xactual;
int Yactual;
bool pulsador;
};
#endif


areadibujo.cpp


#include "areadibujo.h"
#include <QPainter>

AreaDibujo::AreaDibujo(QWidget *parent):QWidget(parent)
{
setPalette(QPalette(QColor(255, 255, 255)));
setAutoFillBackground(true);
Xactual=100;
Yactual=100;
pulsador=false;
}

void AreaDibujo::paintEvent(QPaintEvent *)
{
QPainter painter(this);
if(pulsador)
{
pintaCirculo(painter);
}
}

void AreaDibujo::Dibuja()
{
pulsador=true;
update();
}

void AreaDibujo::pintaCirculo(QPainter &painter)
{
painter.setPen(Qt::blue);
painter.setBrush(Qt::blue);
painter.setRenderHint(QPainter::Antialiasing);
painter.drawEllipse(Xactual-25,Yactual-25,50,50);
}

void AreaDibujo::ColocaX(int colocaX)
{
Xactual=colocaX;
emit cambioX(Xactual);
}

void AreaDibujo::ColocaY(int colocaY)
{
Yactual=colocaY;
emit cambioY(Yactual);
}


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

Malek
19th August 2010, 16:20
Thanks rdelgado.
There is a differance between Beginner and another Beginner. :cool:

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

Thanks for your interest.

grabalon
19th August 2010, 18:13
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.

Malek
21st August 2010, 23:37
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:

void MainWindow::on_pushButton_clicked()
{
QGraphicsScene *scene = new QGraphicsScene();
scene->addLine(10,10,20,20);
QPixmap m("/home/MALEK/QT-Creator/PROJECTs/soso/bomb.jpeg");
scene->setBackgroundBrush(m.scaled(100,100,Qt::IgnoreAspe ctRatio,Qt::SmoothTransformation));

QPainter p(this);
p.beginNativePainting();
MainWindow::alm(p);

ui->graphicsView->setScene(scene);
//QPolygonF *poly;
//scene->addPolygon(poly,QPen(Qt::blue),QBrush(Qt::red));
}

void MainWindow::alm(QPainter &painter)
{
//if(painter.isActive())
{
painter.setPen(Qt::blue);
painter.setBrush(Qt::blue);
painter.setRenderHint(QPainter::Antialiasing);
painter.drawEllipse(100-25,100-25,50,50);
}
}


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

Malek
22nd August 2010, 02:04
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

QGraphicsScene scene;
scene.addRect(QRectF(0, 0, 100, 200), QPen(Qt::black), QBrush(Qt::green));

QPixmap pixmap;
QPainter painter(&pixmap);
painter.setRenderHint(QPainter::Antialiasing);
scene.render(&painter);
painter.end();

pixmap.save("scene.png");

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

Malek
24th August 2010, 01:40
It is very simple issue.I just draw it like this



QPen pen;
pen.color().blue();
QGraphicsScene *scene = new QGraphicsScene();
scene->addEllipse(QRectF(0,0 ,10 ,10 ), pen, QBrush(Qt::red));
ui->graphicsView->setScene(scene);


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.

Malek
26th August 2010, 10:46
911 !!
Help me please !!

norobro
26th August 2010, 20:13
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 (http://doc.trolltech.com/4.6/qgraphicsscene.html#addEllipse): "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:
QGraphicsScene scene;
QPixmap pixmap("/path/to/worldmap.jpg");
scene.addPixmap(pixmap);
scene.addEllipse(QRectF(168,324,10,10),QPen(Qt::re d),QBrush(Qt::red));
QGraphicsView view(&scene);
view.show();You'll have to handle translating lat/long to scene coordinates.