Results 1 to 4 of 4

Thread: Qpainter Help

  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 Qpainter Help

    I have created a ui window(mainwindow.ui). then i have added 3 qframe(Frame1, Frame2,Frame3) on the ui. then i placed an background image on the frame2.
    Then i have created a class frame on the mainwindow.h. I have created a paintEvent of Qframe on the header in frame class. My purpose is to create a Qframe on the class frame. Then place that frame into the frame2 of the mainwindow.ui. So that i can draw over the background image of frame2. i don want painting in the frame1 & frame3. I frame2 i have to draw over the background image.


    I coded and when i run my project the mainwindow.ui & frame class will run in 2different window.

    main.cpp

    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "mainwindow.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. MainWindow w;
    8. w.show();
    9. Frame m;
    10. m.show();
    11. return a.exec();
    12. }
    To copy to clipboard, switch view to plain text mode 



    mainwindow.h
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include<QtGui>
    6. #include<QtCore>
    7. #include<QtDebug>
    8. #include<QFrame>
    9.  
    10. namespace Ui {
    11. class MainWindow;
    12. class Frame;
    13. }
    14.  
    15. class MainWindow : public QMainWindow
    16. {
    17. Q_OBJECT
    18.  
    19. public:
    20. explicit MainWindow(QWidget *parent = 0);
    21. ~MainWindow();
    22.  
    23. private:
    24. Ui::MainWindow *ui;
    25. };
    26. class Frame : public QFrame{
    27. Q_OBJECT
    28. public:
    29. Frame( QWidget * parent = NULL ) : QFrame(parent){
    30.  
    31. }
    32. protected:
    33. void paintEvent( QPaintEvent * event );
    34. };
    35.  
    36. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 



    mainwindow.cpp
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4.  
    5. MainWindow::MainWindow(QWidget *parent) :
    6. QMainWindow(parent),
    7. ui(new Ui::MainWindow)
    8. {
    9. ui->setupUi(this);
    10. }
    11.  
    12.  
    13. MainWindow::~MainWindow()
    14. {
    15. delete ui;
    16. }
    17.  
    18. void Frame::paintEvent( QPaintEvent * event )
    19. {
    20. QFrame::paintEvent(event);
    21. QPainter painter(this);
    22. QPen linepen(Qt::red);
    23. linepen.setCapStyle(Qt::RoundCap);
    24. linepen.setWidth(25);
    25. painter.setRenderHint(QPainter::Antialiasing,true);
    26. painter.setRenderHint(QPainter::SmoothPixmapTransform,true);
    27. painter.setPen(linepen);
    28. painter.drawLine(100,100,300,300);
    29.  
    30. }
    To copy to clipboard, switch view to plain text mode 

  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: Qpainter Help

    I coded and when i run my project the mainwindow.ui & frame class will run in 2different window.
    That is correct, since your code is creating to top level widgets (windows).
    Qt Code:
    1. #include "mainwindow.h"
    2.  
    3. int main(int argc, char *argv[])
    4. {
    5. QApplication a(argc, argv);
    6. MainWindow w;
    7. w.show(); //<--- here you open the main window.
    8. Frame m;
    9. m.show(); //<--- her you open m as a top level window.
    10. return a.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 
    If you want to have one widget to be "in" another, you have to make it a child widget.
    ==========================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: Qpainter Help

    how can i make my qframe as child widget

  4. #4
    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: Qpainter Help

    Read the basics of C++ specifically what is a member variable.
    You can do it also as a non member, by giving the parent at construction time.
    But you better start with C++ basics first.
    ==========================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.

  5. The following user says thank you to high_flyer for this useful post:

    athulms (12th September 2011)

Similar Threads

  1. QPainter(&QPrinter) & QPainter(&QImage) communication
    By gufeatza in forum Qt Programming
    Replies: 2
    Last Post: 2nd February 2010, 07:25
  2. Replies: 5
    Last Post: 7th September 2009, 20:57
  3. gdi and QPainter
    By Elder Orb in forum Qt Programming
    Replies: 13
    Last Post: 29th September 2006, 12:43
  4. QPainter::save and QPAinter::restore()
    By quickNitin in forum Newbie
    Replies: 2
    Last Post: 17th June 2006, 22:11
  5. Replies: 3
    Last Post: 30th April 2006, 19:22

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.