You are not logged in.

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

Mad Max

Trainee

  • "Mad Max" is male
  • "Mad Max" started this thread

Posts: 75

Location: Russia

  • Send private message

1

Thursday, September 15th 2005, 12:33pm

Problem with key event [SOLVED]

Hi all.
I have the form which contains a copy of class QListView. The list contains short information (for example about employes). For viewing the advanced information I have a dock window. To show this window I should intercept a space key. I.e. select the necessery record and press space bar.

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void frmMain::slotShowPassport()
{
  if (passportForm->isShown()) {
    removeDockWindow(passportForm);
    passportForm->hide();
  } else {
    addDockWindow(passportForm, Qt::DockRight);
    passportForm->show();
  }
}

void frmMain::keyPressEvent(QKeyEvent * e)
{
    switch (e->key()) {
    case Qt::Key_Space:
      slotShowPassport();
      break;
    default:
      break;
    }
}


All super, while the list empty. If I fill the list then frmMain::keyPressEvent method do not recieve QKeyEvent. Then I have made thus:

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
void frmMain::init()
{
   ...
   listView->installEventFilter(this);
   ...
}

bool frmMain::eventFilter(QObject * watched, QEvent * e)
{
  if ( watched == listView ) {
    if ( e->type() == QEvent::KeyPress ) {
      QKeyEvent * ev = dynamic_cast<QKeyEvent*>(e);
      switch (ev->key()) {
      case Qt::Key_Space:
	slotShowPassport();
	break;
      default:
	ev->accept();
	break;
      }
      return true;
    } else {
      return false;
    }
  } else {
    return QMainWindow::eventFilter( watched, e );
  }
}


It is worked, the dock window shows and hides perfect, but not work other keys ?( For example navigation under the list does not work. Tell me please, what is wrong?
I am using qt 3.3.5 on Mandrake 10.1

This post has been edited 1 times, last edit by "Mad Max" (Sep 16th 2005, 10:50am)


Mad Max

Trainee

  • "Mad Max" is male
  • "Mad Max" started this thread

Posts: 75

Location: Russia

  • Send private message

2

Friday, September 16th 2005, 10:29am

RE: Problem with key event

Quoted

Originally posted by Mad Max
All super, while the list empty. If I fill the list then frmMain::keyPressEvent method do not recieve QKeyEvent. Then I have made thus:

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
void frmMain::init()
{
   ...
   listView->installEventFilter(this);
   ...
}

bool frmMain::eventFilter(QObject * watched, QEvent * e)
{
  if ( watched == listView ) {
    if ( e->type() == QEvent::KeyPress ) {
      QKeyEvent * ev = dynamic_cast<QKeyEvent*>(e);
      switch (ev->key()) {
      case Qt::Key_Space:
	slotShowPassport();
	break;
      default:
	ev->accept();
	break;
      }
      return true;
    } else {
      return false;
    }
  } else {
    return QMainWindow::eventFilter( watched, e );
  }
}


It is worked, the dock window shows and hides perfect, but not work other keys ?( For example navigation under the list does not work. Tell me please, what is wrong?


I have understood where is wrong :D It should be so:

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void frmMain::init()
{
   ...
   listView->installEventFilter(this);
   ...
}

bool frmMain::eventFilter(QObject * watched, QEvent * e)
{
  if ( watched == listView ) {
    if ( e->type() == QEvent::KeyPress ) {
      QKeyEvent * ev = dynamic_cast<QKeyEvent*>(e);
      switch (ev->key()) {
      case Qt::Key_Space:
	slotShowPassport();
        return true; // !!!!!
      default:
	break;
      }
    }
  }
  return QMainWindow::eventFilter( watched, e );
}
I am using qt 3.3.5 on Mandrake 10.1