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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:09:01+00:00 2026-05-23T11:09:01+00:00

i think of to write primitive snake. i have window where program paints random

  • 0

i think of to write primitive snake. i have window where program paints random lines.

but i can’t think up how to catch pressed key to change direction of painted line.

enter image description here

class QUpdatingPathIte : public QGraphicsPathItem
{
    void advance(int phase)
    {

        if (phase == 0)
            return;

        int x,y;
        int w,a,s,d;

        char c;
        // 
        // HOW TO    MAKE THIS HERE? (i had done early in console application)
        // but i can't do this in GUI , what should i  do?

        scanf("%c",&c);
        if (c=='w')
        { x=-20; y=0; }
        else if (c=='s')
        { x=20; y=0; }
        else if (c=='a')
        { x=0; y=-20; }
        else if(c=='d')
        { x=0; y=20; }

        QPainterPath p = path();
        p.lineTo(x, y);
        setPath(p);
    }
};

#include <QtGui/QApplication>
#include "dialog.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QGraphicsScene s;
    QGraphicsView v(&s);

    QUpdatingPathIte item;
    item.setPen(QPen(QColor("black")));
    s.addItem(&item);
    v.show();

    QTimer *timer = new QTimer(&s);
    timer->connect(timer, SIGNAL(timeout()), &s, SLOT(advance()));
    timer->start(500);

    return a.exec();
}
  • 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-23T11:09:02+00:00Added an answer on May 23, 2026 at 11:09 am

    QGraphicsView inherits from from QWidget because it inherits from QAbstractScrollArea. You should create a custom subclass of QGraphicsView and just override the function keyPressEvent(). Example:

    class SnakeView : public QGraphicsView
    {
    protected:
        keyPressEvent(QKeyEvent *e)
        {
             // Do something
    
             // Otherwise pass to the graphics view
             QGraphicsView::keyPressEvent(e)
        }
    };
    

    Then rather than creating a QGraphicsView object you would create a SnakeView object.

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

Sidebar

Related Questions

I think I can write a QObject like this by taking advantage of the
I like MySQL's Query Analyzer ...but not the price tag. I think I can
I have been trying to figure out how to write a simple program to
just think that when I opened my file then when I want to write
I'm trying to think of clever, clear, and simple ways to write code that
I wrote a custom ordering LINQ extension method as below but I think it
I used to think that write() system call is unbuffered and that fwrite and
Can I write the bytes of a CGPathRef to disk and then read them
I have a Linux USB HID device (a Hama MCE ), and I can
I have this scenario, which I think must be pretty common: class Parameter {

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.