3
votes

I am creating a GUI using Qt. Basically it is an interactive map that shows robots moving in an arena in real time and allows the user to interact with the robots(tell them to go/stop) by using the mouse and keyboard keys. I used opengl to create the map itself and everything is working perfectly, I can see the robots moving on the map and I can press different keys on the keyboard and send the actual robots commands.

Now, I need to take this map and make it become a part of a bigger GUI that holds this map along with other objects as well,not all objects are necessarily using opengl. So, by using the Qt creator (designer) I have some dragged/dropped tabs in my GUI and I have also added a "dockwidget" in my GUI. The dockwidget holds in it the interactive map that I had created earlier. Now, however I can no longer send commands using my keyboard to my map. I can still click on different robots on my map and I can see that they get selected and change colors (as I coded it to do) but pressing keys have no corresponding actions(as it has been coded).

This is what the map by itself looks like. http://dl.dropbox.com/u/46437808/collision3.png

This is the map as a docked widget. (Inside the widge,I was able to click on one robot and make it turn yellow) https://www.dropbox.com/s/lpo43rl6z4268im/argHRI.png

So, my question is how do we direct keyboard input to a specific widget in a window when using Qt. From what I read that it might have to do with setting focus policy. So, I tried to set the focuspolicy of my dockwidget to "StrongFocus" (so that it can take keyboard input) in the constructor but that did not help.

Here is the code in which I'm setting my map as the dockwidget and I'm trying to set the focus as well.

    #include "ui_arghri.h"

argHRI::argHRI(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::argHRI)
{
    ui->setupUi(this);
    ui->dockMap->activateWindow();
    //ui->dockMap->keyboardGrabber();
    //ui->dockMap->grabKeyboard();
    ui->dockMap->setFocus();
    ui->dockMap->setFocusPolicy(Qt::StrongFocus);
}

argHRI::~argHRI()
{
    delete ui;
}

void argHRI::addMap(Map * map)
{

    qDebug()<<"argHRI::in AddMap test is "<<map->test;

    //ui->dockMap->show();
    ui->dockMap->setWidget(map);


}
1

1 Answers

1
votes

Add an event filter that handles KeyPress events to your class. There are examples here: http://doc.qt.io/archives/qt-4.7/eventsandfilters.html Just don't forget to add:

installEventFilter(this);

to the constructor or it won't work otherwise.