Results 1 to 11 of 11

Thread: How to paint a widget outside paintEvent()

  1. #1
    Join Date
    Feb 2008
    Posts
    49
    Thanks
    2
    Thanked 4 Times in 1 Post

    Question How to paint a widget outside paintEvent()

    Hello, can you do me a favor?

    Platform: Qtopia 4.3.2

    question: paint a widget outside paintEvent() or paint()

    problem detail:
    I had implemented a custom list view(Inherits QListView), of course , a item delegate to paint each item the list view has.

    now, when the text of the focus index has exceeded the text rectangle, I should scroll the text.

    my thought is using a timer to scroll text, 2 or 3 characters every time the time-out event happen, but how can i paint the widget now?

    the item delegate just can be paint inside paint(), and delegate->paint() just can be called from list view's paintEvent(). that is the real problem. ~_~

    may be i can ask Qt to repaint it by using update() or repaint(). But how can i pass a flag or parameter to paintEvent() that I just want to scroll text, not paint a entireness widget.



    ^_^, Any one have the good solution for this? thank you very much.
    Last edited by wesley; 16th February 2008 at 14:15.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to paint a widget outside paintEvent()

    Quote Originally Posted by wesley View Post
    question: paint a widget outside paintEvent() or paint()
    answer: you can't.

    Quote Originally Posted by wesley View Post
    now, when the text of the focus index has exceeded the text rectangle, I should scroll the text.

    my thought is using a timer to scroll text, 2 or 3 characters every time the time-out event happen, but how can i paint the widget now?
    It's a good aproach.

    See the "Animated Items" example in Qt Model/View In Depth presentation from DevDays 2007.

  3. #3
    Join Date
    Feb 2008
    Posts
    49
    Thanks
    2
    Thanked 4 Times in 1 Post

    Default Re: How to paint a widget outside paintEvent()

    ok, thank you very much

    From the documentation, I had found out a way to implement this feature. The way is delegate emit a signal to tell list view to update specified index when a scroll timer event happen.

  4. #4
    Join Date
    Feb 2008
    Posts
    49
    Thanks
    2
    Thanked 4 Times in 1 Post

    Default Re: How to paint a widget outside paintEvent()

    and one more question like this kind of problem,

    how can i pass a custom parameter to a paint event
    I don't want to declare a class-wide parameter. I just need a flag to differentiate some thing.

    I had tried to do as follow:

    QCoreApplication:: postEvent(this, QEvent::Paint | QEvent::User );

    but Qt seems does not allow me to do this, it debug some like that
    "QPainter:: ...should no longer be called ...."
    "QPaintEngine : return 0 type = 1...",

    some things like this, I don't remerber so much.
    Last edited by wesley; 20th February 2008 at 10:59.

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to paint a widget outside paintEvent()

    Quote Originally Posted by wesley View Post
    how can i pass a custom parameter to a paint event
    QPaintEvent doesn't have any flags field or anything you could use to do that. You could try to subclass QPaintEvent and then use dynamic_cast, but that might not be reliable.

  6. #6
    Join Date
    Feb 2008
    Posts
    49
    Thanks
    2
    Thanked 4 Times in 1 Post

    Wink Re: How to paint a widget outside paintEvent()

    Quote Originally Posted by jacek View Post
    QPaintEvent doesn't have any flags field or anything you could use to do that. You could try to subclass QPaintEvent and then use dynamic_cast, but that might not be reliable.
    if i declare a class-wide flag named "bDrawAnimatedCaption",

    Qt Code:
    1. void on_AnimationTimer_timeout()
    2. {
    3. do some animation work here
    4. ... ...
    5.  
    6. bDrawAnimatedCaption = true;
    7. update(currentIndex()); //it will trigger paintEvent to update current index
    8. }
    9.  
    10. void paintEvent(QPaintEvent *event)
    11. {
    12. if(bDrawAnimatedCaption)
    13. {
    14. paint animated cation here
    15. ... ...
    16. bDrawAnimatedCaption = false;
    17. }
    18. else
    19. {
    20. QListView:: paintEvent(event);
    21. }
    22. }
    To copy to clipboard, switch view to plain text mode 

    Are these reliable ?

    or is there another solution?
    ^_^, it just a test, but I hope it can work like this, because i can control the animation more easyer on MyListView side than delegate side.
    thank you.
    Last edited by wesley; 21st February 2008 at 06:34.

  7. #7
    Join Date
    Feb 2008
    Posts
    49
    Thanks
    2
    Thanked 4 Times in 1 Post

    Default Re: How to paint a widget outside paintEvent()

    From Qt Assistant, It is say that QWidget::repaint(QRect &rect) will repaints the widget directly by calling paintEvent() immediately, unless updates are disabled or the widget is hidden.

    My list view is Inherits QListView. And I have setUpdatesEnabled to true, of course, My list View is visable on screen.

    But why it doesn't call paintEvent() when I call repaint(QRect) in a time event?

    update(QModelIndex&) is work. But repaint() seems more reliable when I need a immediate painting.

    QWidget is a further base class to QListView, is this a problem?
    Last edited by wesley; 21st February 2008 at 10:45.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to paint a widget outside paintEvent()

    Quote Originally Posted by wesley View Post
    Are these reliable ?
    Qt tends to merge multiple paint events into one, but this might be not enough. IMO it's better to assume that you don't know when the paint even arrives and avoid changing the state in paintEvent().

    Instead of a flag, you could use some variable that denotes the scroll amount. This way you don't have to care how many times and when paintEvent() was called.

  9. #9
    Join Date
    Feb 2008
    Posts
    49
    Thanks
    2
    Thanked 4 Times in 1 Post

    Question Re: How to paint a widget outside paintEvent()

    Quote Originally Posted by jacek View Post
    Qt tends to merge multiple paint events into one, but this might be not enough. IMO it's better to assume that you don't know when the paint even arrives and avoid changing the state in paintEvent().

    Instead of a flag, you could use some variable that denotes the scroll amount. This way you don't have to care how many times and when paintEvent() was called.

    Firsy of all, thanks for all your help.

    But, there is a big problem if we use a flag or a scroll amount in a paintEvent(), we can not differentiate that it is call by we update(currentIndex()) or Qt Server sometimes.


    one more question: Qt normally erases the widget's area before the paintEvent() call, now,
    How can i disable Qt to erase the content? some times I don't want to clear the content when I call update(), like this task to scroll focus item text.

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to paint a widget outside paintEvent()

    Quote Originally Posted by wesley View Post
    But, there is a big problem if we use a flag or a scroll amount in a paintEvent(), we can not differentiate that it is call by we update(currentIndex()) or Qt Server sometimes.
    Why do you need to differentiate them? The scroll amount depends on time --- who and when caused the repaint doesn't matter.

    Quote Originally Posted by wesley View Post
    How can i disable Qt to erase the content?
    You can set the Qt::WA_OpaquePaintEvent widget attribute.

  11. The following user says thank you to jacek for this useful post:

    wesley (27th February 2008)

  12. #11
    Join Date
    Feb 2008
    Posts
    49
    Thanks
    2
    Thanked 4 Times in 1 Post

    Default Re: How to paint a widget outside paintEvent()

    this thread is end. thanks for every one.

Similar Threads

  1. Drawing a widget in QItemDelegate's paint method
    By darkadept in forum Qt Programming
    Replies: 17
    Last Post: 11th August 2009, 05:15
  2. Tricky problem with ARGB widget / UpdateLayeredWindow
    By nooky59 in forum Qt Programming
    Replies: 3
    Last Post: 21st February 2008, 10:35
  3. painting a widget outside a paintEvent
    By jayw710 in forum Qt Programming
    Replies: 1
    Last Post: 25th June 2007, 23:18
  4. paint central widget of a QMainWindow???
    By Shuchi Agrawal in forum Newbie
    Replies: 3
    Last Post: 17th January 2007, 08:02
  5. Replies: 3
    Last Post: 27th November 2006, 09:56

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.