You are not logged in.

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

Guest

Unregistered

1

Monday, December 13th 2004, 9:26pm

drawing a line by by taping the x , y

lol

Is somebody be able to help me to do this simple prog Qt .
The purpose is to draw a line by typing the x and the y .An why not by using the mouse (without typing x and y)

Here is , what I have begin

[code]
#include <qapplication.h>
#include <qcanvas.h>
#include <iostream>

using namespace std;

int main( int argc, char **argv )
{
QApplication a( argc, argv );

QCanvas *c = new QCanvas( 2100, 2940 );
QCanvasView *cv = new QCanvasView( c );

int xA , yA , xB , yB;

cout<<"Types xA ";
cin>>xA;

cout<<"Types le yA";
cin>>yA;

cout<<"Types xB";
cin>>xB;

cout<<"Types yB";
cin>>yB;


QCanvasLine *line = new QCanvasLine( c );
line->setPoints( xA, yA, xB , yB);
line->setPen( QPen( Qt::red, 0 ) );
line->show();

c->update();

a.setMainWidget( cv );
cv->show();

return a.exec();
}
[\code]

jacek

Master

  • "jacek" is male

Posts: 2,729

Location: Warsaw, Poland

  • Send private message

2

Monday, December 13th 2004, 9:41pm

RE: drawing a line by by taping the x , y

Well... it works. So what is your problem?

  • "wysota" is male

Posts: 4,276

Location: Warsaw, POLAND

  • Send private message

3

Monday, December 13th 2004, 9:57pm

RE: drawing a line by by taping the x , y

Using QCanvas and QCanvasItems for drawing a line is not the right way to do this. QCanvas is optimized for high speed and it quite memory consuming. You can draw a line by implementing a paint event, like this:

Source code

1
2
3
4
5
MyWidget::paintEvent(QPaintEvent *e){
// I assume that QPoint p1, p2 are two points that represent ends of a line which is to be drawn
  QPainter p(this);
  p.drawLine(p1,p2);
}

gandalf

Trainee

  • "gandalf" is male

Posts: 123

Location: Germany

Occupation: orderly search informatik job

  • Send private message

4

Tuesday, December 14th 2004, 9:49am

to complete this you can reimplement the mousePressEvent to get your two coordinates.

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int number; <== global
QPoint p1,p2; <== global
MyWidget::mousePressEvent(QMouseEvent *e)
{
  if(number < 1)
  {
     p1.setX(e->x());
     p1.setY(e->y());
     number++;
  }else if(number>1 && number <2)
  {
     p2.setX(e->x());
     p2.setY(e->y());
     number++;
     repaint();
  }
}


a little idea

cheers gandalf
I use Windows 2000, VS 6 with QT 3.2 non com.
QT is great!!!! 8)

Guest

Unregistered

5

Wednesday, December 15th 2004, 9:09am

Thanks for the ideas but at first I will read the blanchette_book.pdf .It seems that there is a similar example for the beginners .

@++

  • "wysota" is male

Posts: 4,276

Location: Warsaw, POLAND

  • Send private message

7

Saturday, January 1st 2005, 11:03am

At first happy new Years 2005 !

Guest alias Spot

Here is what I have done there any bugs ...

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
//header file
#ifndef PLOTTER_H
#define PLOTTER_H

#include <qpainter.h>
#include <qwidget.h>
#include <map>

class QToolButton;
class Plotter : public QWidget
{
    Q_OBJECT
public:
    Plotter(QWidget *parent = 0, const char *name = 0,
            WFlags flags = 0);
public slots:

protected:
    void paintEvent(QPaintEvent *);
    void mousePressEvent(QMouseEvent *event);

private:
    QPoint p1,p2;
  
};


#endif


Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// implementation file
#include <qpainter.h>
#include <cmath>
#include <qwidget.h> 
using namespace std;

#include "ligne.h"

Plotter::Plotter(QWidget *parent, const char *name, WFlags flags)
    : QWidget(parent, name, flags | WNoAutoErase)
{
    setBackgroundMode(PaletteDark);
}

QPoint const p1;
QPoint const p2;
int number = 0;

void Plotter::mousePressEvent(QMouseEvent *event)
{

  if(number < 1)
  {
     p1.setX(event->x());
     p1.setY(event->y());
     number = number +1;
  }else if(number>1 && number <2)
  {
     p2.setX(event->x());
     p2.setY(event->y());
     number = number +1;
     repaint();
  }

}

Plotter::paintEvent(QPaintEvent *)
{
  QPainter p(this);
  p.drawLine(const p1 , const p2);
}


Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// main file
#include <qapplication.h>
#include <qfile.h>
#include "ligne.h"

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    Plotter plotter;
    plotter.setCaption(QObject::tr("Line"));
    app.setMainWidget(&plotter);
    plotter.show();
    return app.exec();
}


Source code

1
2
3
4
5
6
7
8
//and the bug ??!
ligne.cpp:38: error: ISO C++ forbids declaration of `paintEvent' with no type
ligne.cpp:38: error: prototype for `int Plotter::paintEvent(QPaintEvent*)' does not match any in class `Plotter'
ligne.h:18: error: candidate is: virtual void Plotter::paintEvent(QPaintEvent*)
ligne.cpp:38: error: `int Plotter::paintEvent(QPaintEvent*)' and `virtual void Plotter::paintEvent(QPaintEvent*)' cannot be overloaded
ligne.cpp: In member function `int Plotter::paintEvent(QPaintEvent*)':
ligne.cpp:40: error: syntax error before `,' token
make: *** [ligne.o] Erreur 1

pablo21

Unregistered

8

Saturday, January 1st 2005, 11:27am

you forget here something:

Plotter::paintEvent(QPaintEvent *)
{
QPainter p(this);
p.drawLine(const p1 , const p2);
}

this method has to return something it should look like this:

void Plotter::paintEvent(QPaintEvent *)
{
QPainter p(this);
p.drawLine(const p1 , const p2);
}

9

Saturday, January 1st 2005, 3:50pm

You was right Pablo21 but now there is a logical error
Nothing happens when i presse 2 points on my widget .None line was drawn .
What have I to add in my code ?

here is the code that i have changed .

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <qpainter.h>
#include <cmath>
#include <qwidget.h> 

using namespace std;

#include "ligne.h"

Plotter::Plotter(QWidget *parent, const char *name, WFlags flags)
    : QWidget(parent, name, flags | WNoAutoErase)
{
    setBackgroundMode(PaletteDark);
}

QPoint p1;
QPoint p2;
int number = 0;

void Plotter::mousePressEvent(QMouseEvent *event)
{

  if(number < 1)
  {
     p1.setX(event->x());
     p1.setY(event->y());
     number = number +1;
  }else if(number>1 && number <2)
  {
     p2.setX(event->x());
     p2.setY(event->y());
     number = number +1;
     update();
  }
  setCursor(crossCursor);
}

void Plotter::paintEvent(QPaintEvent *)
{
  QPainter p(this);
  p.setPen(colorGroup().light());
  p.drawLine(p1, p2);
}