Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8128779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T08:02:04+00:00 2026-06-06T08:02:04+00:00

I want to create a class which is derived from QLineEdit ,but I can

  • 0

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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T08:02:05+00:00Added an answer on June 6, 2026 at 8:02 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create a Class, say MyDiv , which inherits from the original
I want to create a class that initializes a timer which will be used
I want to create one new model class, which will be extended in all
I have a class derived from slider which uses a custom control template and
I have a base class that inherits List<> which has multiple classes derived from
I need to create a mapping from objects of my own custom class (derived
I want to create a number of exception types which derive from std::runtime_error and
I want to create a class for storing attributes of the many data files
I want to create a class with a nested enum. public class Foo {
I want to create whois class like that public class DomainInfo { public string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.