PDA

View Full Version : Graphics



TheDuncan
1st April 2010, 20:42
I'm trying to do some basic graphics. Basically, I have an array of non-negative values that represent distance from a point and the array index represents the angle. I want to draw an oval at the origin and a circle at each of these places. I also need this area where I'm drawing this stuff to reside inside a section on a form and the rest of the form will have other text data.

Example:

If the ary[90] contains the value 5000 (distance is in millimeters) then it would draw a small circle straight ahead of the oval at the origin.

I have not done anything like this in Qt before so any resources, ideas, advice, etc is GREATLY appreciated.

Thanks.

wysota
1st April 2010, 21:28
Take a look at QPainter or QGraphicsView as any of the two approaches can be used in your situation.

TheDuncan
2nd April 2010, 15:30
QPainter looks like what I need. I understand that it actually does the drawing, but what widgets can I use it on?

wysota
2nd April 2010, 16:06
You can't use any widgets "on" it. It's a method of painting on widgets. You need to create a custom widget and draw it using QPainter in its paintEvent().

TheDuncan
2nd April 2010, 17:30
Gotcha. How does this look for a start.



#ifndef MAPWIDGET_H
#define MAPWIDGET_H

#include <QWidget>
#include <QPainter>

class mapWidget : public QWidget
{
Q_OBJECT

public:
mapWidget(QWidget *parent = 0);

void setLMS(long data[]);
void setCamera(long data[]);
void draw(QPainter *painter);

private:
long lmsData[1080];
long cameraData[180];

protected:
void paintEvent(QPaintEvent * event);

};

#endif // MAPWIDGET_H

wysota
2nd April 2010, 17:42
Looks fine, just make the constructor explicit, "just in case". I'd also suggest to replace those C array arguments with QVector or QList.

TheDuncan
2nd April 2010, 17:55
I'll do that.

How do you add a custom widget to a form?

aamer4yu
2nd April 2010, 18:31
A custom widget is still a widget, you can directly show it using QWidget::show, or you can add it to any other widget in that widgets layout.
Give some time exploring the examples and documentation.

TheDuncan
2nd April 2010, 23:02
Ok, but where do I use those commands? In main? Do How do I limit the space or assign where on the form the widget appears?

TheDuncan
5th April 2010, 18:26
Thanks for all the help. Works like a champ