Question

I want to create a class which is derived from QLineEdit,but I can not assign a signal for Escape button.

The code was working until I add the cancel_signal() and then

LNK2019/LNK1120

errors are appeared.

How can I assign a signal for Escape button?


LineEditAlphaNum.h:

#ifndef _LINEEDIT_ALPHA_NUM_
#define _LINEEDIT_ALPHA_NUM_

#include <QtGui>

class LineEditAlphaNum : public QLineEdit
{
public:
    LineEditAlphaNum(QWidget* parent);

    void setPrevNextWidget(QWidget* prev, QWidget* next);

protected:
    void keyPressEvent(QKeyEvent *);

private:
    void keyLogic(QString& str, int key);

    int keyIndex;
    int lastKey;

    QWidget* m_pPrev;
    QWidget* m_pNext;

signals:
    void cancel_signal();
};

#endif // _LINEEDIT_ALPHA_NUM_

LineEditAlphaNum.cpp:

#include "LineEditAlphaNum.h"

LineEditAlphaNum::LineEditAlphaNum(QWidget *parent) :
    QLineEdit(parent),
    keyIndex(0),
    lastKey(0)
{
}

void LineEditAlphaNum::keyPressEvent(QKeyEvent *e)
{
    QString str = text();

    switch( e->key() )
    {
    case Qt::Key_Escape:
        emit cancel_signal();
    case Qt::Key_Up:
        m_pPrev->setFocus(Qt::OtherFocusReason);
        break;
    case Qt::Key_Down:
        m_pNext->setFocus(Qt::OtherFocusReason);
        break;
    case Qt::Key_Right:
        keyIndex = 0;
        lastKey = e->key();
        break;
    case Qt::Key_0:
    case Qt::Key_1:
    case Qt::Key_2:
    case Qt::Key_3:
    case Qt::Key_4:
    case Qt::Key_5:
    case Qt::Key_6:
    case Qt::Key_7:
    case Qt::Key_8:
    case Qt::Key_9:
        keyLogic(str, e->key());
        break;
    case Qt::Key_Backspace:
        str.remove(str.size()-1, 1);
        break;

    default:
        break;
    }

    setText(str);
}

void LineEditAlphaNum::keyLogic(QString& str, int key)
{
    char* Keys[] = {"0 ",
                    "1",
                    "2ABC",
                    "3DEF",
                    "4GHI",
                    "5JKL",
                    "6MNO",
                    "7PQRS",
                    "8TUV",
                    "9WXYZ"};

    char* keymap = Keys[ key - Qt::Key_0 ];
    int length = strlen(keymap);

    if ( lastKey == key )
    {
        keyIndex = (++keyIndex) % length;
        str.remove( str.size()-1, 1 );
    }
    else
    {
        keyIndex = 0;
        lastKey = key;
    }

    str.append( QChar( keymap[keyIndex] ) );
}
void LineEditAlphaNum::setPrevNextWidget(QWidget* prev, QWidget* next)
{
    m_pPrev = prev;
    m_pNext = next;
}
Was it helpful?

Solution

You got this linkage error because the signal is a C++ function, which hasn't been defined. This is normally done by the moc, which doesn't generate some code of your class.

Whenever you want to use signals and slots in QObject derived classes or need something else which uses the QMetaObject, you need to add

Q_OBJECT

after the opening curly braces of your class definition.

Note that when you add this macro, you also need to manually run qmake, since the QtCreator tries to be smart and skips this build step if the .pro file hasn't changed. But the pre- or absence of the Q_OBJECT macro has to be considered as a change for qmake, since moc (the meta object compiler) needs to run over all files having a Q_OBJECT macro in the class definition.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top