Results 1 to 11 of 11

Thread: Painting problem

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    May 2007
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Painting problem

    Thanks to all for the replies.

    The code is compiled in Qt 4 only.

    Marcel,

    I had actually created a paintEvent in the PBS class before posting my question, but it didn't work either. The code is below. Nothing is painted when this event is called either, but at least I don't get the original error message. The program I'm trying to port works quite nicely in Qt 3, and displays both spectrum and waterfall displays. I've never noticed any flickering. I have no problems with rewriting some of the code, if I can ever get the painting problem solved. Seems like I'm still missing something here ...

    Note that nothing is painted anywhere on the screen when the paintEvent in PBS is executed.

    Code follows ....................................

    #include "pbs.h"
    #include "main_widget.h"

    PBS::PBS(QWidget *parent) : QWidget(parent)
    {
    setMouseTracking( true );
    }

    void PBS::mouseMoveEvent( QMouseEvent *e )
    {
    emit movement( e->x() );
    }

    void PBS::mousePressEvent( QMouseEvent *e )
    {
    x0 = e->x();

    if ( e->button() == Qt::LeftButton )
    emit set_lower_pb( x0 );
    if ( e->button() == Qt::RightButton )
    emit set_upper_pb( x0 );
    }
    void PBS:aintEvent( QPaintEvent * )
    {
    printf("PBS paintEvent() \n");

    QPainter p;
    p.begin(this);
    // p.eraseRect( 0, 0, this->width(), this->height() );
    p.setPen( Qt::yellow );
    p.drawLine(252, 50, 252, 190);
    p.drawLine(4,115,34,115);
    p.end();


    }

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Painting problem

    Yes, I believe you don't see anything on the screen
    This is because you are painting off-widget... You first must take a look at what rect() returns and draw everything relative to rect().topLeft and such that everything will fit in the widget rect.

    My guess is that your widget(PBS, possibly the others) doesn't have a minimum size. You should reimplement minimumSize and sizeHint to return the minimum size and the preferred size for your widget(s).

    Or, if you know these sizes, you can just set them in the widget constructor. Not setting them, your widget will have a size of (-1, -1).

    What errors did you get?

    Try getting the code between CODE tags, because it is very hard top read as simple text.


    regards

  3. #3
    Join Date
    May 2007
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Painting problem

    Marcel,

    Thanks for your patience.

    I get no errors when running the last piece of code I sent; it just doesn't paint. I printed out this->width() and this->height just before the call to eraseRect, and it shows the size of pbs to be (2, 109, 644, 15). I removed the line that was clearly out of the print area, compiled, and ran it again. Still no painting.

    WRT the size of the widgets, I would think that's set in updateLayout(). I didn't write the original code, so I'm not sure why the author chose to implement it that way.

    I'll have many changes in arithmetic to make in the program, but I feel that if I can get the toy I posted to run I'll be able to conquer the rest.

    Thanks.

    Cheers,
    Mel

  4. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Painting problem

    Instead of drawing the lines try p.fillRect( rect(), Qt::red ) ;
    See if anything is being painted now. Perhaps you need to set a width for the pen.

    regards

  5. #5
    Join Date
    May 2007
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Painting problem

    Marcel,

    p.fillRect( rect(), Qt::red ) ;

    filled pbs with a bright red.

    The pen width is set to 0, so a 1 pixel width line should be drawn.

    I removed the background from pbs, but still no line.

    Thus far the only thing I've been able to display on pbs is your fillRect suggestion.
    I also tried
    p.fillRect(4,115,34,6,Qt::red);
    which should have filled a small rectangle, but nothing happened.

    It's probably late in your area, but do you have any last suggestions for today?

    Thanks again.

    Cheers,
    Mel

  6. #6
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Painting problem

    OK.
    A painter draws in paint device (widget in this case ) local coordinates.
    Meaning that when you used my previous suggestion, something got filled, but not the right thing .

    Thus, everything must be drawn relative to (0,0).
    Calling p.fillRect( 0, 0, size().width(), size().height(), Qt:red ) will fill the ENTIRE widget.
    For what you were trying to achieve call p.fillRect( 0, 0, 30, 6, Qt:red ). This will fill a small rect in the top left corner of the widget.

    To be sure just do a drawRect( 0, 0, size().width(), size().height() ) - draw a frame around the widget.

    If this works ( and it will ), draw everything relative to (0, 0 ) - lines, etc.

    It's probably late in your area, but do you have any last suggestions for today?
    It's 11 PM, not so late.

    Regards

  7. #7
    Join Date
    May 2007
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Painting problem

    Sigh ... Of course it's relative! I knew there was some fundamental point I was missing! I adjusted my numbers, and painted the rectangle and the line I was trying to paint.

    I can't thank you enough for your help, Marcel. I have a lot of work still ahead, but your help has removed a big roadblock. I put my question out to this group, because they had advertised a 95% success rate in solving users' problems. Mine can certainly be added to the success list thanks to your efforts.

    Thanks again for your time and patience.

    Cheers,
    Mel

  8. #8
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Painting problem

    Happy to be of some help!

    Regards

Similar Threads

  1. Painting problem
    By ScoOteR in forum Qt Programming
    Replies: 5
    Last Post: 11th March 2007, 11:03
  2. Painting Problem
    By shyam prasad in forum Qt Programming
    Replies: 3
    Last Post: 5th February 2007, 14:07
  3. QTimer problem ... it runs but never triggs
    By yellowmat in forum Newbie
    Replies: 4
    Last Post: 4th July 2006, 12:54
  4. Problem with bitBlt
    By yellowmat in forum Newbie
    Replies: 1
    Last Post: 5th April 2006, 14:08
  5. Replies: 16
    Last Post: 7th March 2006, 15:57

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.