Results 1 to 11 of 11

Thread: pls help. Need Qpainter to draw over a background image of mainwindow

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

    Default pls help. Need Qpainter to draw over a background image of mainwindow

    Qt Code:
    1. [ATTACH=CONFIG]6839[/ATTACH]
    To copy to clipboard, switch view to plain text mode 

    I am developing a qt wordsearch game.
    I have to draw lines on the image when i finds a word from the words panel shown below the letter grid.
    When i draw the line its drawn under the image so i can't see the line on the screen.
    Hope u understood my question. Pls help. Thanks in advance

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: pls help. Need Qpainter to draw over a background image of mainwindow

    When i draw the line its drawn under the image so i can't see the line on the screen.
    That is because you are probably not drawing on the correct widget.
    Draw on the widget which contains your image.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  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: pls help. Need Qpainter to draw over a background image of mainwindow

    i have set the image as style sheet of a frame(ui).
    How can i use painter on the frame created in ui.
    Pls help

  4. #4
    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: pls help. Need Qpainter to draw over a background image of mainwindow

    How can i use painter on the frame created in ui.
    Create a subclass of QFrame (or QWidget) and reimplement paintEvent. Use "promote to..." feature to place it on the ui form (right click on the widget in designer -> "promote to.." and type the details in popup ).

  5. #5
    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: pls help. Need Qpainter to draw over a background image of mainwindow

    can you pls provide me some code??

    I have created a new class
    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-image: url(plasma.png); }" );
    9. }
    10. protected:
    11. void paintEvent( QPaintEvent * event ){
    12. QFrame::paintEvent(event);
    13. QPainter p(this);
    14. p.drawLine( rect().topLeft(), rect().bottomRight() );
    15. p.drawText( rect().center(), "works!" );
    16. }
    17. };
    To copy to clipboard, switch view to plain text mode 

    I have drawn the line in the subclass frame. Now how can i promote the frame over my mainwindow(ui)

  6. #6
    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: pls help. Need Qpainter to draw over a background image of mainwindow

    You already have all needed code, now you have to use the designer:
    - place the QFrame on the main widget
    - right click on that frame, select "Promote to..."
    - type in the class name and header file
    - (!) save the modified form
    - run qmake and compile the project again

  7. #7
    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: pls help. Need Qpainter to draw over a background image of mainwindow

    k it worked for me. But when i am accessing the points from mouse click of button. it have to draw the lines. It draws lines when i give static points. when i am giving dyanamic points the prprogram runs but shows non-image error

  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: pls help. Need Qpainter to draw over a background image of mainwindow

    But when i am accessing the points from mouse click of button. it have to draw the lines. It draws lines when i give static points. when i am giving dyanamic points the prprogram runs but shows non-image error
    What can we do about that without seeing a single line of code, or the "non-image error" ?

  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 Re: pls help. Need Qpainter to draw over a background image of mainwindow

    Qt Code:
    1. void MainWindow:: drawpoint()
    2. {
    3. {calculations of mouse click point, The mouse click point is store in a [B]Qpoint point[/B]}
    4. update();
    5. }
    6.  
    7. void drawing:: paintEvent(QPaintEvent *painter)
    8. {
    9. MainWindow *main=new MainWindow;
    10. QFrame::paintEvent(painter);
    11. QPainter p(this);
    12. QPen linepen(Qt::cyan);
    13. p.setRenderHint(QPainter::Antialiasing,true);
    14. p.setRenderHint(QPainter::SmoothPixmapTransform,true);
    15. linepen.setWidth(25);
    16. linepen.setCapStyle(Qt::RoundCap);
    17. p.setPen(linepen);
    18. p.setOpacity(0.7);
    19. p.drawPoint(main->point);
    20. }
    To copy to clipboard, switch view to plain text mode 



    I have used the above code.
    I have arranged buttons in 11 X 11 format in a grid. I connected all the buttons to the drawpoint function. My drawing function paintevent is of drawing class.
    when i call the update function it does not invoke the paintevent of drawing class.


    I tryed to create an object like


    Qt Code:
    1. drawing *draw=new drawing
    To copy to clipboard, switch view to plain text mode 


    then i sent the point by calling
    Qt Code:
    1. draw->update
    To copy to clipboard, switch view to plain text mode 
    but the qt shows an error "no appropriate default constructor available"
    Last edited by athulms; 13th September 2011 at 11:24.

  10. #10
    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: pls help. Need Qpainter to draw over a background image of mainwindow

    Why are you creating a new window every time paintEvent() of another widget is called? What is "drawing" and how are you updating it?

    By the way, Qt doesn't show you any errors, the compiler does -- your problem is strictly of C++ nature.
    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.


  11. #11
    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: pls help. Need Qpainter to draw over a background image of mainwindow

    k i have completed my drawing. Thanks for ur help wysota, stampede

Similar Threads

  1. Draw line on image using QPainter
    By Qt Coder in forum Qt Programming
    Replies: 29
    Last Post: 11th August 2015, 12:09
  2. Replies: 4
    Last Post: 19th August 2011, 09:13
  3. Replies: 1
    Last Post: 25th June 2010, 18:31
  4. MainWindow Background-Image issues
    By BingoMaster in forum Newbie
    Replies: 2
    Last Post: 5th November 2009, 17:22
  5. Replies: 5
    Last Post: 10th April 2008, 09:52

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.