Important: Please read the Qt Code of Conduct -

basic variables problems with Qt or C++

  • I'm facing a basic problem (because of the lack of knowledge about c++) regarding declaring some variables in Qt. I know is basic but believe me that 1+1 is a very difficult problem for the person that doesn't know it....
    here you have my pseudo code that I want to archive:

    struct command {  //I have about 200 commands which are  with:
        char : device_address; // about 20 different devices addreses
       char : device_pin_no; // 200 different pin address (on the different device they will be the same) 0x05 on dev 0x020 and 0x05 on 0x21 and so on
    enum parameter { // the list with all the parameter for the commands that can be taken by all the devices
        OFF = 0, // logic pin LOW
        ON = 1, // Logic pin HI
        ALLOFF = 2 // all logic pin LOW - on that device
        ALLON = 3 // all logic pin HI - on that device
    Q_ENUM(parameter )
    enum commands { // the list of all commands supported - they have to be of the type command declared above (how can I do that??? enum of a specific type)
        comm1 // command no 1
        comm2 // command no 2
        comm3 // command no 3
        com200 // command no 200
    Q_ENUM(command )

    now when I'm receiving the command I have a case that does that command

    QMetaObject metaObject = this->staticMetaObject;
    	QMetaEnum metaEnum = metaObject.enumerator(metaObject.indexOfEnumerator("commands"));
    		case comm1: // please note that I'm receiving the commands over the network as a QString
    			process_command(comm1, parameter);
    		case comm2: // please note that I'm receiving the commands over the network as a QString
    			process_command(comm2, parameter);
    		default:	//Command not recognized

    can you help me in this basic matter???
    Thanking you

  • @arsinte_andrei said in basic variables problems with Qt od in c++:

    please note that I'm receiving the commands over the network as a QString

    Hi! What's the format of that string? Can you give an example?

  • @Wieland yes the string is coming as QByteArray eg. command:parameter. I interpret it in a function like

    QString holeString= QString::fromLatin1(data);
    	QStringList commandAndParameter = sir.split(":");

    and then processing it

  • so what I wish to do is that to have on each command - assign it's own device address and it's own pin
    so when the user send


    I should have something like

    [comm1] [address for device @comm1] [pin no on that device for comm1]

  • @arsinte_andrei said in basic variables problems with Qt or C++:

    QString holeString= QString::fromLatin1(data);
    QStringList commandAndParameter = sir.split(":");

    data is "comm1:ALLOF", literally as a string, or do you send binary encodes addresses, pin numbers?

  • everything at the moment is Qbytearray or give me a better option if you know... I'm using tcp server with qsocket

  • Mhh, what do you think about this:

    #include <QApplication>
    #include <QDebug>
    enum class command_t : uint16_t
        comm_1 = 0x2342, // address = 0x23, pin = 0x42
        comm_2 = 0xB00B,
        comm_3 = 0xDEAD,
        comm_4 = 0xBEEF,
    enum class parameter_t : uint8_t
        off = 0,
        on = 1,
        all_off = 2,
        all_on = 3,
    void run_command_1(parameter_t p) { qDebug() << "comm_1, parameter =" << static_cast<int>(p); }
    void run_command_2(parameter_t p) { qDebug() << "comm_2, parameter =" << static_cast<int>(p); }
    void run_command_3(parameter_t p) { qDebug() << "comm_3, parameter =" << static_cast<int>(p); }
    void run_command_4(parameter_t p) { qDebug() << "comm_4, parameter =" << static_cast<int>(p); }
    using command_callback_t = void (*)(parameter_t);
    using command_callback_map_t = std::map<command_t, command_callback_t>;
    using command_and_parameter_t = std::pair<command_t, parameter_t>;
    command_callback_map_t make_supported_commands()
        command_callback_map_t m;
        m[command_t::comm_1] = &run_command_1;
        m[command_t::comm_2] = &run_command_2;
        m[command_t::comm_3] = &run_command_3;
        m[command_t::comm_4] = &run_command_4;
        return m;
    void network_send(command_and_parameter_t const &) { /* ... */ }
    command_and_parameter_t network_receive()
        command_and_parameter_t commandAndParameter;
            // simulate receiving comm3 / all_off from the network
            commandAndParameter.first = command_t::comm_3;
            commandAndParameter.second = parameter_t::all_off;
        return commandAndParameter;
    int main(int argc, char *argv[])
        QApplication a(argc, argv);
        auto const supported_commands = make_supported_commands();
        auto const commandAndParameter = network_receive();
        try {
        } catch (std::out_of_range) {
            qDebug() << "command not supported";
        return 0;

  • @arsinte_andrei
    many thanks for your effort to provide me with a working code, your suggestion is good, but I'm not always using the same config for the command so I have to accept some of your proposed - like using the commands in hex like comm_1 = 0x23 but from here I have to be careful at what I'm sending because if my first command is login(username, passowrd) or addNewRoom("RoomName); then the param is different. I wish to stick with the QByteArray at the moment and to have parameters as 0x04 // pin no and 0x01 // pin state
    so it will be like
    [command] ([devId][pinNo][pinState]) or
    [command]([username][separationChar \r][password]) or
    [command]([name...strings anything]

    still thinking on the best option for implementation...

  • a working bit...

    #include <QCoreApplication>
    #include <QDebug>
    #define SEPARATOR_CHAR 0x01
    #define START_CHAR 0x02
    #define END_CHAR 0x03
    #define LOG_IN 0x0A
    #define ADD_NEW_ROOM 0x0B
    #define ADD_SWITCH_DEVICE 0x0C
    #define ADD_UP_DOWN_DEVICE 0x0D
    #define SWITCH_ON_OFF 0x0E
    enum class pinState : char {
    	OFF = 0,
    	ON = 1,
    	ALLOFF = 2,
    	ALLON = 3,
    QByteArray socketReturn(){
    	QByteArray raspuns;
    	raspuns.append(QByteArray::number(LOG_IN, 16));
    	return raspuns;
    void doLogIn(QByteArray username, QByteArray password){
    	qDebug() << "LOGIN";
    	qDebug() <<username <<" - " <<password;
    		password = password.remove(password.indexOf(END_CHAR), 1);
    	qDebug() <<username <<" - " <<password;
    void doAddNewRoom(QByteArray roomName) {
    	qDebug() << "doAddNewRoom";
    	qDebug() <<roomName;
    void doAddSwitchDevice(QByteArray devId, QByteArray devPin, QByteArray deviceName){
    	qDebug() << "doAddSwitchDevice";
    	qDebug() <<devId <<" - " <<devPin << " - " <<deviceName;
    void doAddUpAndDownDevice(QByteArray devId, QByteArray devPinUp, QByteArray devPinDown, QByteArray deviceName){
    	qDebug() << "doAddUpAndDownDevice";
    	qDebug() <<devId <<" - " <<devPinUp <<" - " <<devPinDown << " - " <<deviceName;
    void doSwitchOnOff(QByteArray devId, QByteArray devPin, QByteArray pinMode){
    	qDebug() << "doSwitchOnOff";
    	qDebug() <<devId <<" - " <<devPin <<" - " <<pinMode;
    void doDefault() {
    	qDebug() << "DEFAULT"; //logOut, disconnect and other stuff - unlegal call
    int main(int argc, char *argv[]) {
    	QCoreApplication a(argc, argv);
    	QByteArray comm = socketReturn();
    	QByteArrayList myList = comm.split(SEPARATOR_CHAR);
    	int receivedCommand;
    	qDebug() << comm << " -- " << myList;
    		receivedCommand = myList[0].remove(0,1).toInt(nullptr, 16);
    		qDebug() << "Comanda primita" <<receivedCommand;
    		switch (receivedCommand) {
    			case LOG_IN: doLogIn(myList[1], myList[2]);
    			case ADD_NEW_ROOM: doAddNewRoom(myList[1]);
    			case ADD_SWITCH_DEVICE: doAddSwitchDevice(myList[1], myList[2], myList[3]);
    			case ADD_UP_DOWN_DEVICE: doAddUpAndDownDevice(myList[1], myList[2], myList[3], myList[4]);
    			case SWITCH_ON_OFF: doSwitchOnOff(myList[1],myList[2], myList[3]);
    			default: doDefault();
    	return 0;

    do not forget this is work in progress

  • @arsinte_andrei said in basic variables problems with Qt or C++:

    200 commands

    Hi 200 commands its not so simply ....
    I have generated association from 240 language..
    ID from google language to make associate in QT5.9 QLocale...
    search "DATALANGUAGEPROVIDEQT591_H" in file:

    I love struct data in C++..

Log in to reply