qtqt5qcolor

Qt5 error: ‘ColorKeyMap’ does not name a type


I am writing a class which has the following structure:

Header File:

#pragma once

#include <QtCore>
#include <QtGui>
#include <QtWidgets>

class VirtualButton : public QWidget {
    Q_OBJECT

    public:
        VirtualButton( QWidget *parent );

    private:
        static QMap<unsigned int, QColor> ColorKeyMap;
        static QList<unsigned int> goodKeys;
};

CppFile

#include "VirtualButton.hpp"

QMap<unsigned int, QColor> VirtualButton::ColorKeyMap = QMap<unsigned int, QColor>();
ColorKeyMap[ 23 ] = QColor( 0xff, 0x00, 0xff );

QList<unsigned int> VirtualButton::goodKeys = QList<unsigned int>() << 50 << 62 << 37 << 133 << 64 << 108 << 135 << 109;

VirtualButton::VirtualButton( QWidget *parent ) : QWidget( parent ) {

    setFixedSize( 48, 48 );
};

int main( int argv, char **argv ) {

    QApplication app( argc, argv );

    VirtualButton *btn = new VirtualButton();
    btn->show();

    return app.exec()
}

When I compile this code I get the following error:

VirtualKeyboard.cpp:4:1: error: ‘ColorKeyMap’ does not name a type; did you mean ‘QColormap’?
 ColorKeyMap[ 23 ] = QColor( 0xff, 0x00, 0xff );
 ^~~~~~~~~~~
 QColormap

Why does this error come up? I can see that goodKeys has no issue, but ColorKeyMap does. Is this because QColor should not be used outside the class?


Solution

  • There were a couple of mistakes in your code.

    1. The main problem is that you want to modify for a certain *key, namely 23 the value of color, namely QColor( 0xff, 0x00, 0xff );

      The problem is although you can initialize a private static member outside of your class like you did, you cannot modify it later. You cannot even read it later outside the class. You can only access it in the future through a member function of the class VirtualButton.

    Fortunately, there is work around. You can use a static function (namely initColorKeyMap()) to initialize your static member ColorKeyMap, as shown in the code below.

    virtualbutton.h

    #pragma once
    
    #include <QtWidgets/QtWidgets>
    #include <QMap>
    #include <QList>
    
    class VirtualButton : public QWidget {
        Q_OBJECT
    
    public:
        VirtualButton( QWidget *parent );
    
    private:
        static QMap<unsigned int, QColor> initColorKeyMap();
        static QMap<unsigned int, QColor> ColorKeyMap;
        static QList<unsigned int> goodKeys;
    };
    

    virtualbutton.cpp

    #include "virtualbutton.h"
    
    QMap<unsigned int, QColor> VirtualButton::initColorKeyMap() {
        QMap<unsigned int, QColor> temp = QMap<unsigned int, QColor>();
        temp[ 23 ] = QColor( 0xff, 0x00, 0xff );
        return temp;
    }
    QMap<unsigned int, QColor> VirtualButton::ColorKeyMap = VirtualButton::initColorKeyMap();
    
    QList<unsigned int> VirtualButton::goodKeys = QList<unsigned int>() << 50 << 62 << 37 << 133 << 64 << 108 << 135 << 109;
    
    VirtualButton::VirtualButton( QWidget *parent ) : QWidget( parent ) {
        setFixedSize( 48, 48 );
    }
    

    main.cpp

    #include "virtualbutton.h"
    #include <QtCore>
    
    int main(int argc, char *argv[])
    {
        QApplication app(argc, argv);
        QWidget window;
        window.resize(320, 240);
        window.setWindowTitle(QApplication::translate("childwidget", "Child widget"));
        window.show();
    
    //    QPushButton *button = new QPushButton(
    //        QApplication::translate("childwidget", "Press me"), &window);
    
        VirtualButton *button = new VirtualButton(&window);
        // button->move(100, 100);
        button->show();
        return app.exec();
    }
    

    Other than that the code compiles without a problem.