Important: Please read the Qt Code of Conduct -

uninitialized switchcase error

  • this is my function.

    void coreaction::batteryCheck()
        if (sm.getShowBattery()) {
            UPower *u = new UPower(this);
            Battery *b;
            foreach (Battery *bat, *u->batteries()) {
                b = u->batteries()->value(bat->path());
            switch( b->state() ) {
                case Battery::FullyCharged:
                    ui->batteryStatus->setText( tr( "Full" ) );
                case Battery::Discharging:
                    ui->batteryStatus->setText( tr( "Discharging" ) );
                case Battery::Charging:
                    ui->batteryStatus->setText( tr( "Charging" ) );
                    ui->batteryStatus->setText( tr( "No Battery" ) );
        } else {


    #ifndef BATTERY_H
    #define BATTERY_H
    #include <QObject>
    class QDBusInterface;
    class Battery : public QObject {
        Q_PROPERTY(QString sysfsPath READ sysfsPath STORED false)
        Q_PROPERTY(QString model READ model STORED false)
        Q_PROPERTY(QString vendor READ vendor STORED false)
        Q_PROPERTY(QString technology READ technology STORED false)
        Q_PROPERTY(QString path READ path STORED false)
        Q_PROPERTY(bool powerSupply READ powerSupply STORED false)
        Q_PROPERTY(bool hasHistory READ hasHistory STORED false)
        Q_PROPERTY(bool hasStatistics READ hasStatistics STORED false)
        Q_PROPERTY(bool isPresent READ isPresent STORED false)
        Q_PROPERTY(bool isRechargeable READ isRechargeable STORED false)
        Q_PROPERTY(double energy READ energy STORED false)
        Q_PROPERTY(double energyEmpty READ energyEmpty STORED false)
        Q_PROPERTY(double energyFull READ energyFull STORED false)
        Q_PROPERTY(double energyFullDesign READ energyFullDesign STORED false)
        Q_PROPERTY(double energyRate READ energyRate STORED false)
        Q_PROPERTY(double voltage READ voltage STORED false)
        Q_PROPERTY(double percentage READ percentage STORED false)
        Q_PROPERTY(double capacity READ capacity STORED false)
        Q_PROPERTY(State state READ state STORED false)
        Q_PROPERTY(int lowLevel READ lowLevel)
        Q_PROPERTY(double toFull READ toFull STORED false)
        Q_PROPERTY(double toEmpty READ toFull STORED false)
        Battery(const QString & path, QObject *parent = 0);
        /* Enums */
        enum State {FullyCharged, Charging, Discharging};
        /* Properties */
        QString sysfsPath() const;
        QString model() const;
        QString vendor() const;
        QString technology() const;
        const QString & path() const;
        bool powerSupply() const;
        bool hasHistory() const;
        bool hasStatistics() const;
        bool isPresent() const;
        bool isRechargeable() const;
        bool isValid() const;
        double energy() const;
        double energyEmpty() const;
        double energyFull() const;
        double energyFullDesign() const;
        double energyRate() const;
        double voltage() const;
        double percentage() const;
        double capacity() const;
        double toFull() const;
        double toEmpty() const;
        int lowLevel() const;
        void setLowLevel(int value);
        Battery::State state() const;
        void createInterface();
        QDBusInterface      *m_interface;
        QString             m_path;
        bool                m_hasAlreadyBeenLow;
        bool                m_hasAlreadyBeenFull;
        int                 m_lowLevel;
        bool                m_valid;
    public Q_SLOTS:
        void refresh();
    private Q_SLOTS:
        void update();
        void changed();
        void low();
        void full();

    this is battery.cpp

    #include "battery.h"
    #include <QtDBus/QDBusConnection>
    #include <QtDBus/QDBusInterface>
    #include <QSettings>
    Battery::Battery(const QString &path, QObject *parent)
        : QObject(parent), m_interface(0),
          m_path(path), m_hasAlreadyBeenLow(false),
          m_hasAlreadyBeenFull(false), m_valid(false)  {
    Battery::~Battery() {
    void Battery::createInterface() {
        if(m_interface==0 || !m_interface->isValid()) {
            m_interface = new QDBusInterface("org.freedesktop.UPower", m_path,
                                             "org.freedesktop.UPower.Device", QDBusConnection::systemBus(), this);
            connect(m_interface, SIGNAL(Changed()), this, SIGNAL(changed()));
            if(!m_interface->isValid()) {
                delete m_interface;
                m_interface = 0;
                m_valid = false;
        m_valid = true;
    void Battery::refresh() {
    void Battery::update() {
        int level = percentage();
        if(level<lowLevel()) {
            if(!m_hasAlreadyBeenLow) {
                m_hasAlreadyBeenLow = true;
                emit low();
        } else {
            m_hasAlreadyBeenLow = false;
        if(state() == FullyCharged) {
            if(!m_hasAlreadyBeenFull) {
                m_hasAlreadyBeenFull = true;
                emit full();
        } else {
            m_hasAlreadyBeenFull = false;
    bool Battery::isValid() const {
        return m_interface->property("Type").toInt() == 2 && m_valid;
    int Battery::lowLevel() const {
        QSettings settings;
            int lvl = settings.value(m_path, 20).toInt();
        return lvl;
    void Battery::setLowLevel(int value) {
        QSettings settings;
            settings.setValue(m_path, value);
    QString Battery::sysfsPath() const {
        return m_interface->property("NativePath").toString();
    QString Battery::model() const {
        return m_interface->property("Model").toString();
    QString Battery::vendor() const {
        return m_interface->property("Vendor").toString();
    QString Battery::technology() const {
        return m_interface->property("Technology").toString();
    const QString & Battery::path() const {
        return m_path;
    bool Battery::powerSupply() const {
        return m_interface->property("PowerSupply").toBool();
    bool Battery::hasHistory() const {
        return m_interface->property("HasHistory").toBool();
    bool Battery::hasStatistics() const {
        return m_interface->property("HasStatistics").toBool();
    bool Battery::isPresent() const {
        return m_interface->property("IsPresent").toBool();
    bool Battery::isRechargeable() const {
        return m_interface->property("IsRechargeable").toBool();
    double Battery::energy() const {
        return m_interface->property("Energy").toDouble();
    double Battery::energyEmpty() const {
        return m_interface->property("EnergyEmpty").toDouble();
    double Battery::energyFull() const {
        return m_interface->property("EnergyFull").toDouble();
    double Battery::energyFullDesign() const {
        return m_interface->property("EnergyFullDesign").toDouble();
    double Battery::energyRate() const {
        return m_interface->property("EnergyRate").toDouble();
    double Battery::voltage() const {
        return m_interface->property("Voltage").toDouble();
    double Battery::percentage() const {
        return m_interface->property("Percentage").toDouble();
    double Battery::capacity() const {
        return m_interface->property("Capacity").toDouble();
    double Battery::toEmpty() const {
        return m_interface->property("TimeToEmpty").toDouble();
    double Battery::toFull() const {
        return m_interface->property("TimeToFull").toDouble();
    Battery::State Battery::state() const {
        uint state = m_interface->property("State").toUInt();
        switch( state ) {
            case (uint) 0:
                return FullyCharged;
            case (uint) 1:
                return Charging;
            case (uint) 2:
                return Discharging;
                return FullyCharged;

    problem is when i compile i got this error

    g++ -c -pipe -O2 -march=armv7-a -mfloat-abi=hard -mfpu=vfpv3-d16 -O2 -pipe -fstack-protector-strong -fno-plt -std=gnu++11 -Wall -W -D_REENTRANT -fPIC -DQT_DEPRECATED_WARNINGS -DQT_NO_DEBUG -DQT_PRINTSUPPORT_LIB -DQT_MULTIMEDIAWIDGETS_LIB -DQT_CHARTS_LIB -DQT_WIDGETS_LIB -DQT_MULTIMEDIA_LIB -DQT_GUI_LIB -DQT_DBUS_LIB -DQT_NETWORK_LIB -DQT_CONCURRENT_LIB -DQT_CORE_LIB -I. -isystem /usr/include/qt -isystem /usr/include/qt/QtPrintSupport -isystem /usr/include/qt/QtMultimediaWidgets -isystem /usr/include/qt/QtCharts -isystem /usr/include/qt/QtWidgets -isystem /usr/include/qt/QtMultimedia -isystem /usr/include/qt/QtGui -isystem /usr/include/qt/QtDBus -isystem /usr/include/qt/QtNetwork -isystem /usr/include/qt/QtConcurrent -isystem /usr/include/qt/QtCore -I. -isystem /usr/include/libdrm -I. -I/usr/lib/qt/mkspecs/linux-g++ -o mimeutils.o corefm/mimeutils.cpp
    make: *** [Makefile:2028: corebox.o] Error 1
    make: *** Waiting for unfinished jobs....
    coreaction/coreaction.cpp: In member function 'void coreaction::batteryCheck()':
    coreaction/coreaction.cpp:114:25: warning: 'b' may be used uninitialized in this function [-Wmaybe-uninitialized]
             switch( b->state() ) {
    ==> ERROR: A failure occurred in build().

    what i missing??

  • Lifetime Qt Champion

    @saber Take a look at this code:

    Battery *b;
    foreach (Battery *bat, *u->batteries()) {
        b = u->batteries()->value(bat->path());

    If u->batteries() returns an empty list b will be a dangling pointer and your app will crash. That's what compiler is telling you.
    To be honest I don't understand this code: why do you iterate in a loop over the whole list but only keep the last value in b? It would be way faster to directly access the last element.

  • @jsulm

    please can u show the right way to do this .
    please .
    it is done by my friend and he is not in touch .
    i need the fix badly.

  • Lifetime Qt Champion

    @saber This is really not difficult:

    Battery *b = nullptr;
    foreach (Battery *bat, *u->batteries()) {
        b = u->batteries()->value(bat->path());
    if (!b)

  • @jsulm
    thanks for the solution .

    can i invite u to join my project ? i know it is not the way to do that. but my app needs many fix that i can't ask all of them in forum.

    my app is for linux.please check it out CoreBox

Log in to reply