2
votes

I have a QTableView where a small mark appears if the mouse hover on active cell. This mark is a widget and emit a signal if hover, changing the selecion mode of the table when I drag over the table. The problem is that if I am over the mark, I can't drag over the table. Things I have tried:

  • Set the widget mark like setWindowFlags(Qt::WindowTransparentForInput);, but I can't use it because I need hover event.
  • Ignore the events in the widget mark using event->ignore() or sending the event to parent using and eventFilter:

    bool EventFilterMarca::eventFilter(QObject *obj, QEvent *event)
    {
         if( event->type() == QEvent::HoverMove)
         {
           ....
         }
         else if (event->type() == QEvent::MouseButtonPress ||
                              event->type() == QEvent::MouseButtonRelease ||
                              event->type() == QEvent::MouseMove ||
                              event->type() == QEvent::MouseButtonDblClick)
                    {                
                        //QApplication::sendEvent(parent(),event);//one try
                        //event->ignore();//another try
                        return QObject::eventFilter(obj,event);;
                    }
            }
    
  • Subclassing mousePressEvent, mouseReleaseEvent and mouseMoveEvent in the mark widget and call to parent class. Looks like if it works (pass the event to parent) into the current cell of the table (the parent):

void Marca::mousePressEvent(QMouseEvent *event)

{
    //event->setAccepted(false);
    if(event->buttons() == Qt::LeftButton)
    {
        MiTabla* tabla = qobject_cast<MiTabla*>(parent());
        if (tabla)
        {
            tabla->mousePressEvent(event);
            //QApplication::sendEvent(parent(),event);
        }
    }
    //event->ignore();
}

Well, the question is how could get that the behaviour of the table was the same if I am over the widget of the cell or directly on the cell.

Also I add a link with my first approach to getting it. It works but the code is awful and not easy to follow:

https://github.com/exodehm/tablacalc

1
One way to deal with the issue would be to have the mark be drawn into the table-cell directly (via a custom QItemDelegate subclass) rather than implementing it as a separate widget. That way it would never interfere when mouse-event handling.Jeremy Friesner
@JeremyFriesner. Thank you. I'll try using delegates. I think that If the mark it's only a picture, the relationship between the mark and the beauviour of the table can be not clear. Actually I made a version that works, but the code is awful. I going to update the question for add the link to my first (and awful) solutionuser3733164

1 Answers

0
votes

I think the event filter is not what you want. This is what makes a widget not receive specific events. What you actually want to do is to mark the event to be unhandled by specific widget, putting it higher in the class for handling. In order to do that you should try and reimplement either the widgets ::event method, or more specific handler.

In qt docs it says that:

bool QWidget::event(QEvent *event)

This function returns true if the event was recognized, otherwise it returns false. If the recognized event was accepted (see QEvent::accepted), any further processing such as event propagation to the parent widget stops.

So what I think you should be doing is basically marking event as not accepted when specific event type you want to propagate to parent widget happens for your widget.

So in your case I would expect something like:

void QWidget::dropEvent(QDropEvent *event) {
   event->setAccepted(false);
}

Also in order for this to work your table needs to be parent widget of the mark widget.