Seite 1 von 1
keyReleaseEvent funktioniert nicht!
Verfasst: 28. August 2007 17:23
von slash-ex
Ich habe in einer Klasse, die ich von QWidget abgeleitet habe die key-event Funktionen überladen. Leider wird diese anscheinend nie aufgerufen... weiß jemand was ich da beachten muss, oder woran es liegen könnte, dass es anscheinend einfach nicht klappen will? Ein MouseEvent funktioniert jedenfalls wunderbar?!
Code: Alles auswählen
//!27.08.2007
void Battlefield::keyPressEvent(QKeyEvent *event)
{
std::cout<<"pressed: "<<std::endl;
/*
if(abbruchbedingung) {
keyPressEvent(event);
return;
}
*/
switch (event->key()) {
case Qt::Key_M:
current_action = bfa_move_unit;
break;
default:
keyPressEvent(event);
}
}
//!27.08.2007
void Battlefield::keyReleaseEvent(QKeyEvent *event)
{
std::cout<<"released: "<<std::endl;
/*
if(bedingung) {
keyPressEvent(event);
return;
}
*/
switch (event->key()) {
case Qt::Key_M:
current_action = bfa_set_unit;
break;
default:
keyPressEvent(event);
}
}
Re: keyReleaseEvent funktioniert nicht!
Verfasst: 28. August 2007 17:47
von gerome69
Was steht denn in deinem Header? Fehlt vielleicht "Q_OBJECT"?
Oder bekommt dein Widget niemals den Keyboard-Focus?
Gérôme
Verfasst: 28. August 2007 18:01
von slash-ex
Nicht erschrecken, die Klasse ist recht vollgepackt
Wenn jemand will, poste ich gerne noch die Implementation...
Code: Alles auswählen
#ifndef BATTLEFIELD_H
#define BATTLEFIELD_H
#include "army/Model.h"
#include "army/Unit.h"
//#include "battlefield/Dice.h"
#include <cmath>
#include <QtGui>
#include <iostream>
enum battlefield_action
{
bfa_no_action,
bfa_set_arrow,
bfa_move_unit, //!unit soll bewegt werden (reichweite wird angezeigt, etc...
bfa_set_unit, //!nach mouse-release wird unit bewegt!
//bfa_single_dice,
bfa_measure,
bfa_addUnit,
bfa_setZoom
};
class Battlefield : public QWidget
{
Q_OBJECT
public:
Battlefield(QWidget *parent = 0);
int add_unit(Unit *unit);
void deploy_unit(int id, const QPointF &pos);
//QList <short> dice_values;
public slots:
void action_requested(int action);
void cleanup();
void addUnitFrLst(Unit *unit);
signals:
void measure_done(double);
void addingUnit_done();
void arrow_done();
private slots:
void show_context_menu(QPoint pos);
void move_unit();
private:
int UnitFrListId;
QList <Unit *> units;
QList <Model *> models;
//QList <Dice *> dice_list;
QList <int> models_selected;
QSizeF table_size; // Tischgr��e in Zoll
QPointF arrow[2]; // angezeigter Pfeil, Start- und Endpunkt
double zoom_factor; // Verh�ltnis Pixel pro Zoll
float zoom_level; //zoom durch mousewheel
int add_model(int owner);
//int add_dice(short value, QPointF pos);
int current_action;
int unit_selected; // Aktuell ausgew�hlte Einheit
int moving_unit;
double move_dist;
void calc_zoom_factor();
void acquire_wh_relation();
void deselect_all();
void init_context_menu();
void select_unit(int id);
void select_model(int id);
void enable_unit_controls(bool enable=true);
void set_arrow(QPointF from, QPointF to) {arrow[0] = from; arrow[1] = to; arrow_active=true;};
bool mouse_down;
bool arrow_active;
QPointF mouse_down_at;
QPointF mouse_pos;
QList<QPointF> mouse_stack;
QPointF UnitMovVect;
QList <QPointF> moving_unit_model_offsets;
QMenu *context_menu;
QMenu *unit_menu;
QAction *ct_cleanup_field;
QAction *unit_move_unit;
void transformPainter(QPainter &painter);
protected:
void paintEvent(QPaintEvent *event);
void resizeEvent(QResizeEvent *event);
void mouseMoveEvent(QMouseEvent *event);
void mousePressEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event);
void mouseDoubleClickEvent(QMouseEvent *event);
void wheelEvent(QWheelEvent *event);
//!27.08.2007
void keyPressEvent(QKeyEvent *event);
void keyReleaseEvent(QKeyEvent *event);
};
#endif
Hier noch der Konstruktor-Impl.:
Code: Alles auswählen
Battlefield::Battlefield(QWidget *parent) : QWidget(parent)
{
//! 24.08.2007
zoom_level = 1;
//!
setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
setContextMenuPolicy(Qt::CustomContextMenu);
init_context_menu();
setMinimumWidth (72 * 8);
setMinimumHeight (48 * 8);
mouse_down = false;
arrow_active = false;
moving_unit = -1;
current_action = bfa_no_action;
table_size = QSizeF(72.0, 48.0);
Unit * test = new Unit("test", 100, 6, 10, "");
int id = add_unit(test);
deploy_unit(id, QPoint(30, 30));
enable_unit_controls(false);
}
Verfasst: 28. August 2007 20:18
von gerome69
Q_OBJECT ist drin, da würde ich mal tippen, daß dein Widget einfach nicht den Keyboard-Focus hat. G.
Verfasst: 28. August 2007 20:32
von slash-ex
Hmm, wie setzt man einen keyboard-focus? Ich suche mal solange im Assistant. setFocus() im Konstr. scheint nicht zu klappen.
Verfasst: 28. August 2007 20:57
von gerome69
slash-ex hat geschrieben:Hmm, wie setzt man einen keyboard-focus? Ich suche mal solange im Assistant. setFocus() im Konstr. scheint nicht zu klappen.
http://doc.trolltech.com/4.3/focus.html
http://doc.trolltech.com/4.3/qwidget.ht ... olicy-prop
http://doc.trolltech.com/4.3/qfocusevent.html
G.