PDA

View Full Version : Qwt Plot not working



astronaut
13th June 2014, 04:14
Hello

I followed the example of simple plot in qwt examples to plot a curve. The axis and the graph appear in the Qt main window user interface but the curve not. I assigned values to fit the curve but the curve not appear. Any suggestions and help how to solve the problem? Here is my code


MainWindow::MainWindow( int argc, char** argv, QWidget *parent )
: QMainWindow( parent )
, qnode( argc,argv )
{
ui.setupUi( this ); // Calling this incidentally connects all ui's triggers to on_...() callbacks in this class.
QObject::connect( ui.actionAbout_Qt, SIGNAL( triggered( bool )), qApp, SLOT( aboutQt( ))); // qApp is a global variable for the application
ReadSettings( );
setWindowIcon( QIcon( ":/images/icon.png" ));
ui.tab_manager->setCurrentIndex( 0 ); // ensure the first tab is showing - qt-designer should have this already hardwired, but often loses it (settings?).
QObject::connect( &qnode, SIGNAL( rosShutdown( )), this, SLOT( close( )));

/*********************
** Logging
**********************/
ui.view_logging->setModel( qnode.loggingModel( ));
QObject::connect( &qnode, SIGNAL( loggingUpdated( )), this, SLOT( updateLoggingView( )));
QObject::connect( &qnode, SIGNAL( graphReceived( )), this, SLOT( onGraphReceived( )));
QObject::connect( &qnode, SIGNAL( parameterReceived( )), this, SLOT( onParameterReceived( )));
/*********************
** Auto Start
**********************/
if ( ui.checkbox_remember_settings->isChecked( ))
{
on_button_connect_clicked( true );
}
ui.parameters->setAttribute( Qt::WA_NoMousePropagation );
ui.parameters->setAttribute( Qt::WA_OpaquePaintEvent );
ui.plotgraph->setAttribute( Qt::WA_NoMousePropagation );
ui.plotgraph->setAttribute( Qt::WA_OpaquePaintEvent );

p_plot = new QwtPlot(ui.plotgraph);
p_plot->setTitle( "Plot LinVel" );
p_plot->setCanvasBackground( Qt::white );
// Axis
p_plot->setAxisTitle( QwtPlot::xBottom, "Time(sec)" );
p_plot->setAxisTitle( QwtPlot::yLeft, "Linear Velocity (m/sec)" );
p_plot->setAxisScale( QwtPlot::yLeft, 0.0, 10.0 );
p_plot->setAxisScale( QwtPlot::xBottom, 0.0, 50.0 );
p_plot->insertLegend( new QwtLegend() );

//samplingThread.start();
QwtPlotGrid *grid = new QwtPlotGrid();
grid->attach( p_plot );

curve = new QwtPlotCurve();
curve->setTitle( "Linear velocity" );
// Set curve styles
curve->setPen( Qt::blue, 4 ),
curve->setRenderHint( QwtPlotItem::RenderAntialiased, true );

QwtSymbol *symbol = new QwtSymbol( QwtSymbol::Ellipse,
QBrush( Qt::yellow), QPen( Qt::red, 2 ), QSize( 8, 8 ) );
curve->setSymbol( symbol);

// Assign values to the curve
//curve->setSamples(ui.plotgraph.get_linv_g());//yaw_g,trav_g,wall_g;
curve->attach( p_plot );

p_plot->resize( 600, 400 );
p_plot->show();

void MainWindow::onGraphReceived( )
{
{
QMutexLocker locker( &qnode.m_mutex );
}
}


void MainWindow::onParameterReceived( )
{
{
QMutexLocker locker( &qnode.m_mutex );
std::vector<double> p_ = qnode.get_parameters();
std::cout << p_[0]<<" "<<p_[1]<<" "<<p_[2]<<" "<<p_[3]<<" "<<p_[4] << std::endl;
}
}


thanks

TheBadger
13th June 2014, 06:41
Hi,

Perhaps you have the same problem I had...
I needed to call QwtPlot::replot()

All I can help with...

Uwe
13th June 2014, 07:42
I assigned values to fit the curve ...
Not in the posted code above.

Uwe

astronaut
16th June 2014, 07:17
Sorry here is the code

namespace enc = sensor_msgs::image_encodings;
namespace pow_gui {

QNode::QNode( int argc, char** argv )
: init_argc( argc )
, init_argv( argv )
, m_parameterCallbackCount( 0 )
, m_graphCallbackCount( 0 )
{}

QNode::~QNode() {
if(ros::isStarted()) {
ros::shutdown( ); // explicitly needed since we use ros::start();
ros::waitForShutdown( );
}
wait( );
}

bool QNode::initCommon( )
{

ros::NodeHandle nh( "~" );
std::string parameterTopic = nh.resolveName("/stats");
std::string graphTopic = nh.resolveName("/to_graph");
m_parameter_subscriber = nh.subscribe<std_msgs::Float64MultiArray>(parameterTopic, 10, &QNode::parameterCallback, this );
gsub = nh.subscribe<std_msgs::Float64MultiArray>(graphTopic, 10, &QNode::graphCallback, this );
result = 0;
start();
return true;
}

void QNode::parameterCallback( const std_msgs::Float64MultiArray::ConstPtr& msg )
{
std::cout << "parameterCallback " << ++m_parameterCallbackCount << std::endl;
parameters=msg->data;
Q_EMIT parameterReceived();
}

void QNode::graphCallback( const std_msgs::Float64MultiArray::ConstPtr& msg )
{
//std::cout << "graphCallback " << ++m_graphCallbackCount << std::endl;
++m_graphCallbackCount;

linv_g<< QPointF( (float)msg->data[0], (float)msg->data[1] );
yaw_g<< QPointF( (float)msg->data[0], (float)msg->data[2] );
trav_g<< QPointF( (float)msg->data[0], (float)msg->data[3] );
wall_g<< QPointF( (float)msg->data[0], (float)msg->data[4] );
Q_EMIT graphReceived();

}

Here the header files

class QNode : public QThread {
Q_OBJECT
public:
QNode(int argc, char** argv );
virtual ~QNode();
bool init();
bool init(const std::string &master_url, const std::string &host_url);
void run();

/*********************
** Logging
**********************/
enum LogLevel {
Debug,
Info,
Warn,
Error,
Fatal
};


inline std::vector<double> get_parameters() { return parameters; }
QPolygonF linv_g,yaw_g,trav_g,wall_g;
}

void parameterReceived();
void graphReceived();

private:
bool initCommon();
void parameterCallback( const std_msgs::Float64MultiArrayConstPtr& msg );
void graphCallback( const std_msgs::Float64MultiArrayConstPtr& msg );
private:
int init_argc;
char** init_argv;
ros::Subscriber m_parameter_subscriber;
ros::Subscriber gsub;
float m_parameterCallbackCount;
float m_graphCallbackCount;
std::vector<double> parameters;

And the interface header

public Q_SLOTS:
/******************************************
** Auto-connections (connectSlotsByName( ))
*******************************************/
void onScoreReceived( );
void onParameterReceived( );
void onGraphReceived( );

inline QPolygonF get_linv_g() { return qnode.linv_g; }
inline QPolygonF get_yaw_g() { return qnode.yaw_g; }
inline QPolygonF get_trav_g() { return qnode.trav_g; }
inline QPolygonF get_wall_g() { return qnode.wall_g; }

private:
Ui::MainWindowDesign ui;
QNode qnode;
std::stringstream ss1, ss2;

Plot *d_plot;
QwtPlot* p_plot;
QwtPlotCurve *curve;
};

}

#endif