Hello there,

I'm new to Qt and just working my way through the tutorials and docs. But now I am stuck on a seemingly simple problem. I hope that you can help me out.

So I was trying to hack one of Qt's tutorials a bit: http://doc.trolltech.com/4.3/tutorial-t10.html
I created a custom widget like this
Qt Code:
  1. #include "forceindicator.h"
  2.  
  3. CannonField::CannonField(QWidget *parent)
  4. : QWidget(parent)
  5. {
  6. /* stuff */
  7.  
  8. indicator = new ForceIndicator;
  9. overlayLayout = new QGridLayout(this);
  10. overlayLayout->addWidget(indicator,0,1,Qt::AlignRight | Qt::AlignTop);
  11. this->setLayout(overlayLayout);
  12. /* more stuff */
  13. }
To copy to clipboard, switch view to plain text mode 

Now this child widget "indicator" has a slot "setForce" which is triggered by some events. Within this slot (see below) update() is called.
I was thinking that this in turn would trigger a paint event and call my paintEvent(QPaintEvent *) method. However this is not working. The slot is called alright and everything before and after my update() calls is executed (checked with some couts), but the paintEvent (again couts) is not called.

I tried repaint instead - same result. Triggering the paintEvent explicitely does call my custom function.

So, what stupid mistake am I missing out on?

Thanks for your help!

Here is the header of my custom widget
Qt Code:
  1. #ifndef FORCEINDICATOR_H
  2. #define FORCEINDICATOR_H
  3.  
  4. #include <QWidget>
  5.  
  6. class ForceIndicator : public QWidget
  7. {
  8. Q_OBJECT
  9.  
  10. public:
  11. ForceIndicator(QWidget *parent = 0);
  12.  
  13. int force() const {return currentForce;}
  14.  
  15. public slots:
  16. void setForce(int force);
  17.  
  18. signals:
  19. void forceChanged(int newForce);
  20.  
  21. protected:
  22. void paintEvent(QPaintEvent *event);
  23.  
  24. private:
  25. int currentForce;
  26. };
  27.  
  28. #endif
To copy to clipboard, switch view to plain text mode 

And here are parts of the body
#
Qt Code:
  1. include <QPainter>
  2. #include <iostream>
  3. #include "forceindicator.h"
  4.  
  5. void ForceIndicator::setForce(int force)
  6. {
  7. if (currentForce == force)
  8. return;
  9. std::cout << "bla" << std::endl;
  10. currentForce = force;
  11.  
  12. update();
  13. std::cout << "blabla" << std::endl;
  14. //emit forceChanged(currentForce);
  15. }
  16.  
  17.  
  18. void ForceIndicator::paintEvent(QPaintEvent* /*event*/)
  19. {
  20. std::cout << "blub" << std::endl;
  21. QPainter painter(this);
  22.  
  23. /* stuff */
  24.  
  25. }
To copy to clipboard, switch view to plain text mode