How to use QPainter and QTimer to draw a real time sinusoid like
sinus( 2 * w * t + phi )
thanks.
I would assume something like this, for the interaction between QTimer and painting:
// Periodically paints a sinusoid on itself.
class SinPainter : public QWidget
{
Q_OBJECT
public:
SinPainter( QWidget *parent_p = NULL ) :
QWidget( parent_p ),
m_timer_p( new QTimer( this ) ),
m_t( 0.0 )
{
// When the timer goes off, run our function to change the t value.
connect( m_timer_p, SIGNAL( timeout() ), SLOT( ChangeT() ) );
// Start the timer to go off every TIMER_INTERVAL milliseconds
m_timer_p->start( TIMER_INTERVAL );
}
// ...
protected slots:
void ChangeT()
{
// Change m_t to the new value.
m_t += T_INCREMENT;
// Calling update schedules a repaint event, assuming one hasn't
// already been scheduled.
update();
}
protected:
void paintEvent( QPaintEvent *e_p )
{
QPainter painter( this );
// Use painter and m_t to draw your current sinusoid according
// to your function.
}
private:
QTimer *m_timer_p;
double m_t; // <-- Or whatever variable type it needs to be.
};
Related
I am having a difficulty in understanding qwt oscilloscope example.
i understand most of the program roughly, but i can not find the linkage between samplingthread class and plot class.
It seems like chart samples are from samplingthread and it is provided to QwtPlotCurve object in plot class.
However i can not find linkage between samplingthread object and plot object. But when i change the frequency value in samplingthread object, it is applied and appears on plot object (canvas).
Below is part of code (from main.cpp) i do not really understood but please reference full project (need decompress i think) by downloading from
http://sourceforge.net/projects/qwt/files/qwt/6.1.2/.
int main( int argc, char **argv )
{
QApplication app( argc, argv );
app.setPalette( Qt::darkGray );
MainWindow window;
window.resize( 800, 400 );
SamplingThread samplingThread;
samplingThread.setFrequency( window.frequency() ); // window.frequency()'s type is double
samplingThread.setAmplitude( window.amplitude() ); // window.amplitude()'s type is double
samplingThread.setInterval( window.signalInterval() ); // window.signalInterval()'s type is double
window.connect( &window, SIGNAL( frequencyChanged( double ) ),
&samplingThread, SLOT( setFrequency( double ) ) );
window.connect( &window, SIGNAL( amplitudeChanged( double ) ),
&samplingThread, SLOT( setAmplitude( double ) ) );
window.connect( &window, SIGNAL( signalIntervalChanged( double ) ),
&samplingThread, SLOT( setInterval( double ) ) );
window.show();
samplingThread.start();
window.start();
bool ok = app.exec();
samplingThread.stop();
samplingThread.wait( 1000 );
return ok;
}
above's window.start() is equal to plot->start().
and i can not find the linkage between plot object and samplingthread object.
Can anyone explain this part for me?
signal/slot between 2 threads ends up as QEvents in the Qt event queue. Considering, that the sampling thread creates values very fast ( f.e every 10ms ) it is obvious, that this is no option.
So there needs to be a shared buffer, where writing/reading is guarded by a mutex - this is what SignalData is.
The bridge between SignalData and the curve is done by CurveData, that implements a given API. If you like you could compare CurveData with the idea of QAbstractItemModel.
But the oscilloscope application is more a demo than an example. It shows what is possible with very low CPU usage ( runs nicely even on the Pi with reasonable CPU usage ) with using a couple of tricks.
There is a use of a curious singleton pattern based on the class SignalData.
First of all, the singleton :
class CurveData: public QwtSeriesData<QPointF>
{
public:
const SignalData &values() const;
SignalData &values();
...
};
const SignalData &CurveData::values() const
{
return SignalData::instance();
}
QPointF CurveData::sample( size_t i ) const
{
return SignalData::instance().value( i );
}
On one side
Plot::Plot( QWidget *parent ):
{
d_curve = new QwtPlotCurve();
...
d_curve->setData( new CurveData() ); //singleton being used inside each curvedata
...
}
And on the other
void SamplingThread::sample( double elapsed )
{
if ( d_frequency > 0.0 )
{
const QPointF s( elapsed, value( elapsed ) );
SignalData::instance().append( s ); //singleton being used
}
}
I'll refrain from using this example as it is.
I would like to create QPushButton which gives an opportunity to press not often than one time in 200 msec. When I use func sleep(200) all GUI thread will stop.
Waiting for your ideas!
Thankx!
Something like this:
class Controller : public QObject
{
// ...
private:
QPointer< QPushButton > btn;
private slots:
void onClicked();
void enableClick();
};
Controller::onClicked()
{
disconnect( btn, SIGNAL( clicked() ), SLOT( onClicked() ) );
QTimer::singleShot( 200, this, SLOT( enableClick() ) );
// Optional
btn->setEnabled( false );
}
Controller::enableClick()
{
connect( btn, SIGNAL( clicked() ), SLOT( onClicked() ) );
// Optional
btn->setEnabled( true );
}
i Guys ,
Qt Version 5.1
I am working on existing qt project and I failed to understand why when I click on button , it fails to fire up function in slot
What I am trying to do is my Qt application connects to the network and displays all the machine with their mac address using QTreewidget. This part works fine
my nest task is to select the mac-adrress from the QTree object created in above step AND then create a pushbutton to start the upgrade process on that machine.
Below is my source code
1) main.cpp
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Regular_Work w;
w.show();
return a.exec();
}
2) regular_work.cpp
Regular_Work::Regular_Work(QWidget *parent)
: QMainWindow(parent),macAdd_tree_p_( 0 ), reg_upgrade_button_(0),
box_table_p_( 0 ),
udp_listner_p_( 0 )
{
ui.setupUi(this);
// Create the main view
create_main_view( this, macAdd_tree_p_, box_table_p_ , reg_upgrade_button_);
init(); // this function upgradels other signals and slots from other class to find the network and upgradel the slots which displays teh tree view of mac address connected to a network.
create_menu_actions();
}
Regular_Work::~Regular_Work()
{
}
// this function is called from another slot when itemClicked signal is received from QTreeWidgetItem
void Regular_Work::set_reg_upgrade_button_visible( Regular_Work* main_p )
{
QPushButton* reg_upgrade_button = new QPushButton ("Regular_upgradei");
reg_upgrade_button->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding ) ;
QWidget* centralWidget = new QWidget( main_p );
centralWidget->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
QHBoxLayout* layout = new QHBoxLayout(centralWidget);
layout->addWidget( reg_upgrade_button_ );
main_p->setCentralWidget(centralWidget);
reg_upgrade_button->setVisible( true );
connect(reg_upgrade_button, SIGNAL( clicked() ), main_p, SLOT( start_Work( "start Work" ) ) ); // this is teh problem ?
}
void Regular_Calibration::start_Work( const QString& error_message )
{
QMessageBox::information( this,
tr( "Push button works " ),
error_message );
}
Thanks a lot for the help
Your problem lies in the fact that you're trying to pass a parameter value in the connect statement. This can not work: -
SLOT( start_Work( "start Work" ) )
What you need to do is to create a slot that matches the arguments of the clicked() signal. Then in that slot function, you can call your start_Work("start Work") function. Something like this: -
class Regular_Work : public QMainWindow
{
Q_OBJECT
private:
void start_Work(const QString&);
private slots:
void ReceiveButtonClicked();
};
void RegularWork::ReceivedButtonClicked()
{
start_Work("start Work");
}
Connect the signal and slots: -
connect(reg_upgrade_button, SIGNAL( clicked() ), main_p, SLOT( ReceiveButtonClicked()));
If you use Qt 5, you can use the new connection syntax: -
connect(reg_upgrade_button, &QPushButton::clicked, main_p, &RegularWork::ReceiveButtonClicked);
This has the advantage of telling you if there's a problem at compile time, as well as taking pointers to functions, so you don't specify any arguments for the functions.
I want to draw a grid (series of lines) when I click the draw button, and I want them to clear when I click the clear button.
I got the grid to appear as a standalone program, but I cannot figure out how to combine it with QPushButton.
I get the following message when clicking on the Draw button while the program is running.
"QPainter::begin: Paint device returned engine == 0, type: 1
QPainter::setPen: Painter not active"
Thank you
#include <QtGui>
#include <QPainter>
#include "myqtapp.h"
// including <QtGui> saves us to include every class user, <QString>, <QFileDialog>,...
myQtApp::myQtApp(QWidget *parent)
{
setupUi(this); // this sets up GUI
// signals/slots mechanism in action
connect( pushButton_draw, SIGNAL( clicked() ), this, SLOT( draw() ) );
connect( pushButton_clear, SIGNAL( clicked() ), this, SLOT( clear() ) );
connect( pushButton_about, SIGNAL( clicked() ), this, SLOT( about() ) );
}
void myQtApp::draw()
{
//draw the grid
int lineSpacing(30),// line spacing in pixels
numberOfLines;
QPen pen(Qt::black, 2, Qt::SolidLine);
QPainter painter(this);
painter.setPen(pen);
//Grid takes up at most a 400x400 area starting at (right 150, down 50) from upper left
numberOfLines = 400/lineSpacing; //Round down grid size to fit in 400x400
for(int i = 0; i<numberOfLines; i++){
painter.drawLine(150, 50+i*lineSpacing, 150+(numberOfLines-1)*lineSpacing, 50+i*lineSpacing);
painter.drawLine(150+i*lineSpacing, 50, 150+i*lineSpacing, 50+(numberOfLines-1)*lineSpacing );
}
}
The problem you are having is because you are trying to draw on the UI using QPainter outside of the paintEvent() call of a widget - from the Qt docs :
The common use of QPainter is inside a widget's paint event: Construct
and customize (e.g. set the pen or the brush) the painter. Then draw.
Remember to destroy the QPainter object after drawing.
If you try and draw on the widget outside of the paintEvent() call, results are unpredictable.
The correct way to do this would be something like this:
// myQtApp.h
class myQtApp : public QWidget
{
Q_OBJECT
public:
myQtApp(QWidget *parent = 0); // Constructor as you have
protected:
void paintEvent(QPaintEvent *event); // This is re-implemented from QWidget
protected slots:
void draw();
private:
bool drawTheLines;
}
and
// myQtApp.cpp
void myQtApp::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
if(drawTheLines)
{
// Do the drawing here - as in your current draw() function
}
QWidget::paintEvent(event); // call the base class so everything else is drawn OK
}
void draw();
{
drawTheLines = true;
update(); // This forces a repaint of the widget with paintEvent()
}
I am just learning Qt and have a very basic question.
If there is a (function scope) variable in a slot, and the slot is called multiple times, each time before the last call has returned (is this even possible?), will the variable be overwritten each time? In the sense that, if the slot is called before the previous run has returned, wouldn't that cause errors?
Thanks.
Yes if the calls are made from different threads AND you are using direct connection.
If you use queued connections then the slot calls will be performed one after another on event loop which is ran on the thread your receiving object belongs to. (edit thanks to Idan K comment).
Checkout Signal and slots's queued connection or QMutexLocker to solve your problem.
If you truly use function scope variables, then it shouldn't matter. Example:
class WheelSpinner : public QThread
{
Q_OBJECT;
public:
WheelSpinner( QObject* receiver, const char* slot )
{
connect( this, SIGNAL( valueChanged( int ) ), receiver, slot,
Qt::DirectConnect );
}
void run()
{
for ( int i = 0; i < 100000; ++i )
{
emit ( valueChanged( i ) );
}
}
public signals:
void valueChanged( int value );
};
class ProgressTracker : public QObject
{
Q_OBJECT;
public:
ProgressTracker() { }
public slots:
void updateProgress( int value )
{
// While in this function, "value" will always be the proper
// value corresponding to the signal that was emitted.
if ( value == 100000 )
{
// This will cause us to quit when the *first thread* that
// emits valueChanged with the value of 100000 gets to this point.
// Of course, other threads may get to this point also before the
// program manages to quit.
QApplication::quit();
}
}
};
int main( int argc, char **argv )
{
QApplication app( argc, argv );
ProgressTracker tracker;
WheelSpinner spinner1( &tracker, SLOT( updateProgress( int ) ) );
WheelSpinner spinner2( &tracker, SLOT( updateProgress( int ) ) );
WheelSpinner spinner3( &tracker, SLOT( updateProgress( int ) ) );
spinner1.run();
spinner2.run();
spinner3.run();
return ( app.exec() );
}
As long as the function is reentrant there is no problem.