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
#include "forceindicator.h"
CannonField
::CannonField(QWidget *parent
){
/* stuff */
indicator = new ForceIndicator;
overlayLayout->addWidget(indicator,0,1,Qt::AlignRight | Qt::AlignTop);
this->setLayout(overlayLayout);
/* more stuff */
}
#include "forceindicator.h"
CannonField::CannonField(QWidget *parent)
: QWidget(parent)
{
/* stuff */
indicator = new ForceIndicator;
overlayLayout = new QGridLayout(this);
overlayLayout->addWidget(indicator,0,1,Qt::AlignRight | Qt::AlignTop);
this->setLayout(overlayLayout);
/* more stuff */
}
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
#ifndef FORCEINDICATOR_H
#define FORCEINDICATOR_H
#include <QWidget>
class ForceIndicator
: public QWidget{
Q_OBJECT
public:
ForceIndicator
(QWidget *parent
= 0);
int force() const {return currentForce;}
public slots:
void setForce(int force);
signals:
void forceChanged(int newForce);
protected:
private:
int currentForce;
};
#endif
#ifndef FORCEINDICATOR_H
#define FORCEINDICATOR_H
#include <QWidget>
class ForceIndicator : public QWidget
{
Q_OBJECT
public:
ForceIndicator(QWidget *parent = 0);
int force() const {return currentForce;}
public slots:
void setForce(int force);
signals:
void forceChanged(int newForce);
protected:
void paintEvent(QPaintEvent *event);
private:
int currentForce;
};
#endif
To copy to clipboard, switch view to plain text mode
And here are parts of the body
#
include <QPainter>
#include <iostream>
#include "forceindicator.h"
void ForceIndicator::setForce(int force)
{
if (currentForce == force)
return;
std::cout << "bla" << std::endl;
currentForce = force;
update();
std::cout << "blabla" << std::endl;
//emit forceChanged(currentForce);
}
void ForceIndicator
::paintEvent(QPaintEvent* /*event*/) {
std::cout << "blub" << std::endl;
/* stuff */
}
include <QPainter>
#include <iostream>
#include "forceindicator.h"
void ForceIndicator::setForce(int force)
{
if (currentForce == force)
return;
std::cout << "bla" << std::endl;
currentForce = force;
update();
std::cout << "blabla" << std::endl;
//emit forceChanged(currentForce);
}
void ForceIndicator::paintEvent(QPaintEvent* /*event*/)
{
std::cout << "blub" << std::endl;
QPainter painter(this);
/* stuff */
}
To copy to clipboard, switch view to plain text mode
Bookmarks