|
发表于 2006-6-11 05:02:43
|
显示全部楼层
Re: 求助:请教大家一个QT的画图问题(急)
[quote:5fada19c78="realxin"]下面这个程序能使用鼠标画直线,但是画了新的直线后原来的就消失了,请问如何让已经画好的直线不消失?请高手指点啊!多谢!!!
#include <qwidget.h>
#include <qpainter.h>
#include <qapplication.h>
#include <stdlib.h>
const int MAXPOINTS = 2000; // maximum number of points
const int MAXCOLORS = 40;
class ConnectWidget : public QWidget
{
public:
ConnectWidget( QWidget *parent=0, const char *name=0 );
~ConnectWidget();
protected:
void paintEvent( QPaintEvent * );
void mousePressEvent( QMouseEvent *);
void mouseReleaseEvent( QMouseEvent *);
void mouseMoveEvent( QMouseEvent *);
private:
QPoint *points; // point array
QColor *colors; // color array
int count; // count = number of points
bool down; // TRUE if mouse down
};
ConnectWidget::ConnectWidget( QWidget *parent, const char *name )
: QWidget( parent, name, WStaticContents )
{
setBackgroundColor( white ); // white background
count = 0;
down = FALSE;
points = new QPoint[MAXPOINTS];
colors = new QColor[MAXCOLORS];
for ( int i=0; i<MAXCOLORS; i++ ) // init color array
colors = QColor( rand()&255, rand()&255, rand()&255 );
}
ConnectWidget::~ConnectWidget()
{
delete[] points; // cleanup
delete[] colors;
}
void ConnectWidget::paintEvent( QPaintEvent * )
{
QPainter paint( this );
paint.setPen( colors[rand()%MAXCOLORS] ); // set random pen color
paint.drawLine( points[0], points[count-1] ); // draw line
}
void ConnectWidget::mousePressEvent( QMouseEvent * )
{
down = TRUE;
count = 0; // start recording points
erase(); // erase widget contents
}
void ConnectWidget::mouseReleaseEvent( QMouseEvent * )
{
down = FALSE; // done recording points
update(); // draw the lines
}
void ConnectWidget::mouseMoveEvent( QMouseEvent *e )
{
if ( down && count < MAXPOINTS ) {
QPainter paint( this );
points[count++] = e->pos(); // add point
paint.drawPoint( e->pos() ); // plot point
}
}
int main( int argc, char **argv )
{
QApplication a( argc, argv );
ConnectWidget connect;
#ifndef QT_NO_WIDGET_TOPEXTRA // for Qt/Embedded minimal build
connect.setCaption( "Qt Example - Draw lines");
#endif
a.setMainWidget( &connect );
connect.show();
return a.exec();
}[/quote]
我也是刚刚开始学习Linux的编程,不太清楚,但是我想和Windows下面应该差不多,在Windows下面需要将原来的保存(可以保存坐标)。当画下一条线时先画原来的,再画新的。 |
|