PDA

View Full Version : Doubts related with QPainter & QVector



j0rt4g4
2nd December 2008, 03:45
Well I have 2 questions but I have really no idea of what I can do to solve this.

About QPainter:
in the .h file:


public:
QVector<int> xpuntos;
QVector<int> ypuntos;

In the cpp:


r,paso, itemax,and i (defined).

for(r=0.0;r<=4.0;r+=paso){
i=0;
x0=qrand();
x0/=RAND_MAX;
while(i<itemax){
x=r*x0*(1.0-x0);
//if( (x-x0)<epsilon){
// painter.drawPoint((int)(r*450.0/4.0),(int)(0-(x*450)));
// break;
//}
x0=x;
i++;
if(i>250){
xpuntos.append((int)(r*450.0/4.0));
ypuntos.append((int)(0-(x*450)));
}
}
}
for(i=0;i<xpuntos.size();i++){
painter.drawPoint(xpuntos.at(i),y.puntos.at(i)); //it could no determine type ....
}

The error is Qt doesn't recognize the type. why it cant determine type?

About QVector:
in the .h file:


QVector<QPoints> puntos;
In the cpp:
My idea was inserting calculated data
i have 100 points with positions double x, double y.For example:


x=0.0, y=1;
for(i=0;i<itmax;i++){
x+=0.01
y=C*y0*(1.0-y0);
//Now the question... how I could add correctly this values to the QPoints vector?. My idea was:
puntos.append( puntos.setX( (int) x, puntos.setY( (int) y) );
}

It doesn't worked.... any other idea?

drhex
2nd December 2008, 14:54
try


QVector<QPoint> puntos;

...

puntos.append(QPoint(int(x),int(y)));

as for the unrecognized type, can you post the error mesage you get?

jpn
2nd December 2008, 17:59
painter.drawPoint(xpuntos.at(i),y.puntos.at(i)); //it could no determine type ....


Notice "y.puntos" vs. "ypuntos". Next time please paste the error message.

j0rt4g4
2nd December 2008, 18:03
And to access that "QVector<QPoints>" this will be the way?


for(i=0;i<puntos.size(); i++){
painter.drawPoint(puntos.at(i));
}

In archive " If I use painter.draw crashes.zip (9.6 KB)" and it doesnt paint in the "widget" ... at all...

and "Unrecogniced type.zip (10.1 k) " reflects my doubt number #1. thanks

jpn
2nd December 2008, 18:06
And to access that "QVector<QPoints>" this will be the way?


for(i=0;i<puntos.size(); i++){
painter.drawPoint(puntos.at(i));
}


Yet better using QPainter::drawPoints():


painter.drawPoints(&puntos.at[0], puntos.count());

j0rt4g4
5th December 2008, 04:35
Yet better using QPainter::drawPoints():


painter.drawPoints(&puntos.at[0], puntos.count());


It can't be just like that. I tried:

for(long int i=0;i<250;i++){
painter.drawPoints(&puntos.at(i), puntos.count());
//painter.drawPoint(i,50);
}


What I want is to generate pointsF when I click "Generar"
and to paint on the widget called "widget" when I click in Pintar.
But when I click Paint.... and I did this code for drawing on it like this:

QPainter painter(widget);
painter.setPen(Qt::white);
QBrush brush(1);
brush.setColor(QColor::fromRgbF(1,1,1,1));
painter.setBrush(brush);
painter.drawRect(0,0,450,450); //draw a white rect
It doesn't draw anything.... and how can I see if the slot "Generar" is working Correctly?... :S

I dont know if I have to make it "current()" of something like that to draw on it..
It draws when I use QPainter painter(this) but it draw in the whole windows, and I need it draws in the object called widget.

jpn
5th December 2008, 06:30
Why "it can't be like that"? There is no point to call drawPoints() in a loop, because it already takes an ARRAY of points. The whole idea of drawPoints() is the take all the points in a group so that you don't have to call drawPoint() for each and every point.

When it comes to the visible result on the screen, there is no difference between these two:


for(i=0;i<puntos.size(); i++){
painter.drawPoint(puntos.at(i));
}

and


painter.drawPoints(&puntos.at[0], puntos.count());

The latter is just more efficient.

j0rt4g4
5th December 2008, 12:52
Why "it can't be like that"?


for(i=0;i<puntos.size(); i++){
painter.drawPoint(puntos.at(i));
}

and


painter.drawPoints(&puntos.at[0], puntos.count());

The latter is just more efficient.

Thank you for aswering because there is a tiny diference between them...
If you use "[]" or "()" of course there's a diference.
If I use "[]" i got this mistake: Invalid types' <unknown types>[int]' for array suscript.
I got your point and you're rigth is more eficient do it like this:

painter.drawPoints(&puntos.at(0), puntos.count());

If but I got the problem If I use:

QPainter painter(this) // it draws in the whole windows
QPainter painter(widget) // it doesn't draws at all
Why is happening this?. I have no clue

jpn
5th December 2008, 13:34
If but I got the problem If I use:

QPainter painter(this) // it draws in the whole windows
QPainter painter(widget) // it doesn't draws at all
Why is happening this?. I have no clue
A widget can only paint on itself. It can NOT paint on other widgets. Reimplement the paintEvent() handler of THAT widget you want to paint on.

j0rt4g4
6th December 2008, 20:48
mmmm And how I can do that?
I used designer to promote this widget called "widget" so I could create .h file and .cpp file about the "writing area" but I dont know if that what you meant.

j0rt4g4
9th December 2008, 16:13
Well I will keep my doubt... And check if in other forum give me an answer... thanks

jpn
9th December 2008, 16:51
mmmm And how I can do that?
I used designer to promote this widget called "widget" so I could create .h file and .cpp file about the "writing area" but I dont know if that what you meant.
So put your painting code into that class. Reimplement the paintEvent() function of that class as usual. Don't try to open a painter on that widget in another class.

wysota
9th December 2008, 17:23
J-P, I know this is not a topic for newbies, but maybe it'll be easier for him if he uses event filters? Of course it won't change the fact that a skill to create a widget class and reimplement its paintEvent to do the drawing is essential to make any progress in Qt and he has to learn it anyway.

j0rt4g4
9th December 2008, 18:02
So put your painting code into that class. Reimplement the paintEvent() function of that class as usual. Don't try to open a painter on that widget in another class.

The fact is that i know how to use the eventfilters, because in other thread they tell me how I could do this using evenfilters. But If every time I want to draw in the widget, I think I can't use eventfilters every time the users wants to do an acction on this widget, or at least I dont know how to implement that.

That's why I thinks that must be a better way "more profesional" to do this...
And I'm trying to learn it :P (Thanks for the aswer everyone... I really apreciate your help).

jpn
9th December 2008, 18:44
J-P, I know this is not a topic for newbies, but maybe it'll be easier for him if he uses event filters?
Hush! :)


The fact is that i know how to use the eventfilters, because in other thread they tell me how I could do this using evenfilters. But If every time I want to draw in the widget, I think I can't use eventfilters every time the users wants to do an acction on this widget, or at least I dont know how to implement that.
Where painting with an event filter on an empty QWidget might be suitable, things start to get pretty ugly if you want to paint something on top of what the base class draws.

j0rt4g4
9th December 2008, 18:53
Well I promoted the widget in designer to a class named....
"mwidget" that is in the file "mwidget" so I tried to implement this but I got several mistakes in the constructor, I did what I think will be right but it's not :P

This is what i tried: (mwidget.h):

Class mwidget: public QWidget {
Q_OBJECT
public:
class mwidget(QWidget *parent = 0);
protected:
void paintEvent(QPaintEvent *event);
};

(mwidget.cpp):

mwidget::mwidget(QWidget *parent) :
QWidget(parent)
{
}

mwidget::paintEvent( QPaintEvent * )
{
QPainter painter( this );
}

This was the mistakes I got:

In file included from ui_mlogistico.h:31,
from aread.h:6,
from aread.cpp:2:
./mwidget.h:9: error: expected `)' before '*' token
In file included from aread.h:6,
from aread.cpp:2:
ui_mlogistico.h: In member function `void Ui_Form::setupUi(QWidget*)':
ui_mlogistico.h:80: error: no matching function for call to `mwidget::mwidget(QWidget*&)'
./mwidget.h:5: note: candidates are: mwidget::mwidget()
./mwidget.h:5: note: mwidget::mwidget(const mwidget&)
aread.cpp: In member function `void Mlog::pintar()':
mingw32-make[1]: Leaving directory `C:/Documents and Settings/Administrador/Escritorio/logistico'
mingw32-make[1]: *** [debug/aread.o] Error 1
mingw32-make: *** [debug] Error 2

and I will attach the code too.. I hope this information will be enough to do this.

j0rt4g4
9th December 2008, 18:56
Where painting with an event filter on an empty QWidget might be suitable, things start to get pretty ugly if you want to paint something on top of what the base class draws.

I agree :P... I think so too :P
If sometimes get pretty ugly when the class isn't the base class .... imagine if it is... :)

jpn
9th December 2008, 19:03
At least you forgot to #include <QWidget> in mwidget.h.

wysota
9th December 2008, 19:17
And it's "class", not "Class" :) Including <QPainter> and <QPaintEvent> in the implementation file will be required as well. And lose the "class" keyword before the constructor declaration.

j0rt4g4
9th December 2008, 20:18
And it's "class", not "Class" :) Including <QPainter> and <QPaintEvent> in the implementation file will be required as well. And lose the "class" keyword before the constructor declaration.

I wrote class in the .h :S but I had a mistake when I posted... I'm working on this... Thanks.

j0rt4g4
9th December 2008, 21:33
I did it... And finally WORKS...
:)

Now I got a "QVector<QPoints> puntos" that I create fill with the "generar()" function in the "main" .h and .cpp called "aread.h" and "aread.cpp".

Now I want to pass this QVector to the QPainter to draw. But QPainter is implemented in the promoted file, And It doesn't recognize any label or widget in the "main files (.h) and (.cpp).

Should I define in the promoted file a class called Mlog? and try to use the "QVector of this one" in the promoted file?:crying:

I defined the QVector as global :) I guess that was it.
But I'm still confused between the 2 files.
and How I could call a slots or widget in the main files in the promoted file...