I created a Qwidget, Form_temp, that draw lines based on an array of data created in the parent widget MainWindow. The issue I face is the data I send from the MainWindow to Form_temp via the slot send_data is not seen by other functions in Form_temp. (paintEvent).
I am not able to figure out the loop hole. I added few Debug points to validate that the data arrives in Form_temp.
Here is the code with some explanation. I did this with QTCreator Please help, I spend few days on this and am not able to move forward.
Another question: paintEven happen each time the user moves the mouse or another widget is updating its view (e.g I have a label showing the time). I'd like to filter the QPaintevens, I just want an update when the data changes. Is there a better way to do this than what I have coded?
Qwidget : header
#ifndef FORM_TEMP_H
#define FORM_TEMP_H
#include <QWidget>
#include <QDebug>
namespace Ui { class Form_temp; }
class Form_temp : public QWidget {
Q_OBJECT
public:
QPainter *p;
void paintEvent(QPaintEvent*);
explicit Form_temp(QWidget *parent = 0);
~Form_temp();
void send_data (int *parray, int asize);
int array[48];
int size;
bool myupdate;
private:
Ui::Form_temp *ui;
};
#endif // FORM_TEMP_H
Qwidget : core
#include "form_temp.h"
#include "ui_form_temp.h"
#include <cstdlib>
#include <QPainter>
#include <QDebug>
Form_temp::Form_temp(QWidget *parent) : QWidget(parent), ui(new Ui::Form_temp) {
myupdate = false;
ui->setupUi(this);
}
Form_temp::~Form_temp() { delete ui; }
void Form_temp::paintEvent(QPaintEvent*) {
qDebug("Paintevent occurs");
if (myupdate) { // Event happen whenever I move the mouse,
// I only want an update when data changes.
p = new QPainter(this);
QPen pen(Qt::green, 3, Qt::DashDotLine, Qt::RoundCap, Qt::RoundJoin);
p->setPen(pen);
p->setRenderHint(QPainter::Antialiasing,true);
qDebug()<< "this size" <<size;
for (int i= 0; i< size; ++i) {
qDebug()<< "array[i" <<i <<"]="<< array[i];
}
[...]
p->drawLine(x1,y1,x2,y2);
[...]
}
myupdate = false;
}
void Form_temp::send_data (int *parray, int asize){
size = asize;
for (int i= 0; i< asize; ++i) {array[i] = parray[i];}
myupdate = true; // make sure the event will update the drawing
this->update(); // create a Qpaint Event
qDebug()<< size; // print the data so we know we are passing data correctly
for (int i= 0; i< asize; ++i) {
qDebug()<< "array[i" <<i <<"]="<< array[i];
}
}
MainWindow: header
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QtGui>
#include "gpio.h"
#include "form_temp.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
//QPropertyAnimation *m_ani ;
//QPropertyAnimation *m_ani2 ;
Form_temp *temp_graph;
[...]
#endif // MAINWINDOW_H
MainWindow: core
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow){
// Start UI
ui->setupUi(this);
temp_graph = new Form_temp;
startTimer(1000); // get timer event every sec.
}
void MainWindow::timerEvent(QTimerEvent *event) {
int data[]= {1,2,3};
temp_graph->send_data(data, 3);
}
[...]
Thanks for reading. Sebastien.