[kde-edu]: Cartesian plane on qt canvas

Jason Harris kstars at 30doradus.org
Wed Dec 15 04:04:15 CET 2004


Hello,

On Wednesday 08 December 2004 01:47 pm, Trenton wrote:
> I have hit another snag, unrelated to your excellent plot lib.
> Every thing is working perfectly, but now I would like to connect the  
> signal from a double spinbox to the trigviewer and I can't get it to 
> work.   

>      connect( spin, SIGNAL(valueChanged( double )), deg,  
> SLOT(setAngle( double )));                    
> 
>      deg_theta = deg->angle();  
> 
The problem is that you set the value of deg_theta in the constructor 
function, which is called only once when the program starts.  At this 
point, the "valueChanged" signal has never fired, so deg_theta is equal 
to 45.0 (the default value for the deg object).  Later, when you modify 
the spinbox, the valueChanged signal is sent, so the setAngle() slot 
fires and resets the value of the deg object.  However, you never 
update the value of deg_theta, so it's always equal to 45.0.  (You 
actually don't need the deg_theta variable at all, you can just use 
deg->angle() instead).

Similarly, your "drawObjects()" function is only called once, in the 
constructor, so the plot objects are set initially, and then never 
changed.  Actually, the "drawObjects()" function as written actually 
*should* only be called once, since it creates new KPlotObjects (maybe 
rename it "initObjects()" to avoid confusion).  What you want is 
another SLOT function that modifies the existing plot objects when the 
angle value is modified by the spinbox (i.e., connect the slot to deg's 
valueChanged() signal).

Actually, why do you use three separate plot objects for the sides of 
the triangle?  You could just use one object of type POLYGON, and use 
the three vertices of the triangle for the object's points. If you make 
this change, then the new slot function will look something like this:

void TrigViewer::updateAngle() {
    double radians = deg->angle() * M_PI / 180;
    double X_val = 100*cos(radians);
    double Y_val = 100*sin(radians);
    
    //We now have only one plot object; modify its 
    //three vertices to match the new angle
    KPlotObject *o = pw->object(0);
    
    //First point is (0, 0), so no need to modify it
    //Second point is (X, 0)
    o->point(1)->setX( X_val );

    //Third point is (X, Y)
    o->point(2)->setX( X_val );
    o->point(2)->setY( Y_val );

    //Redraw the widget
    update();
}

> By the way, your most excellent work has taught me more about coding 
> in the last three days than the preceding year of books and web 
> resources. 

Wow, thanks!  I guess that's what this open source thing is all about :)

One more hint:  your Deg_Theta class doesn't need to inherit QWidget, 
since it doesn't have any GUI aspect to it.  It should inherit QObject 
instead.

regards,
Jason
-- 
KStars: Desktop Planetarium for KDE
http://edu.kde.org/kstars


More information about the kde-edu mailing list