Bonjour à toutes et à tous,

Toujours dans la découverte de Qt, je m'amuse (et oui je m'éclate ) avec les widget. Pour pousser l'apprentissage un peu plus loin, je souhaiterai créer mon propre widget animé ("animable") pour l'insérer ensuite sous QtDesigner.
Ce widget représentera l'état d'une batterie (comme sur un téléphone portable mais uniquement l'indication du reste de batterie, pas d'animation lors du branchement). J'ai commencé par créer une classe BatteryDisplay qui hérite de QLabel dont voici le code :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#ifndef BATTERYDISPLAY_H
#define BATTERYDISPLAY_H
 
#include <QObject>
#include <QWidget>
#include <QLabel>
#include <QMetaEnum>
 
class BatteryDisplay : public QWidget
{
    Q_OBJECT
    Q_ENUMS(State)
public:
    explicit BatteryDisplay(QWidget *parent = 0);
    void setBatteryRange(int FullValue = 100, int EmptyValue = 25);
    enum  State
    {
        Fullcharge,
        Full,
        HalfFull,
        HalfEmpty,
        Empty
    };
 
signals:
 
public slots:
    void updateValue(float);
    void updateState(int);
 
private:
 
    State state;
    QLabel* batteryLogo;
    int batteryFullValue;
    int batteryHalfFullValue;
    int batteryHalfEmptyValue;
    int batteryEmptyValue;
    float batteryCurrentValue;
    bool inCharge;
    float coeffBattery;
    float offsetBattery;
    QString enumToString(State aState);
};
 
 
#endif // BATTERYDISPLAY_H
.cpp
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include "batterydisplay.h"
#include <QtDebug>
 
BatteryDisplay::BatteryDisplay(QWidget *parent) : QWidget(parent)
{
    batteryLogo = new QLabel(parent);
    //batteryLogo->setText("icons/BatteryFull.png");
    batteryLogo->setPixmap(QPixmap(":icons/BatteryFull.png"));
    batteryFullValue = 100;
    batteryHalfFullValue = 75;
    batteryHalfEmptyValue = 50;
    batteryEmptyValue = 25;
    QString valueInString = enumToString(state);
    qDebug() << valueInString;
}
 
void BatteryDisplay::updateValue(float value)
{
 
    qDebug() << "update() battery ! " << value;
    batteryCurrentValue = value * coeffBattery + offsetBattery;
    qDebug() << "battery scaled: " << batteryCurrentValue;
    if( batteryCurrentValue > batteryFullValue ) batteryCurrentValue = batteryFullValue;
    if ( batteryCurrentValue <= batteryFullValue && batteryCurrentValue > batteryHalfFullValue)
    {
        if (inCharge)
            state = Fullcharge;
        else
        {
            state = Full;
        }
        batteryLogo->setPixmap(QPixmap(":icons/BatteryFull.png"));
    }
    else if ( batteryCurrentValue <= batteryHalfFullValue && batteryCurrentValue > batteryHalfEmptyValue)
    {
        state = HalfFull;
        batteryLogo->setPixmap(QPixmap(":icons/BatteryHalfFull.png"));
    }
    else if ( batteryCurrentValue <= batteryHalfEmptyValue && batteryCurrentValue > batteryEmptyValue)
    {
        state = HalfEmpty;
        batteryLogo->setPixmap(QPixmap(":icons/BatteryHalfEmpty.png"));
    }
    else
    {
        state = Empty;
        batteryLogo->setPixmap(QPixmap(":icons/BatteryEmpty.png"));
    }
 
    QString valueInString = enumToString(state);
    qDebug()  << valueInString;
 
}
 
void BatteryDisplay::updateState(int state)
{
    inCharge = state;
    qDebug() << "Batterie branchée: "<< inCharge;
}
 
void BatteryDisplay::setBatteryRange(int FullValue, int EmptyValue)
{
    coeffBattery = (batteryFullValue - batteryEmptyValue) / ( FullValue - EmptyValue );
    qDebug() << coeffBattery;
    offsetBattery = batteryFullValue - ( coeffBattery * FullValue );
    qDebug() << offsetBattery;
}
 
QString BatteryDisplay::enumToString(State aState)
 {
     int index = metaObject()->indexOfEnumerator("State");
     QMetaEnum metaEnum = metaObject()->enumerator(index);
     return metaEnum.valueToKey(aState);
 }
Main.cpp :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
 
 
    BatteryMonitor *batteryMonitor = new BatteryMonitor();
    BatteryDisplay *batteryDisplay = new BatteryDisplay();
 
    batteryDisplay->setBatteryRange(25,18);
    QObject::connect(batteryMonitor, SIGNAL(valueChanged(float)), batteryDisplay, SLOT(updateValue(float)));
    QObject::connect(batteryMonitor, SIGNAL(stateChanged(int)), batteryDisplay, SLOT(updateState(int)));
 
 
    MainWindow w;
    w.show();
    w.setCursor(Qt::BlankCursor); // Hide the mouse cursor
 
 
    return a.exec();
 
}
BatteryMonitor ne contient qu'un simple timer qui émet un signal toutes les 5 secondes (pour rafraichir la valeur de la batterie - testé et fonctionnel)
Ensuite, sous QtDesignger, j'insère un widget que je promeus en BatteryDisplay. Au lancement de mon application, le logo de la batterie s'affiche correctement (d'ailleurs il persiste à rester en haut à gauche), mais ne s'anime pas...
En écrivant ces lignes, je me pose la question suivante : Est-ce que la le widget promu fait référence à l'objet créé dans le main ? cela pourrait expliquer pourquoi il n'est pas mis à jour. Aussi, comment faire pour lier ce widget personnalisé inséré via QtDesigner avec l'instance créé dans mon main.cpp ? Peut-être devrais-je plutôt créer cet objet dans mon mainwindow.cpp ?

EDIT 1 : lorsque je clique droit sur le widget inséré sous QtDesiger je n'ai pas accès à ses slots / signaux.

Merci d'avance, dM