Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Struggle with struct, const QVector and register to QML
Forum Updated to NodeBB v4.3 + New Features

Struggle with struct, const QVector and register to QML

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 311 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Krulle
    wrote on last edited by
    #1

    Hello Guys,

    I am currently trying to write an application for the Raspberry Pi.
    I have compiled Qt version 5.15 and am using the latest QtCreator.

    I'm struggling about an error during compilation:
    Compilation error

    I have a class "Core", which is registered in main.cpp as

        // init core-Application
        Core core;
        engine.rootContext()->setContextProperty("CoreApp", &core);
    

    core.h

    using namespace Gpio;
    
    class Core : public QObject
    {
        Q_OBJECT
    
        Q_PROPERTY(GpioBase gpioBase READ gpioBase)
    
    public:
        explicit Core(QObject *parent = nullptr);
    
        const GpioBase &gpioBase() const;
    
    private:
        GpioBase m_gpioBase;
    }
    

    I think the problem is here in class GpioBase (gpiobase.h):

    namespace Gpio {
    
    class GpioBase : public QObject
    {
        Q_OBJECT
    
        Q_PROPERTY(QStringList gpioNameList READ gpioNameList)
    
    public:
        explicit GpioBase(QObject *parent = nullptr);
    
        const QStringList &gpioNameList();
    
    signals:
    
    private:
        typedef enum gpioFunc {
            VCC = -1,
            IO,
            ALT
        } gpioFunc;
    
        struct gpio {
            int physical;
            int wPi;
            gpioFunc mode;
            QString altFunctionName;
            QString name;
        };
    
        const QVector<gpio> m_gpioList { // name (GPIOx), wPi
            gpio {1, -1, VCC, "", "3.3V"},
            gpio {2, -1, VCC, "", "5V"},
            gpio {3, 8, ALT, "SDA1", "GPIO 8"},
            gpio {4, -1, VCC, "", "5V"},
            gpio {5, 9, ALT, "SCL1", "GPIO 9"},
            gpio {6, -1, VCC, "", "0V"},
            gpio {7, 7, IO, "", "GPIO 7"},
            gpio {8, 15, ALT, "TxD", "GPIO 15"},
            gpio {9, -1, VCC, "", "0V"},
            gpio {10, 16, ALT, "RxD", "GPIO 16"},
            gpio {11, 0, IO, "", "GPIO 0"},
            gpio {12, 1, IO, "", "GPIO 1"},
            gpio {13, 2, IO, "", "GPIO 2"},
            gpio {14, -1, VCC, "", "0V"},
            gpio {15, 3, IO, "", "GPIO 3"},
            gpio {16, 4, IO, "", "GPIO 4"},
            gpio {17, -1, VCC, "", "3.3V"},
            gpio {18, 5, IO, "", "GPIO 5"},
            gpio {19, 12, ALT, "MOSI", "GPIO 12"},
            gpio {20, -1, VCC, "", "0V"},
            gpio {21, 13, ALT, "MISO", "GPIO 13"},
            gpio {22, 6, VCC, "", "GPIO"},
            gpio {23, 14, ALT, "SCLK", "GPIO 14"},
            gpio {24, 11, ALT, "CE0", "GPIO 10"},
            gpio {25, -1, VCC, "", "0V"},
            gpio {26, 11, ALT, "CE1", "GPIO 11"},
            gpio {27, 30, ALT, "SDA0", "GPIO 30"},
            gpio {28, 31, ALT, "SCL0", "GPIO 31"},
            gpio {29, 21, IO, "", "GPIO 21"},
            gpio {30, -1, VCC, "", "0V"},
            gpio {31, 22, IO, "", "GPIO 22"},
            gpio {32, 26, IO, "", "GPIO 26"},
            gpio {33, 23, IO, "", "GPIO 23"},
            gpio {34, -1, VCC, "", ""},
            gpio {35, 24, IO, "", "GPIO 24"},
            gpio {36, 27, IO, "", "GPIO 27"},
            gpio {37, 25, IO, "", "GPIO 25"},
            gpio {38, 28, IO, "", "GPIO 28"},
            gpio {39, -1, VCC, "", "0V"},
            gpio {40, 29, IO, "", "GPIO 29"}
        };
    
        QStringList m_gpioNameList;
    };
    
    }
    

    gpiobase.cpp

    using namespace Gpio;
    
    GpioBase::GpioBase(QObject *parent) : QObject(parent)
    {
        wiringPiSetup();
    
        // init list for gpio comboModel
        for(auto elem : m_gpioList) {
            if(elem.mode != VCC)
                m_gpioNameList.append(elem.mode == ALT ? QString("%1/%2").arg(elem.altFunctionName).arg(elem.name) : elem.name);
        }
    }
    
    const QStringList &GpioBase::gpioNameList()
    {
        qDebug() << Q_FUNC_INFO;
        return m_gpioNameList;
    }
    

    Can you tell me what I'm doing wrong or which structure is not supported by Qt?

    Thanks a lot!

    1 Reply Last reply
    0
    • K Offline
      K Offline
      Krulle
      wrote on last edited by
      #2

      Okay, i've found a solution.
      If change the parameter "gpiobase" to a pointer, it works.
      But i don't know why.
      Can anyone explain?

      core.h

      using namespace Gpio;
      
      class Core : public QObject
      {
          Q_OBJECT
      
          Q_PROPERTY(QPointer<GpioBase> gpioBase READ gpioBase CONSTANT)
      
      public:
          explicit Core(QObject *parent = nullptr);
          ~Core() override;
      
          QPointer<GpioBase> gpioBase() const;
      
      private:
          QPointer<GpioBase> m_gpioBase;
      
      
      };
      
      1 Reply Last reply
      0
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @Krulle said in Struggle with struct, const QVector and register to QML:

        Can anyone explain?

        You get the error because QObject's are not copyable. When you use a pointer nothing gets copied since its... a pointer.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        1 Reply Last reply
        1

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved