PDA

View Full Version : Problem with drawing on the Widget



kaszewczyk
15th April 2010, 23:19
Hello,
My problem looks like this: I have two widget in first i draw lines etc and it is child widget situated in parent widget which is the main of application. I have connected those two widget with QSlider, so when i move silder the length of line drawn on child widget should change but that dont happend... i checked and the paintEvent is called on both widget but i cant see result, please healp.

Best Regards
kszewczyk

Lykurg
15th April 2010, 23:29
Without seeing the code we hardly can help you. Probably you have an error in the connection statement...

kaszewczyk
16th April 2010, 05:13
I tested connection, everything is ok becouse i pass some values from one widget to another and variables are changing so that should be ok.

Regards

kaszewczyk
17th April 2010, 09:44
Hi, sorry for duble post but i cant find what is wrong

the main window look like this:


#include "window.h"

Window::Window(QWidget *parent) : QWidget(parent){

amplitudeP = 10.0;
ampliSliderPosP = 0;

mainLay = new QHBoxLayout;
renderLay = new QVBoxLayout;
optionsLay = new QVBoxLayout;
renderGroupBox = new QGroupBox("Wykresy", this);
optionsGroupBox = new QGroupBox("Opcje", this);

oscylo = new Oscyloscop(this);
mightGraph = new SignalMight(this);

oscylo->setRenderAreaSize(400 , 400);
oscylo->resizeRenderArea();
mightGraph->setRenderAreaSize(400, 150);
mightGraph->resizeRenderArea();

renderLay->addWidget(oscylo);
renderLay->addWidget(mightGraph);
renderGroupBox->setLayout(renderLay);

ampliSilder = new QSlider(Qt::Horizontal, this);

optionsLay->addWidget(ampliSilder);

optionsGroupBox->setLayout(optionsLay);

mainLay->addWidget(renderGroupBox);
mainLay->addWidget(optionsGroupBox);
setLayout(mainLay);

connect(ampliSilder, SIGNAL(valueChanged(int)), this, SLOT(changeAmplitude(int)));
connect(this, SIGNAL(amplitudeChanget(double)), oscylo, SLOT(reciveAmplitude(double)));


}

Window::~Window(){
delete mainLay;
delete renderLay;
delete optionsLay;
delete oscylo;
delete mightGraph;
}

void Window::changeAmplitude(int _pos)
{
if(_pos > ampliSliderPosP)
amplitudeP += 10.0;
else
amplitudeP -=10.0;

ampliSliderPosP = _pos;
emit amplitudeChanget(amplitudeP);
}

void Window::paintEvent(QPaintEvent *)
{
qDebug("update okna glownego");
}


the child class look like this:


#include "oscyloscop.h"

Oscyloscop::Oscyloscop(QWidget* parent) : RenderArea(parent)
{
nullCordP = new Point2D(100, 900);
xEndP = new Point2D(900, 900);
yEndP = new Point2D(100, 100);
signal = new Signal();
amplitudeP = 10.0;

}

Oscyloscop::~Oscyloscop(){
delete nullCordP;
delete xEndP;
delete yEndP;
}

void Oscyloscop::drawCoordSystem(QPen& _pen, QPainter& _painter){
_painter.drawLine(100, 900, 900, 900);
_painter.drawLine(100, 100, 100, 900);
_painter.setFont(QFont("Tahoma", 25));
_painter.drawText(nullCordP->x-6, nullCordP->y+45, "0");
_painter.drawText(450, 980, "Czas [t]");

int xDrawPoint = 260;
int yDrawPoint = 740;
_pen.setWidth(5);
_painter.setPen(_pen);
for(int i = 0; i < 5; i++){
_painter.drawLine(xDrawPoint, nullCordP->y, xDrawPoint, nullCordP->y+10);
_painter.drawLine(nullCordP->x-10, yDrawPoint, nullCordP->x, yDrawPoint);
xDrawPoint += 160;
yDrawPoint -= 160;
}

_pen.setStyle(Qt::DotLine);
_pen.setWidth(3);
_painter.setPen(_pen);

xDrawPoint = 260;
yDrawPoint = 740;
for(int i = 0; i < 5; i++){
_painter.drawLine(xDrawPoint, nullCordP->y, xDrawPoint, nullCordP->y-800);
_painter.drawLine(nullCordP->x, yDrawPoint, nullCordP->x+800, yDrawPoint);
xDrawPoint += 160;
yDrawPoint -= 160;
}
setNumX(2);
float divX = retXscaleDigit().toFloat() / 5.0;
float divXconst = divX;
QString xPrintNum;
xDrawPoint = 260;
for(int i = 0; i < 5; i++){
xPrintNum.setNum(divX);
_painter.drawText(xDrawPoint-6, nullCordP->y+45, xPrintNum);
divX += divXconst;
xDrawPoint += 160;
}
nullCordP->x = 100;
}

void Oscyloscop::drawGraph(QPen& _pen, QPainter& _painter){
_pen.setStyle(Qt::SolidLine);
_pen.setColor(Qt::black);
_pen.setWidth(3);
_painter.setPen(_pen);


samples = signal->generateSignalSin(800, amplitudeP, 5.0, Signal::SIN_2PI5);

for(int i = 0 ; i < 800 ; ++i)
{
_painter.drawPoint(nullCordP->x, 500 + (samples[i] + 10));
++nullCordP->x;

}

nullCordP->x = 100;

}

void Oscyloscop::paintEvent(QPaintEvent* _p){
QPainter painter(this);
QPen pen;
QMatrix matrix;
pen.setWidth(10);
pen.setColor(QColor::fromRgb(104, 104, 104, 100));
pen.setStyle(Qt::SolidLine);
painter.setPen(pen);
painter.setRenderHint(QPainter::Antialiasing);
matrix.scale( retWidth() / 1000.0 , retHeight() / 1000.0 );
painter.setMatrix(matrix);
drawCoordSystem(pen, painter);
drawGraph(pen, painter);

qDebug("update oscyloscopu");
}

void Oscyloscop::reciveAmplitude(double _a)
{
amplitudeP = _a;
update();
}

and here i make y values for the graph


QVector<int> Signal::generateSignalSin(int _period, double _amplitude, double _freq, int _type)
{
double tmp;
QString str;
QStringList strList;
switch(_type)
{
case SIN_2PI5:

for(int x = 0 ; x < _period ; ++x)
{
tmp = _amplitude * sin(x * 2.0 * 3.14 * _freq);
str.setNum(tmp);
strList = str.split(".");
str = strList.last();
str.resize(4);
int mod = str.toInt();
if( tmp > 0)
{
if(mod > 5000)
y = ceil(tmp);

else
y = floor(tmp);
}
else
{
if(mod > 5000)
y = floor(tmp);
else
y = ceil(tmp);
}
samples.push_back(y);
}

break;

case SIN_2PI5_2PI10:

break;

case SIN_2PI5_02_2PI25:

break;

}// end of switch

return samples;
}


i dont know what might be wrong
//SOLVED
i didnt clear up vector with samples

Lykurg
17th April 2010, 15:00
Just a side node: All the deletes in the dtor of your main window are unnecessary. Qt will delete child widgets automatically (if the inherit QObject). Your layouts get reparent after assigning them to the central layout. So they will be deleted also.