Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Adapt OpenGL Code to Qt (especially keyPressEvent)
Forum Updated to NodeBB v4.3 + New Features

Adapt OpenGL Code to Qt (especially keyPressEvent)

Scheduled Pinned Locked Moved General and Desktop
9 Posts 3 Posters 5.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    basil_fawlty
    wrote on last edited by
    #1

    I have a working program in OpenGL which shall now work under Qt. Some things are a bit different and I am struggling right now with the keypressevent-function. I have a global variable which should be changed by pressing a key (later by presing a button).
    @
    int modus = -1;

    void GLWidget::keyPressEvent(QKeyEvent* event)
    {
        switch (event->key())
        {
        case Qt::Key_F2 :
            modus = 1;
            updateGL();
            break; 
        default:
            break;
        }
    }
    

    @
    This changed variable should be used in mousePressEvent()
    @
    void GLWidget::mousePressEvent(QMouseEvent* e)
    {
    if (modus == 1 && (e->buttons() & Qt::LeftButton))
    {
    counter++;
    p_list[counter].x = mx - width1/2;
    p_list[counter].y = my - height1/2;
    glPointSize(5);
    glColor3d(1, 0, 0);
    updateGL();
    }
    }
    @
    Thanks for your help

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      Could you specify what actually seems to be the problem? The variable is not being set in QKeyEvent or what? Also, where exactly is 'modus' declared? Do you want those events to fire in parallel, or sequentially (from the text, I guess it's sequential, in which case your approach seems to be OK)?

      (Z(:^

      1 Reply Last reply
      0
      • B Offline
        B Offline
        basil_fawlty
        wrote on last edited by
        #3

        The variable is not being set in QKeyEvent! Yes, thats what is my problem. I want to use the variable in the mousePressEvent, but the value isnt changing.I have defined the variable modus globally.

        Thanks

        1 Reply Last reply
        0
        • sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          Strange. I would blame the variable definition, but you say it worked in pure OpenGL app. Have you tried debugging to see what really happens?

          (Z(:^

          1 Reply Last reply
          0
          • B Offline
            B Offline
            basil_fawlty
            wrote on last edited by
            #5

            Thanks for your reply.
            It seems that i dont get into the function keypressevent. Even print-commands are not displayed. There is no reaction by clicking any button. I mean, even if i try different buttons in the code.It is strange. Any ideas?

            1 Reply Last reply
            0
            • L Offline
              L Offline
              loladiro
              wrote on last edited by
              #6

              That's very, odd. Does the mouse event function work/get called (i.e is it only the keyPressEvent) ?

              1 Reply Last reply
              0
              • sierdzioS Offline
                sierdzioS Offline
                sierdzio
                Moderators
                wrote on last edited by
                #7

                OK I've searched the docs a bit, and here is one possible cause:
                A widget must call setFocusPolicy() to accept focus initially and have focus in order to receive a key press event.

                I'll search some more now.

                (Z(:^

                1 Reply Last reply
                0
                • L Offline
                  L Offline
                  loladiro
                  wrote on last edited by
                  #8

                  That might indeed be the problem.
                  You can easily check which widget is the keyboard grabber by calling "QWidget::keyboardGrabber();":http://doc.qt.nokia.com/4.7/qwidget.html#keyboardGrabber

                  1 Reply Last reply
                  0
                  • B Offline
                    B Offline
                    basil_fawlty
                    wrote on last edited by
                    #9

                    Thanks for your replies. The problem was that i used the keypressfunction in the wrong class. My mistake. Nevertheless I thank you for your anwers.

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved