Page 1 of 2 12 LastLast
Results 1 to 20 of 21

Thread: Doubts related with QPainter & QVector

  1. #1
    Join Date
    Nov 2008
    Location
    Caracas - Venezuela
    Posts
    29
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    3

    Red face Doubts related with QPainter & QVector

    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:
    Qt Code:
    1. public:
    2. QVector<int> xpuntos;
    3. QVector<int> ypuntos;
    To copy to clipboard, switch view to plain text mode 
    In the cpp:
    Qt Code:
    1. r,paso, itemax,and i (defined).
    2.  
    3. for(r=0.0;r<=4.0;r+=paso){
    4. i=0;
    5. x0=qrand();
    6. x0/=RAND_MAX;
    7. while(i<itemax){
    8. x=r*x0*(1.0-x0);
    9. //if( (x-x0)<epsilon){
    10. // painter.drawPoint((int)(r*450.0/4.0),(int)(0-(x*450)));
    11. // break;
    12. //}
    13. x0=x;
    14. i++;
    15. if(i>250){
    16. xpuntos.append((int)(r*450.0/4.0));
    17. ypuntos.append((int)(0-(x*450)));
    18. }
    19. }
    20. }
    21. for(i=0;i<xpuntos.size();i++){
    22. painter.drawPoint(xpuntos.at(i),y.puntos.at(i)); //it could no determine type ....
    23. }
    To copy to clipboard, switch view to plain text mode 
    The error is Qt doesn't recognize the type. why it cant determine type?

    About QVector:
    in the .h file:
    Qt Code:
    1. QVector<QPoints> puntos;
    To copy to clipboard, switch view to plain text mode 
    In the cpp:
    My idea was inserting calculated data
    i have 100 points with positions double x, double y.For example:
    Qt Code:
    1. x=0.0, y=1;
    2. for(i=0;i<itmax;i++){
    3. x+=0.01
    4. y=C*y0*(1.0-y0);
    5. //Now the question... how I could add correctly this values to the QPoints vector?. My idea was:
    6. puntos.append( puntos.setX( (int) x, puntos.setY( (int) y) );
    7. }
    To copy to clipboard, switch view to plain text mode 
    It doesn't worked.... any other idea?
    Last edited by j0rt4g4; 2nd December 2008 at 05:31.

  2. #2
    Join Date
    Jan 2006
    Location
    Knivsta, Sweden
    Posts
    153
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11
    Thanks
    30
    Thanked 13 Times in 12 Posts

    Default Re: Doubts related with QPainter & QVector

    try

    Qt Code:
    1. QVector<QPoint> puntos;
    2.  
    3. ...
    4.  
    5. puntos.append(QPoint(int(x),int(y)));
    To copy to clipboard, switch view to plain text mode 

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

  3. #3
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts

    Default Re: Doubts related with QPainter & QVector

    Quote Originally Posted by j0rt4g4 View Post
    Qt Code:
    1. painter.drawPoint(xpuntos.at(i),y.puntos.at(i)); //it could no determine type ....
    To copy to clipboard, switch view to plain text mode 
    Notice "y.puntos" vs. "ypuntos". Next time please paste the error message.
    J-P Nurmi

  4. #4
    Join Date
    Nov 2008
    Location
    Caracas - Venezuela
    Posts
    29
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    3

    Default Re: Doubts related with QPainter & QVector

    And to access that "QVector<QPoints>" this will be the way?
    Qt Code:
    1. for(i=0;i<puntos.size(); i++){
    2. painter.drawPoint(puntos.at(i));
    3. }
    To copy to clipboard, switch view to plain text mode 
    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
    Attached Files Attached Files

  5. #5
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts

    Default Re: Doubts related with QPainter & QVector

    Quote Originally Posted by j0rt4g4 View Post
    And to access that "QVector<QPoints>" this will be the way?
    Qt Code:
    1. for(i=0;i<puntos.size(); i++){
    2. painter.drawPoint(puntos.at(i));
    3. }
    To copy to clipboard, switch view to plain text mode 
    Yet better using QPainter::drawPoints():
    Qt Code:
    1. painter.drawPoints(&puntos.at[0], puntos.count());
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  6. #6
    Join Date
    Nov 2008
    Location
    Caracas - Venezuela
    Posts
    29
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    3

    Default Re: Doubts related with QPainter & QVector

    Quote Originally Posted by jpn View Post
    Yet better using QPainter::drawPoints():
    Qt Code:
    1. painter.drawPoints(&puntos.at[0], puntos.count());
    To copy to clipboard, switch view to plain text mode 
    It can't be just like that. I tried:
    Qt Code:
    1. for(long int i=0;i<250;i++){
    2. painter.drawPoints(&puntos.at(i), puntos.count());
    3. //painter.drawPoint(i,50);
    4. }
    To copy to clipboard, switch view to plain text mode 


    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:
    Qt Code:
    1. QPainter painter(widget);
    2. painter.setPen(Qt::white);
    3. QBrush brush(1);
    4. brush.setColor(QColor::fromRgbF(1,1,1,1));
    5. painter.setBrush(brush);
    6. painter.drawRect(0,0,450,450); //draw a white rect
    To copy to clipboard, switch view to plain text mode 
    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.
    Attached Files Attached Files
    Last edited by j0rt4g4; 5th December 2008 at 06:12.

  7. #7
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts

    Default Re: Doubts related with QPainter & QVector

    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:
    Qt Code:
    1. for(i=0;i<puntos.size(); i++){
    2. painter.drawPoint(puntos.at(i));
    3. }
    To copy to clipboard, switch view to plain text mode 
    and
    Qt Code:
    1. painter.drawPoints(&puntos.at[0], puntos.count());
    To copy to clipboard, switch view to plain text mode 
    The latter is just more efficient.
    J-P Nurmi

  8. #8
    Join Date
    Nov 2008
    Location
    Caracas - Venezuela
    Posts
    29
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    3

    Default Re: Doubts related with QPainter & QVector

    Quote Originally Posted by jpn View Post
    Why "it can't be like that"?
    Qt Code:
    1. for(i=0;i<puntos.size(); i++){
    2. painter.drawPoint(puntos.at(i));
    3. }
    To copy to clipboard, switch view to plain text mode 
    and
    Qt Code:
    1. painter.drawPoints(&puntos.at[0], puntos.count());
    To copy to clipboard, switch view to plain text mode 
    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:
    Qt Code:
    1. painter.drawPoints(&puntos.at(0), puntos.count());
    To copy to clipboard, switch view to plain text mode 

    If but I got the problem If I use:
    Qt Code:
    1. QPainter painter(this) // it draws in the whole windows
    2. QPainter painter(widget) // it doesn't draws at all
    To copy to clipboard, switch view to plain text mode 
    Why is happening this?. I have no clue
    Last edited by j0rt4g4; 5th December 2008 at 12:56.

  9. #9
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts

    Default Re: Doubts related with QPainter & QVector

    Quote Originally Posted by j0rt4g4 View Post
    If but I got the problem If I use:
    Qt Code:
    1. QPainter painter(this) // it draws in the whole windows
    2. QPainter painter(widget) // it doesn't draws at all
    To copy to clipboard, switch view to plain text mode 
    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.
    J-P Nurmi

  10. #10
    Join Date
    Nov 2008
    Location
    Caracas - Venezuela
    Posts
    29
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    3

    Default Re: Doubts related with QPainter & QVector

    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.

  11. #11
    Join Date
    Nov 2008
    Location
    Caracas - Venezuela
    Posts
    29
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    3

    Default Re: Doubts related with QPainter & QVector

    Well I will keep my doubt... And check if in other forum give me an answer... thanks

  12. #12
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts

    Default Re: Doubts related with QPainter & QVector

    Quote Originally Posted by j0rt4g4 View Post
    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.
    J-P Nurmi

  13. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Doubts related with QPainter & QVector

    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.

  14. #14
    Join Date
    Nov 2008
    Location
    Caracas - Venezuela
    Posts
    29
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    3

    Default Re: Doubts related with QPainter & QVector

    Quote Originally Posted by jpn View Post
    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).
    Last edited by j0rt4g4; 9th December 2008 at 18:35.

  15. #15
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts

    Default Re: Doubts related with QPainter & QVector

    Quote Originally Posted by wysota View Post
    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!

    Quote Originally Posted by j0rt4g4 View Post
    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.
    J-P Nurmi

  16. #16
    Join Date
    Nov 2008
    Location
    Caracas - Venezuela
    Posts
    29
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    3

    Default Re: Doubts related with QPainter & QVector

    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):
    Qt Code:
    1. Class mwidget: public QWidget {
    2. Q_OBJECT
    3. public:
    4. class mwidget(QWidget *parent = 0);
    5. protected:
    6. void paintEvent(QPaintEvent *event);
    7. };
    To copy to clipboard, switch view to plain text mode 

    (mwidget.cpp):
    Qt Code:
    1. mwidget::mwidget(QWidget *parent) :
    2. QWidget(parent)
    3. {
    4. }
    5.  
    6. mwidget::paintEvent( QPaintEvent * )
    7. {
    8. QPainter painter( this );
    9. }
    To copy to clipboard, switch view to plain text mode 

    This was the mistakes I got:
    Qt Code:
    1. In file included from ui_mlogistico.h:31,
    2. from aread.h:6,
    3. from aread.cpp:2:
    4. ./mwidget.h:9: error: expected `)' before '*' token
    5. In file included from aread.h:6,
    6. from aread.cpp:2:
    7. ui_mlogistico.h: In member function `void Ui_Form::setupUi(QWidget*)':
    8. ui_mlogistico.h:80: error: no matching function for call to `mwidget::mwidget(QWidget*&)'
    9. ./mwidget.h:5: note: candidates are: mwidget::mwidget()
    10. ./mwidget.h:5: note: mwidget::mwidget(const mwidget&)
    11. aread.cpp: In member function `void Mlog::pintar()':
    12. mingw32-make[1]: Leaving directory `C:/Documents and Settings/Administrador/Escritorio/logistico'
    13. mingw32-make[1]: *** [debug/aread.o] Error 1
    14. mingw32-make: *** [debug] Error 2
    To copy to clipboard, switch view to plain text mode 

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

  17. #17
    Join Date
    Nov 2008
    Location
    Caracas - Venezuela
    Posts
    29
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    3

    Default Re: Doubts related with QPainter & QVector

    Quote Originally Posted by jpn View Post
    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...

  18. #18
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts

    Default Re: Doubts related with QPainter & QVector

    At least you forgot to #include <QWidget> in mwidget.h.
    J-P Nurmi

  19. #19
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Doubts related with QPainter & QVector

    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.

  20. #20
    Join Date
    Nov 2008
    Location
    Caracas - Venezuela
    Posts
    29
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    3

    Default Re: Doubts related with QPainter & QVector

    Quote Originally Posted by wysota View Post
    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.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.