Results 1 to 11 of 11

Thread: QTDesigner + QPaint [QT 4.4]

  1. #1
    Join Date
    Nov 2008
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default QTDesigner + QPaint [QT 4.4]

    Hi, i have i little problem in painting with qt.
    This is the situation: i draw my ui application using QTDesigner, so this is my code:

    -----myqtapp.h-----

    Qt Code:
    1. #include "ui_myqtapp.h"
    2. class myQtApp : public QWidget, private Ui::myQtAppDLG
    3. {
    4. Q_OBJECT
    5. public:
    6. myQtApp(QWidget *parent = 0);
    7. public slots:
    8. void doSomething();
    9. };
    To copy to clipboard, switch view to plain text mode 
    -----myqtapp.cpp--------

    Qt Code:
    1. #include <QtGui>
    2. #include "myqtapp.h"
    3. myQtApp::myQtApp(QWidget *parent)
    4. {
    5. setupUi(this);
    6.  
    7. connect( btnCrea, SIGNAL( clicked() ), this, SLOT( doSomething() ) );
    8. }
    9.  
    10. void myQtApp::doSomething()
    11. {
    12. //here i want to paint for example a simple point in my ui. In my UI there is a widget called "widget".
    13.  
    14. }
    To copy to clipboard, switch view to plain text mode 
    Can someone help me? i try many solution as QPaint, or creating a new Widget, but nothing...
    Thanks
    Last edited by wysota; 15th November 2008 at 08:57. Reason: missing [code] tags

  2. #2
    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: QTDesigner + QPaint [QT 4.4]

    You have to do the painting in the paintEvent() of the widget you want to paint on or you can apply an event filter to the widget so that you can intercept the event and do the painting elsewhere. Alternatively you can paint on a pixmap at an arbitrary moment and display it on a QLabel. The painting itself should be done using QPainter.

  3. #3
    Join Date
    Nov 2008
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTDesigner + QPaint [QT 4.4]

    ok, i insert this code in my file
    Qt Code:
    1. void QWidget::paintEvent(QPaintEvent *)
    2. {
    3. QPainter painter(this);
    4. painter.setPen(Qt::blue);
    5. painter.setFont(QFont("Arial", 30));
    6. painter.drawText(rect(),
    7. Qt::AlignCenter, "Qt");
    8. }
    To copy to clipboard, switch view to plain text mode 


    but it's not clear how to call this event...
    I try in many modes, but nothing, the widget doesn't display nothing

  4. #4
    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: QTDesigner + QPaint [QT 4.4]

    You don't call it - Qt does. You just have to reimplement the paintEvent virtual method in your class.

  5. #5
    Join Date
    Nov 2008
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTDesigner + QPaint [QT 4.4]

    hi everyone,
    now i'm able to draw something on my widget, but i can't create a function (connected to a button) called doSomething() that draw something of new in my widget. How can i call the paintevent method in my code??
    thanks

    Qt Code:
    1. #include <QtGui>
    2. #include "test.h"
    3.  
    4. Test::Test(QWidget *parent)
    5. : QWidget(parent)
    6. {
    7. setupUi(this);
    8. m_drawer->installEventFilter(this);
    9. [COLOR="red"]connect(pushButton, SIGNAL( clicked()), this, SLOT( doSomething() ));[/COLOR]
    10. }
    11.  
    12.  
    13. void Test::doSomething()
    14. {
    15. i want to call a method that paint something of new on the widget!!
    16. }
    17.  
    18. bool Test::eventFilter(QObject *o, QEvent *e)
    19. {
    20. if (o == m_drawer && e->type() == QEvent::Paint) {
    21. QPainter painter(m_drawer);
    22. painter.setPen(Qt::blue);
    23. painter.setFont(QFont("Arial", 30));
    24. painter.drawText(m_drawer->rect(), Qt::AlignCenter, "Qt");
    25. return true;
    26. }
    27. return QWidget::eventFilter(o, e);
    28. }
    29.  
    30.  
    31. void Test::paintEvent(QPaintEvent *event){
    32.  
    33. QPainter painter(this);
    34. painter.setRenderHint(QPainter::Antialiasing, true);
    35. painter.setPen(QPen(Qt::black, 15, Qt::SolidLine, Qt::RoundCap,Qt::MiterJoin));
    36. painter.setBrush(QBrush(Qt::blue, Qt::DiagCrossPattern));
    37. painter.setPen(QPen(Qt::yellow, 5, Qt::SolidLine, Qt::RoundCap,Qt::MiterJoin));
    38. painter.translate(100.0,100.0);
    39. painter.drawPie(0, 0, 300, 300, 60 * 16, 270 * 16);
    40. painter.drawEllipse(30,30,60,60);
    41.  
    42. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    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: QTDesigner + QPaint [QT 4.4]


  7. #7
    Join Date
    Nov 2008
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTDesigner + QPaint [QT 4.4]

    thank you!!
    I've another problem now.

    Qt Code:
    1. distanzaEuclidea[j] = Math::Sqrt((nodi[i].x - nodi[j].x) * (nodi[i].x - nodi[j].x) + Math::Pow((nodi[i].y - nodi[j].y), 2));
    To copy to clipboard, switch view to plain text mode 

    i want to use mathematic functions of c++ library, but i can't include them in my project.
    I try
    #include <cmath>
    #include "match.h"
    but the compiler give me always error;
    What's the problem??

  8. #8
    Join Date
    Nov 2008
    Posts
    142
    Thanks
    3
    Thanked 20 Times in 20 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTDesigner + QPaint [QT 4.4]

    Quote Originally Posted by pippofrank View Post
    #include "match.h"
    It would be math.h, or is that just a typo in your post?

    Anyway, what error does the compiler give?

  9. #9
    Join Date
    Nov 2008
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTDesigner + QPaint [QT 4.4]

    sorry,
    #include "math.h"

    the compiler say:
    "Math has not been declared"
    "Pow undeclared"
    "Sqtr undeclared"

  10. #10
    Join Date
    Nov 2008
    Posts
    142
    Thanks
    3
    Thanked 20 Times in 20 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTDesigner + QPaint [QT 4.4]

    Well, that figures. math.h only declares functions like pow, sqrt etc. No "Math" namespace or anything like that. So the problem is not including the headers, but your attempt of using the defined functions. Just drop the "Math::" part and write the function names in all lower case, that should do the trick.

  11. #11
    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: QTDesigner + QPaint [QT 4.4]

    <cmath> uses the std:: namespace hence it is std::sqrt(). Sometimes it is worth looking into the source (or in this case header) files...

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.