Results 1 to 11 of 11

Thread: how to draw a line over a frame?

  1. #1
    Join Date
    May 2011
    Posts
    120
    Thanks
    33
    Qt products
    Qt4
    Platforms
    Windows

    Default Qpainter donot work

    Hai,
    I am doing a project which needs a Qpainter to draw line but Qpainter donot work when we apply a style sheet to the frame where we have to draw the line

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Qpainter donot work

    This works ok:
    Qt Code:
    1. class Frame : public QFrame{
    2. Q_OBJECT
    3. public:
    4. Frame( QWidget * parent = NULL ) : QFrame(parent){
    5. setStyleSheet( "Frame{ background: blue }" );
    6. }
    7. protected:
    8. void paintEvent( QPaintEvent * event ){
    9. QFrame::paintEvent(event);
    10. QPainter p(this);
    11. p.drawLine( rect().topLeft(), rect().bottomRight() );
    12. p.drawText( rect().center(), "works!" );
    13. }
    14. };
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jul 2011
    Posts
    81
    Thanks
    8
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows Symbian S60 Maemo/MeeGo

    Default Re: Qpainter donot work

    can you provide me with an example. pls

  4. #4
    Join Date
    May 2011
    Posts
    16
    Thanks
    3
    Qt products
    Qt3 Qt4 Qt/Embedded Qt Jambi PyQt3 PyQt4
    Platforms
    Windows

    Default Re: Qpainter donot work

    Since iam using the form window , i cannot apply this code , please give me an Qt example of an frame with Style sheet(background image) and Qpainter drawing lines on it

  5. #5
    Join Date
    May 2011
    Posts
    120
    Thanks
    33
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Qpainter donot work

    thanx for your help but it doesnt work and gives errors while we run it. can you please give some more details

  6. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Qpainter donot work

    You only have to change one line... clearly too hard:
    Qt Code:
    1. #include <QtGui>
    2. #include <QDebug>
    3.  
    4. class Frame : public QFrame{
    5. Q_OBJECT
    6. public:
    7. Frame( QWidget * parent = NULL ) : QFrame(parent){
    8. // setStyleSheet( "Frame{ background: blue }" );
    9. setStyleSheet( "Frame{ background-image: url(plasma.png); }" );
    10. }
    11. protected:
    12. void paintEvent( QPaintEvent * event ){
    13. QFrame::paintEvent(event);
    14. QPainter p(this);
    15. p.drawLine( rect().topLeft(), rect().bottomRight() );
    16. p.drawText( rect().center(), "works!" );
    17. }
    18. };
    19.  
    20. int main(int argc, char *argv[])
    21. {
    22. QApplication app(argc, argv);
    23.  
    24. Frame m;
    25. m.show();
    26. return app.exec();
    27. }
    28. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 
    and it looks like:
    simple_example.jpg
    with a 64x64 tile background.


    If you do not share the errors then you cannot expect corrections.

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

    Default Re: Qpainter donot work

    Quote Originally Posted by vishnu717 View Post
    Since iam using the form window , i cannot apply this code
    Why is that?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Qpainter donot work

    can you provide me with an example. pls
    I already did.
    thanx for your help but it doesnt work and gives errors while we run it. can you please give some more details
    What errors ? It seems to work ok for ChrisW67.

  9. #9
    Join Date
    Jul 2011
    Posts
    81
    Thanks
    8
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows Symbian S60 Maemo/MeeGo

    Default how to draw a line over a frame?

    in my progrm i am using 4frames(UI). I use show/hide frames using buttons. I have two draw a line in the 3rd frame. I have used the code
    Qt Code:
    1. void MainWindow::paintEvent(QPaintEvent *e)
    2. {
    3. QPainter painter(this);
    4. QPen linepen(Qt::red);
    5. linepen.setWidth(25);
    6. linepen.setCapStyle(Qt::RoundCap);
    7. painter.setRenderHint(QPainter::Antialiasing,true);
    8. painter.setPen(linepen);
    9. painter.drawLine(point,point2);
    10. }
    To copy to clipboard, switch view to plain text mode 



    since i have used the painter(this) the drawing is done on the main window so i cannot see the line on the frame. since frames(with background images) are placed over the main window.


    Now i try to use painter(ui->frame3)

    But it doesn't work. pls help me, thanks in advance
    Last edited by wysota; 17th August 2011 at 22:41. Reason: missing [code] tags

  10. #10
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: how to draw a line over a frame?

    Asked here already. What part of the sample code you don't understand ?

  11. #11
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: how to draw a line over a frame?

    The (default) painting of the child will happen after the painting of the parent. If you override the paint event of the parent then why do you expect that the painting behaviour of a child widget will be changed? You want to override the paint event of the child widget.
    "We can't solve problems by using the same kind of thinking we used when we created them." -- Einstein
    If you are posting code then please use [code] [/code] tags around it - makes addressing the problem easier.

Similar Threads

  1. To draw a circle on a frame when the key is pressed
    By soumya in forum Qt Programming
    Replies: 18
    Last Post: 9th February 2010, 10:21
  2. How to draw a widget directly on the frame buffer
    By kapoorsudhish in forum Qt Programming
    Replies: 8
    Last Post: 12th November 2009, 06:59
  3. how to draw a circle on a frame in Qt-4
    By grsandeep85 in forum Qt Programming
    Replies: 1
    Last Post: 16th September 2009, 08:05
  4. How do draw a frame in QListView?
    By someralex in forum Qt Programming
    Replies: 24
    Last Post: 21st December 2006, 11: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.