Results 1 to 3 of 3

Thread: KeyPressEvent()

  1. #1
    Join Date
    Nov 2017
    Posts
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default KeyPressEvent()

    Hello,

    I'm pretty new to Qt and wanted to make a small puzzle game.

    So I tried to use the KeyPressEvent() function to receive the keyboard input,
    but if I try to use some memberfunctions or variables of my game class inside the KeyPressEvent() function it doesn't work.
    It's written in Visual Studio.
    I hope that someone can tell me where I messed up.

    Here is a section of my programmtext:

    #include "game.h"
    #include "ui_game.h"

    game::game(QWidget *parent)
    : QMainWindow(parent)
    {
    ui.setupUi(this);

    scene = new QGraphicsScene(this);
    ui.graphicsView->setScene(scene);

    fDraw(boxPos);
    }

    int game::fPosCalc(int counter)
    {...}

    void game::fDraw(int field[3][3])
    {...}

    void QWidget::keyPressEvent(QKeyEvent *w)
    {
    game.boxPos[3][3] = 1;
    game.fDraw(boxPos)
    }
    Last edited by Knogger; 6th November 2017 at 01:55.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: KeyPressEvent()

    void QWidget::keyPressEvent(QKeyEvent *w)
    {
    game.boxPos[3][3] = 1;
    game.fDraw(boxPos)
    }
    This is one of the things that is "messed up". This keyPressEvent() method should be a protected method of the "game" class. You should read about "overloading" in C++; you should know that you don't override a virtual function of a base class by simply redefining it. You should also know that you can't call a member function of any class using the "dot" notation: game.fDraw() is nonsense - it won't even compile.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Nov 2017
    Posts
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: KeyPressEvent()

    I will look that up,

    Thank you very much for your advise.

Similar Threads

  1. keyPressEvent not being called...
    By bruceariggs in forum Newbie
    Replies: 3
    Last Post: 10th October 2011, 22:39
  2. top level keyPressEvent
    By mhoover in forum Qt Programming
    Replies: 1
    Last Post: 14th September 2010, 22:42
  3. keypressevent
    By djwk in forum Newbie
    Replies: 9
    Last Post: 5th July 2010, 03:12
  4. prob with keyPressEvent()
    By Askar in forum Qt Programming
    Replies: 3
    Last Post: 14th December 2009, 08:47
  5. Replace keyPressEvent (dot with tab)
    By the_bis in forum Qt Programming
    Replies: 1
    Last Post: 6th June 2008, 11:43

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.