March 14, 2011

jgroleau jgroleau
Lab Rat
1 posts

QsqlTableModel/QtableView Event Filter Capture

 

I am trying to develop an email reader for my visually impaired father who is 94. I am using QsqlTableModel and QtableView to create a list of email files from the dbx file. The table displays 3 columns for Subject, From, and Date. This works fine but since the minimum font size he can read is 34, the text is truncated in each column. I would like to improve the design by allowing him to see all the text under the selected row of the table in a temporary text box similar to a qtooltip. The two
approaches I have tried are Mouse Over(Hover ) and MouseButtonPress. In the Mouse Over approach I would like the text to display when the mouse cursor is over the selected row/column(i.e. cell) and disappear when the mouse cursor is not over the selected row/column (i.e. cell). The following code snippet is what I have tried:

  1. TableEditor::TableEditor(const QString &tableName, QWidget *parent)
  2.     : QDialog(parent)
  3. {
  4. setAttribute(Qt::WA_Hover,true);
  5. //Also tried these
  6. //setMouseTracking(true);
  7. //view->viewport()->setMouseTracking(true);
  8. //view->setMouseTracking(true);
  9. //view->viewport()->setAttribute(Qt::WA_Hover,true);
  10. //view->setAttribute(Qt::WA_Hover,true);
  11.  
  12. // This code allows total row to be highlighted and disable editing
  13. view->setSelectionBehavior(QAbstractItemView::SelectRows);
  14. view->setSelectionMode(QAbstractItemView::SingleSelection);
  15. view->setEditTriggers(QAbstractItemView::NoEditTriggers);    //Could this cause the problem
  16. .
  17. .
  18. .
  19. void TableEditor::mouseHoverEvent(QHoverEvent *event)
  20. {
  21.   if ((event->type() == QHoverEvent::HoverEnter)) {
  22.    accept();
  23.      //Put code here to open and display dialog (I open message box for testing  for testing purposes)
  24.      update();
  25.   }
  26.   else if ((event->type() == QHoverEvent::HoverLeave)) {
  27.  accept();
  28.      //put code here to close dialog (I open message box for testing for testing purposes)
  29.      update();
  30.   }
  31. }

I don’t seem to be capturing the event which I think the setAttribute(Qt::WA_Hover,true) is supposed to do? I also do not understand the logic necessary to trigger the event when the mouse cursor is over the selected row? Could it be the code that disables edit triggers that is causing the problem?

The second approach I tried is to detect a right mouse button press event using an event filter. I would like the text to display when the right mouse button is pressed and held over a selected cell. The text would diappear when the right mouse button is released. The code snippet code snippet I have so far for this is:

  1. TableEditor::TableEditor(const QString &tableName, QWidget *parent)
  2.     : QDialog(parent)
  3. {
  4. .
  5. .
  6. .
  7. view -> installEventFilter(this);
  8. .
  9. .
  10. bool TableEditor::eventFilter(QObject *obj, QEvent *event)
  11.  {
  12.      if (obj == view) {
  13.          if (event->type() == QEvent::MouseButtonPress) {  //This if does not capture MouseButtonPress event type does work for KeyPress
  14.  QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
  15.  if ( mouseEvent->button() == Qt::RightButton ) {
  16.   //Put code here to open text (I open message box for testing purposes)
  17.                return true;
  18.  }
  19.          } else {
  20.              return false;
  21.          }
  22.          if (event->type() == QEvent::MouseButtonRelease) {
  23.  QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
  24.  if ( mouseEvent->button() == Qt::RightButton ) {
  25.   //Put code here to open text (I open message box for testing purposes)
  26.                return true;
  27.  }
  28.          } else {
  29.              return false;
  30.          }
  31.      } else {
  32.          // pass the event on to the parent class
  33.         // return TableEditor::eventFilter(obj, event);
  34.  return false;
  35.      }
  36.  }

Again I do not seem to be capturing the event. The if (event->type() == QEvent::MouseButtonPress) does not capture MouseButtonPress event type. It does work for KeyPress. I also need to add logic to make sure I am over selected row.

Any help anyone can give me would be appreciated. Thanks.

edit: Mark up code, Tobias

2 replies

March 15, 2011

Andre Andre
Robot Herder
6395 posts

Perhaps it would be easier to use QML? It is easier to adapt it to non-standard form factors and display methods than the standard item views.

January 24, 2012

neel2818 neel2818
Lab Rat
57 posts

First You have to do installEventFilter On the widget to get the events ftom widget.

 
  ‹‹ QTabWidget not displayed      Sound and the using of the Resource system ››

You must log in to post a reply. Not a member yet? Register here!