PDA

View Full Version : Signals & Slots question



JPNaude
22nd August 2008, 10:22
Hi

I'm struggling to get certain signal and slot combinations to work. I've used signals and slots successfully for a while now but at the moment I'm stuck with drag & drop operations. I want to be able to drag from a QTableView object and I connect the necessary signals in the way shown below:


connect(tableViewWorkspace,SIGNAL(mousePressEvent( QMouseEvent *event)),this,SLOT(mousePressEventWorkspace(QMouse Event *event)));

This does not seems to work and when I click in the table view it never enters the mousePressEventWorkspace function. Do I do something wrong? I know that mousePressEvent is a function of the QAbstractItemView class, but shouldn't it be called for the tableView object as well? Do I perhaps need some type of pointer along with some object casting?

Thanks
Jaco

wysota
22nd August 2008, 10:29
1. Have you seen our FAQ?

http://www.qtcentre.org/forum/faq.php?faq=qt_signalslot#faq_qt_signalslot_with_n ames

2. events are not signals nor slots, so your connect statement has no chance to work even if you correct the issue mentioned in (1). If you want some method to be called when an event handler is executed, simply reimplement the event and call you method there or use event filters.

spirit
22nd August 2008, 10:31
connect(tableViewWorkspace,SIGNAL(mousePressEvent( QMouseEvent *event)),this,SLOT(mousePressEventWorkspace(QMouse Event *event)));
at first, there is no signal with name mousePressEvent.
at second, you mustn't specify parameters.

read this for details
http://doc.trolltech.com/4.4/signalsandslots.html

JPNaude
22nd August 2008, 10:51
Thanks wysota.

I missed the fact that the events are not signals by default. It might seem like a stupid question, but I'm not sure how to reimplement the event. Should I subclass the QTableView class and implement these functions there and use this class rather than the current QTableView (I thought about this but I thought there should be an easier way), or how is it possible to reimplement the events in the main window class?

wysota
22nd August 2008, 10:54
You can either subclass the table and reimplement its event handler or install an event filter on it so that all its events are passed to another object before reaching the destination (see QObject::installEventFilter).

JPNaude
22nd August 2008, 12:38
Hi again Wysota

The event filter works well and I'm able to track all the events that happen for any control now. The problem now is that none of the mouse events are triggered for a tableView object. My event filter looks as follows:


bool mainWindow::eventFilter(QObject *obj, QEvent *event)
{
if (obj == this->tableViewWorkspace) {
if (event->type() == QEvent::MouseButtonPress) {
QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
if (mouseEvent->button() == Qt::LeftButton)
// Commands
return false;
} else if (event->type() == QEvent::MouseMove) {
// etc.

I log all these events (code for that not shown above) and the eventFilter function is never called for an tableView object. For example, if I press the right mouse button it issues QEvent::Context menu, but never QEvent::MouseButtonPress.

There is probably a reason for this, since the mouse press event is called for other objects in my GUI.

Does anyone know what the reason is for that?

spirit
22nd August 2008, 12:46
after handeling of event you should return true if you don't need that parent widget process handled event and false in another case.

wysota
22nd August 2008, 12:49
Did you use installEventFilter()? Also don't return false here. Return QMainWindow::eventFilter(obj, event) instead.

JPNaude
22nd August 2008, 13:00
Thanks, I changed the return value now.

However this does not solve the problem. I did install the event filter for the QTableView correctly because it logs all sorts of events for it (paint, focus related events etc.). The eventFilter does not get called for mouse related events. To verify this I've done the following:


bool mainWindow::eventFilter(QObject *obj, QEvent *event)
{
if (event->type() == 2) {
loggerSingleton::Instance()->addDebugString("found a key press for object " + obj->objectName(),this);
this->updateMessagesAndLogs();
}

Now when I click on different GUI objects for which I've installed this filter the debug message get locked. However when I click on the table view, it does not log this message so the eventFilter function is not called. It does not make sense to me at all since the keyPressEvent documentation for the QTableView does not state anything unusual.

wysota
22nd August 2008, 13:12
You probably want to install the event filter on the viewport() of the table...

JPNaude
22nd August 2008, 14:33
Yes! Finally. Thanks it works now.

viasant
27th August 2008, 17:34
Yes! Finally. Thanks it works now.
How did you solve it?
I get this problem too. Can you post the codes?

JPNaude
1st September 2008, 19:51
bool mainWindow::eventFilter(QObject *obj, QEvent *event)
{
/* if (event->type() == QEvent::DragEnter) {
loggerSingleton::Instance()->addDebugString("found a drag enter for object " + obj->objectName(),this);
this->updateMessagesAndLogs();
}*/

if ((obj == this->tableViewWorkspace->viewport()) || (obj == this->tableViewWorkspace)) {
this->updateMessagesAndLogs();
if (event->type() == QEvent::MouseButtonPress) {