Ok, here is the minimal program with the basic architecture I am using. After building in Momentics and porting to BeagleBoard-xM I get the same problem → gradient is not working well/correct.
The kommunikation class is for the communication with the rs232 interface which I use to read data from my laptop. This class is running in a seperate thread.
For the communication I send first a value for the switch-loop (“100” in this case) and then the value (for design change “0” and “1” at the moment).
This value is then emitted via a signal to the main-thread and received from the function “setDesign()” in controller.cpp
main.cpp
#include "controller.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Controller *control = new Controller;
control->view->show();
return a.exec();
}
controller.h
#ifndef CONTROLLER_H
#define CONTROLLER_H
#include <QWidget>
#include <QtGui/QApplication>
#include <QtGui/QGraphicsView>
#include <QThread>
#include <QtCore>
#include "kommunikation.h"
class GraphicsView : public QGraphicsView
{
public:
GraphicsView(QGraphicsScene *scene) : QGraphicsView(scene)
{}
protected:
virtual void resizeEvent(QResizeEvent *event)
{}
};
//------------------------------------------------------------------
//------------------------------------------------------------------
class Controller : public QWidget
{
Q_OBJECT
public:
explicit Controller(QWidget *parent = 0);
GraphicsView *view;
Kommunikation *worker;
public slots:
void setDesign(int);
private:
QGraphicsScene *scene;
QThread *thread;
int design;
QLinearGradient *gradient;
QBrush *brushBlue;
QLinearGradient *gradientRed;
QBrush *brushRed;
};
#endif // CONTROLLER_H
controller.cpp
#include "controller.h"
Controller::Controller(QWidget *parent) :
QWidget(parent), design(0)
{
scene = new QGraphicsScene(-400, -300, 800, 600);
view = new GraphicsView(scene);
thread = new QThread();
worker = new Kommunikation();
view->setRenderHint(QPainter::Antialiasing);
view->setBackgroundBrush(QBrush(Qt::black));
view->setFrameStyle(QFrame::NoFrame);
view->showFullScreen();
gradient = new QLinearGradient(0,scene->sceneRect().top(), 0, scene->sceneRect().bottom());
gradient->setColorAt(0, QColor(Qt::darkBlue));
gradient->setColorAt(0.3, QColor(Qt::black));
gradient->setColorAt(0.7, QColor(Qt::black));
gradient->setColorAt(1, QColor(Qt::darkBlue));
brushBlue = new QBrush(*gradient);
gradientRed = new QLinearGradient(0,scene->sceneRect().top(), 0, scene->sceneRect().bottom());
gradientRed->setColorAt(0, QColor(Qt::darkRed));
gradientRed->setColorAt(0.3, QColor(Qt::black));
gradientRed->setColorAt(0.7, QColor(Qt::black));
gradientRed->setColorAt(1, QColor(Qt::darkRed));
brushRed = new QBrush(*gradientRed);
setDesign(0);
worker->moveToThread(thread);
QObject::connect(thread, SIGNAL(started()), worker, SLOT(process()));
QObject::connect(worker, SIGNAL(finished()), thread, SLOT(quit()));
QObject::connect(worker, SIGNAL(finished()), worker, SLOT(deleteLater()));
QObject::connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
QObject::connect(worker, SIGNAL(setDesign(int)), this, SLOT(setDesign(int)));
thread->start();
}
void Controller::setDesign(int b)
{
design = b;
switch(design)
{
case 0:
view->setBackgroundBrush(*brushBlue);
break;
case 1:
view->setBackgroundBrush(*brushRed);
break;
}
scene->update();
}
kommunikation.h
#ifndef KOMMUNIKATION_H
#define KOMMUNIKATION_H
#include <QObject>
#include <QDebug>
#include <termios.h>
#include <fcntl.h>
#include <string.h>
#include <process.h>
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/neutrino.h>
class Kommunikation : public QObject
{
Q_OBJECT
public:
Kommunikation();
~Kommunikation();
public slots:
int portInit();
void process();
signals:
void finished();
void eror(QString er);
void setDesign(int);
private:
int choice;
int value;
int count;
int speed;
int fd;
char data[3];
char intArr[4];
int intData;
struct termios raw;
};
#endif // KOMMUNIKATION_H
kommunikation.cpp
#include "kommunikation.h"
Kommunikation::Kommunikation()
{}
Kommunikation::~Kommunikation()
{}
int Kommunikation::portInit()
{
//---------------------------------------------------
//---------------------------------------------------
// Open the port
if ((fd = open ("/dev/ser1", O_RDWR)) == -1)
{
fprintf(stderr, "Error with open() on /dev/ser1. Make sure exists.\n");
perror (NULL);
exit(EXIT_FAILURE);
}
// Get the attributes
if (tcgetattr( fd, &raw))
{
close( fd );
return -1;
}
// Set input baud rate
speed = B115200;
cfsetispeed(&raw, speed);//inputspeed
cfsetospeed(&raw, speed);//outputspeed
raw.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON );
raw.c_oflag &= ~(OPOST);
raw.c_cflag &= ~(CSIZE|IHFLOW|OHFLOW);
raw.c_cflag |= CS8 | CREAD | CLOCAL;
raw.c_cflag &= ~CSTOPB;
raw.c_cflag &= ~PARENB;
raw.c_lflag &= ~(ECHO | ICANON | ISIG | ECHOE | ECHOK | ECHONL | IEXTEN);
raw.c_cc[VMIN] = 1;
raw.c_cc[VTIME] = 0;
if ( tcsetattr( fd, TCSADRAIN, &raw ) == -1 ) // CHECK FOR -1 failure here - this is the actual "set" and it must succeed on nothing may have changed
{
fprintf(stderr, "Error with tcsetattr() on /dev/ser1.\n");
perror (NULL);
exit(EXIT_FAILURE);
}
return fd;
}
void Kommunikation::process()
{
choice = 0;
value = 0;
count = 0;
fd = 0;
fd = portInit();
while(1)
{
for(int i = 0; i < sizeof(data); i++)
{data[i] = 0;}
for(int i = 0; i < sizeof(intArr); i++)
{intArr[i] = 0;}
count = read(fd, data, 3);
if(data[1] == 0){value = ((int)data[2]);}
if(data[1] == 1){value = ((int)data[2]) + 127;}
if(data[1] == 2){value = ((int)data[2]) + 254;}
if(data[1] == 3){value = ((int)data[2]) + 381;}
if(data[1] == 4){value = ((int)data[2]) + 508;}
if(data[1] == 5){value = ((int)data[2]) + 635;}
if(data[1] == 6){value = ((int)data[2]) + 762;}
if(data[1] == 7){value = ((int)data[2]) + 889;}
if(data[1] == 8){value = ((int)data[2]) + 1016;}
switch(value)
{
case 100:
count = read(fd, intArr, 2);
if(intArr[0] == 0){intData = ((int)intArr[1]);}
if(intArr[0] == 1){intData = ((int)intArr[1]) + 127;}
emit setDesign(intData);
break;
}
}
}