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 6720895
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:15:57+00:00 2026-05-26T09:15:57+00:00

I have a QWidget derived class as such: class tetris_canvas : public QWidget {

  • 0

I have a QWidget derived class as such:

class tetris_canvas : public QWidget
{
    Q_OBJECT

public:
    tetris_canvas(QWidget * parent = 0);
    ~tetris_canvas();

protected:
    void paintEvent(QPaintEvent *event);
    void keyPressEvent(QKeyEvent *event);
};

//Never hits this keyPressEvent!!!
void tetris_canvas::keyPressEvent(QKeyEvent * event)
{
    if (event->key() == Qt::Key_Down)
    {
        rect->moveBottom(20);
        update();
    }
}

Then I have my main_window class:

class main_window : public QWidget
{
    Q_OBJECT

public:
    main_window(QWidget* parent = 0, Qt::WFlags flags = 0);
    ~main_window();

protected:
    void keyPressEvent(QKeyEvent * event);
};

//This keyPressEvent is hit!
void main_window::keyPressEvent(QKeyEvent* event)
{
    if (event->key() == Qt::Key_Escape)
    {
        QApplication::exit(0);
    }
    QWidget::keyPressEvent(event);
}

My question is, how do I get the keyPressEvent in my tetris_canvas widget to respond to a key press?

I am drawing inside that canvas and I need to respond to keypresses so the user can interact with things on that canvas.

The widget is added to a QGridLayout in the ctor or my main_window class.

  • 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-05-26T09:15:58+00:00Added an answer on May 26, 2026 at 9:15 am

    The QWidget::keyPressEvent says this:

    A widget must call setFocusPolicy() to accept focus initially and have focus in order to receive a key press event.

    So you should do that. (Since you’re not showing your constructors, I’m guessing you missed that part.)

    Also the line after that says:

    If you reimplement this handler, it is very important that you call the base class implementation if you do not act upon the key.

    You’re missing that in your widget, but doing it in your main window. Make sure you do it in both places.

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

Sidebar

Related Questions

I have a KMainWindow: //file.h class MainWindow: public KMainWindow { public: MainWindow(QWidget *parent =
I have two widgets defined as follows class mainWindow : public QWidget { Q_OBJECT
I have this class: class CustomEdit : public QTextEdit { Q_GADGET public: CustomEdit(QWidget* parent);
I have a class like this class GUI : public QWidget, public QThread When
I have some constructor for the class LCDRange: LCDRange::LCDRange(QWidget *parent) : QWidget(parent) { QLCDNumber
I have a QWidget -derived class. In the constructor I say: setPalette(QPalette(QColor(250,250,200))); setAutoFillBackground(true); Then
I have the following Qt class: class GLWidget : public QGLWidget { Q_OBJECT public:
I have a certain QWidget derived class. Look at the window flags i'm giving
In a Qt program, I have a QWidget class that is the superclass of
I have a class which displays waveform data of audiofiles in a QWidget (see

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.