Page 1 of 2 12 LastLast
Results 1 to 20 of 28

Thread: another setMouseTracking(true) not working; QWidget

  1. #1
    Join Date
    Apr 2007
    Location
    Rakovnik, Czech Republic
    Posts
    175
    Thanks
    43
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default another setMouseTracking(true) not working; QWidget

    I am trying to enable mouseTracking in a widget subclassed from QWidget. Here's the subclassed QWidget

    Qt Code:
    1. //.h
    2. #include <QWidget>
    3. #include <Inventor/Qt/SoQt.h>
    4.  
    5. class Model;
    6.  
    7. class CoinWidget : public QWidget
    8. {
    9. Q_OBJECT
    10.  
    11. protected:
    12. void mouseMoveEvent(QMouseEvent *event);
    13. ...
    14.  
    15. //.cpp
    16. CoinWidget::CoinWidget(QWidget *parent) : QWidget(parent)
    17. {
    18. SoQt::init(this);
    19. model = new Model(this);
    20. this->setMouseTracking(true); //first attempt to setMouseTracking(true)
    21. }
    22.  
    23. void CoinWidget::mouseMoveEvent(QMouseEvent *event)
    24. {
    25. this->setMouseTracking(true); //second
    26. qDebug("%d %d", event->x(), event->y());
    27. }
    To copy to clipboard, switch view to plain text mode 

    and the constructor for the main window which instantiates the CoinWidget object:

    Qt Code:
    1. myWindow::myWindow()
    2. {
    3. coinWidget = new CoinWidget;
    4. coinWidget->setMouseTracking(true); //third
    5. this->setMouseTracking(true); //forth
    6. .
    7. .
    8. .
    To copy to clipboard, switch view to plain text mode 

    um, as you can see I've tried to setMouseTracking(true) 4 different ways, none of which is successful. I saw in the setMouseTracking/QCanvasView posts, you need to do it via the viewport(). But what about my example: if the widget in which you want to enable mouseTracking is subclassed from QWidget?

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: another setMouseTracking(true) not working; QWidget

    Are you trying to receive mouse move events on the coin widget?
    Have you installed any event filters on it? Because usually it is not required to do this. It receives mouse move events by default...

    Regards

  3. #3
    Join Date
    Apr 2007
    Location
    Rakovnik, Czech Republic
    Posts
    175
    Thanks
    43
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: another setMouseTracking(true) not working; QWidget

    Quote Originally Posted by marcel View Post
    Are you trying to receive mouse move events on the coin widget?
    yes, only on the CoinWidget. I want to pass event->x() and event->y() on to another part of the program. The only way I know how to get their values is to include a QMouseEvent. The problem is: I can only get the values when the mouse button is pushed. Are you suggesting there's another way to get these values, whether or not a mouse button is pushed?


    Quote Originally Posted by marcel View Post
    Have you installed any event filters on it? Because usually it is not required to do this.
    hmmm, there are no event filters on it (unless QMouseEvent is considered a "filter").

  4. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: another setMouseTracking(true) not working; QWidget

    So mouseMoveEvent does not get called. That is not normal.
    Are you sure there is nothing covering the widget?

    Perhaps more code would help.

    Regards

  5. #5
    Join Date
    Apr 2007
    Location
    Rakovnik, Czech Republic
    Posts
    175
    Thanks
    43
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: another setMouseTracking(true) not working; QWidget

    mouseMoveEvent *is* getting called... but only when the button is pushed. I think I'm not using setMouseTracking(true) properly.

  6. #6
    Join Date
    Apr 2007
    Location
    Rakovnik, Czech Republic
    Posts
    175
    Thanks
    43
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: another setMouseTracking(true) not working; QWidget

    my bad (perhaps): i *do* have an event filter for the myWindow class (the main Window, in which CoinWidget is instantiated), something like:

    Qt Code:
    1. bool Window::eventFilter(QObject *obj, QEvent *event)
    2. {
    3. if(event->type() == QEvent::Enter)
    4. {
    5. if(obj == button_with_popup_widget[0])
    6. {
    7. panel[0]->show();
    8. return QWidget::eventFilter(obj, event);
    9. }
    10. else
    11. {
    12. return QWidget::eventFilter(obj, event);
    13. }
    14. }
    15. else if(event->type() == QEvent::Leave)
    16. {
    17. if(if(obj == panel[0])
    18. {
    19. panel[0]->hide();
    20. return QWidget::eventFilter(obj, event);
    21. }
    22. else
    23. {
    24. return QWidget::eventFilter(obj, event);
    25. }
    26. }
    27. else
    28. {
    29. return QWidget::eventFilter(obj, event);
    30. }
    31. }
    To copy to clipboard, switch view to plain text mode 

    ...I wouldn't think this is the problem, though, since mousing over the CoinWidget window shouldn't be caught by this filter (which catches some mousing over specific buttons not in the CoinWidget window).

  7. #7
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: another setMouseTracking(true) not working; QWidget

    No, here must be it. What class is the main window? Is it QWidget or QMainWindow?
    Because you should call QMainWindow::eventFilter not QWidget.

    And the common practice is to test first for the target object and then for the event type, not the other way around. Take a look at the eventFilter documentation. There is an example there.

    Regards

  8. #8
    Join Date
    Apr 2007
    Location
    Rakovnik, Czech Republic
    Posts
    175
    Thanks
    43
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: another setMouseTracking(true) not working; QWidget

    Quote Originally Posted by marcel View Post
    No, here must be it. What class is the main window? Is it QWidget or QMainWindow?
    Because you should call QMainWindow::eventFilter not QWidget.
    actually, the main window class is QWidget, not QMainWindow. (I have a couple questions about this that I'll post in another thread later.) Anyway, since the main Window is a QWidget, I assume it's correct to call QWidget::eventFilter?

    Quote Originally Posted by marcel View Post
    And the common practice is to test first for the target object and then for the event type, not the other way around. Take a look at the eventFilter documentation. There is an example there.
    I'll look more into this. The myWidget example here tests event type first, then object, but perhaps this is a special case. But this doesn't seem to be the problem, anyway: when I comment the eventFilter out of my Main Window, the mouseEvent is still behaving the same way. In other words, this function:

    Qt Code:
    1. void CoinWidget::mouseMoveEvent(QMouseEvent *event)
    2. {
    3. this->setMouseTracking(true);
    4. qDebug("%d %d", event->x(), event->y());
    5. }
    To copy to clipboard, switch view to plain text mode 

    is only called when I press/hold a mouseButton *before* moving the mouse. Thanks for helping me narrow it down, though...

  9. #9
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Thanks
    37
    Thanked 53 Times in 40 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: another setMouseTracking(true) not working; QWidget

    I feel you still have something wrong in your eventFilter code. Did you comment out the function both in declaration and definition or just commented function body ? Try calling return QWidget::eventFilter(obj,e); in the body of event filter defined by you in the beginning itself instead of just commenting.

    If everything is proper may be you have installed event filter for QApplication, did you check for this ?

    The following example works fine for me
    Qt Code:
    1. #include <QtGui>
    2.  
    3.  
    4. class Widget : public QWidget
    5. {
    6. public:
    7. Widget() : QWidget()
    8. {
    9. setMouseTracking(true);
    10. }
    11. void mouseMoveEvent(QMouseEvent *e)
    12. {
    13. te->append(QString("%1,%2\n").arg(e->pos().x()).arg(e->pos().y()));
    14. QWidget::mouseMoveEvent(e);
    15. }
    16. };
    17.  
    18. int main(int argc, char *argv[])
    19. {
    20. QApplication app(argc,argv);
    21. Widget w;
    22. w.show();
    23. te = new QTextEdit;
    24. te->show();
    25. return app.exec();
    26. }
    To copy to clipboard, switch view to plain text mode 

    EDIT: Actually your event filter code seems to be proper(even without commenting). So my guess is there is one more event filter(may be installed on qapp) which is causing malfunction in your case.
    Last edited by Gopala Krishna; 3rd September 2007 at 19:25.
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  10. #10
    Join Date
    Apr 2007
    Location
    Rakovnik, Czech Republic
    Posts
    175
    Thanks
    43
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: another setMouseTracking(true) not working; QWidget

    ...this has become hugely frustrating. Thanks, GK- yeah, your example worked for me, too. I tinkered w/the HelloGL, as well, and it works. I even included the glwidget.h/.cpp file in my own application (i replaced coinWidget with the glWidget), turned on MouseTracking, at it works.

    But nothing I do with coinWidget will work! I even tried commenting the moveMouseEvent feature in the mainWindow class, then editing the mainWindow eventFilter like so:

    Qt Code:
    1. mainWindow::mainWindow()
    2. {
    3. coinWidget = new CoinWidget;
    4. coinWidget->setMouseTracking(true);
    5. coinWidget->installEventFilter(this);
    6. .
    7. .
    8. .
    9.  
    10. bool mainWindow::eventFilter(QObject *obj, QEvent *event)
    11. {
    12. if(event->type() == QEvent::MouseMove)
    13. {
    14. qDebug("eventFilter: hello");
    15. return QWidget::eventFilter(obj, event);
    16. }
    17. else
    18. {
    19. return QWidget::eventFilter(obj, event);
    20. }
    21. }
    To copy to clipboard, switch view to plain text mode 

    but still only see "eventFilter: hello" if I click/drag the mouse button (on coinWidget). Other widgets with both installEventFilter() and setMouseTracking(true), however, work as expected.

    So: the problem has to be my coinWidget class. This is a container widget for a coin3d scenegraph. Perhaps the SoQt widget embedded in the coinWidget is grabbing the moveMouseEvent even before Qt can? But I wouldn't think this could happen. Anyway, I'm posting the coinWidget code below. See anything???

    Qt Code:
    1. //+++++++++++ coinwidget.h
    2. #ifndef COINWIDGET_H
    3. #define COINWIDGET_H
    4.  
    5. #include <QWidget>
    6. #include <Inventor/Qt/SoQt.h>
    7.  
    8.  
    9. class Model; // the class defining the coin3d geometry
    10.  
    11. class CoinWidget : public QWidget
    12. {
    13. Q_OBJECT
    14.  
    15. public:
    16. CoinWidget(QWidget *parent = 0);
    17. ~CoinWidget();
    18.  
    19. QSize minimumSizeHint() const;
    20. QSize sizeHint() const;
    21.  
    22. public slots:
    23.  
    24. void lyBG(int value);
    25. void vuBG(int value);
    26. void lyImgSL(int objID, int value);
    27. void setComp(int value);
    28. void zXagSL(int objID, int value);
    29. void setRotAxis();
    30. void vuSterSL();
    31. void vuSterAcSL(int value);
    32. void vuDrawAcSL(int value);
    33. void vuOrthoSL();
    34. void vuModLocSL();
    35.  
    36.  
    37. signals:
    38.  
    39.  
    40. protected:
    41.  
    42. // void mouseMoveEvent(QMouseEvent *event);
    43. // above: deleted. Now only included in the mainWindow class
    44.  
    45. Model *model;
    46.  
    47. };
    48. #endif
    49.  
    50.  
    51. //+++++++++ coinwidget.cpp
    52. #include <QtGui>
    53. #include <QtCore>
    54. #include <Inventor/Qt/SoQt.h>
    55. #include <Inventor/Qt/viewers/SoQtExaminerViewer.h>
    56.  
    57. #include "coinwidget.h"
    58. #include "model.h"
    59.  
    60. CoinWidget::CoinWidget(QWidget *parent) : QWidget(parent)
    61. {
    62. SoQt::init(this);
    63.  
    64. model = new Model(this);
    65. model->setDefaultScene();
    66.  
    67. // this->setMouseTracking(true); // no longer included here;
    68. // i do this in the mainWindow class
    69.  
    70. }
    71.  
    72. CoinWidget::~CoinWidget() { }
    73.  
    74. QSize CoinWidget::minimumSizeHint() const {
    75. return QSize(200, 200);
    76. }
    77.  
    78. QSize CoinWidget::sizeHint() const {
    79. return QSize(400, 400);
    80. }
    81.  
    82.  
    83. void CoinWidget::vuBG(int value)
    84. {
    85. model->vuBG(value);
    86. }
    87.  
    88.  
    89. void CoinWidget::lyBG(int value)
    90. {
    91. model->lyBG(value);
    92. }
    93.  
    94.  
    95. void CoinWidget::setComp(int value)
    96. {
    97. model->setComp(value);
    98. }
    99.  
    100.  
    101. void CoinWidget::lyImgSL(int objID, int value)
    102. {
    103. model->lyImgSwi(objID, value);
    104. }
    105.  
    106.  
    107. void CoinWidget::zXagSL(int objID, int value)
    108. {
    109. model->set_zXag(objID, value);
    110. }
    111.  
    112.  
    113. void CoinWidget::setRotAxis()
    114. {
    115. model->setRotAxis();
    116. }
    117.  
    118.  
    119. void CoinWidget::vuSterSL()
    120. {
    121. model->vuSter();
    122. }
    123.  
    124.  
    125. void CoinWidget::vuSterAcSL(int value)
    126. {
    127. model->vuSterType(value);
    128. }
    129.  
    130.  
    131. void CoinWidget::vuDrawAcSL(int value)
    132. {
    133. model->vuDrawType(value);
    134. }
    135.  
    136.  
    137. void CoinWidget::vuOrthoSL()
    138. {
    139. model->vuOrtho();
    140. }
    141.  
    142.  
    143. void CoinWidget::vuModLocSL()
    144. {
    145. model->vuModLoc();
    146. }
    147.  
    148. /*
    149. void CoinWidget::mouseMoveEvent(QMouseEvent *event)
    150. {
    151.  
    152.   qDebug("CoinWidget:: %d %d", event->x(), event->y());
    153.  
    154. }
    155. */
    To copy to clipboard, switch view to plain text mode 

  11. #11
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: another setMouseTracking(true) not working; QWidget

    This is a container widget for a coin3d scenegraph. Perhaps the SoQt widget embedded in the coinWidget is grabbing the moveMouseEvent even before Qt can?
    Well, you didn't mention this before.

    This is the reason. It happened to me lots of times.
    The fix is to install an event filter on the child wiidget. Do what you want with the event, but make sure to forward it to the child, in case it needs it.

    Regards

  12. #12
    Join Date
    Apr 2007
    Location
    Rakovnik, Czech Republic
    Posts
    175
    Thanks
    43
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: another setMouseTracking(true) not working; QWidget

    I was all set to donate a few kopeks to the Ceauseascu Center for Historical Revisionism in your honor... when I realized I don't know what you're talking about (yet).

    When you say

    install an event filter on the child wiidget.
    ...make sure to forward it to the child
    I tried that (i think), above: "coinWidget->installEventFilter(this);" ...or what am I missing?

  13. #13
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: another setMouseTracking(true) not working; QWidget

    Forget Ceausescu .
    What you're missing (I think) is that you have to install an event filter for the widget contained within the coin widget(I don't know what is called), because that is the one that's eating all the mouse move events for your widget.

    So, what do you have inside the coin widget?

    Regards

  14. #14
    Join Date
    Apr 2007
    Location
    Rakovnik, Czech Republic
    Posts
    175
    Thanks
    43
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: another setMouseTracking(true) not working; QWidget

    the coinWidget constructor is basically:

    Qt Code:
    1. CoinWidget::CoinWidget(QWidget *parent) : QWidget(parent)
    2. {
    3.  
    4. SoQt::init(this); // initializing the coin3d render window
    5.  
    6. model = new Model(this); // the coin3d scenegraph
    7. model->setDefaultScene(); // set up default 3d scene
    8.  
    9. }
    To copy to clipboard, switch view to plain text mode 

    and the constructor for Model:

    Qt Code:
    1. Model::Model( QWidget *parent, const char * name, const SbBool embed)
    2. : SoQtExaminerViewer( parent, name, embed, SoQtFullViewer::BUILD_POPUP, SoQtViewer::BROWSER, TRUE)
    3. {
    4.  
    5. QWidget *widget = this->buildWidget(this->getParentWidget());
    6. this->setBaseWidget(widget);
    7.  
    8. }
    To copy to clipboard, switch view to plain text mode 

    so you're suggesting that I add an eventFilter to the Model class to intercept the mouseMovements? I'll try this. It does seem odd that I can intercept the mouseMovements at the application/mainWindow level, but only if the mouse-button is pushed/dragged.

    ok, we'll bag Ceauseascu for now. ...maybe Vlad? I just listened to a podcast about the famed Impaler a couple days ago. tisk tisk: everyone knows the humane way to slaughter folks is from a B-52. (sorry, gettin' off-topic here)

  15. #15
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: another setMouseTracking(true) not working; QWidget

    Install the event filter on the baseWidget of Model.
    Does it have a getter for it?

    Regards

  16. #16
    Join Date
    Apr 2007
    Location
    Rakovnik, Czech Republic
    Posts
    175
    Thanks
    43
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: another setMouseTracking(true) not working; QWidget

    I don't understand what the BaseWidget is. It looks to me like in:

    QWidget *widget = this->buildWidget(this->getParentWidget());

    widget is a pointer to the coinWidget class/object? So

    this->setBaseWidget(widget);

    is setting baseWidget to the coinWidget? I don't know what a getter is.

  17. #17
    Join Date
    Apr 2007
    Location
    Rakovnik, Czech Republic
    Posts
    175
    Thanks
    43
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: another setMouseTracking(true) not working; QWidget

    void SoQtComponent::setBaseWidget(QWidget * widget) [protected]

    Set the core widget for this SoQt component. It is important that subclasses get this correct, as the widget set here will be the widget returned from query methods.
    from the SoQtExaminerViewer documentation.


    EDIT: isn't "the widget set here" in my case the CoinWidget?

    QWidget * SoQtFullViewer::buildWidget (QWidget * parent ) [protected]

    This method builds the component contents in the given parent widget. For subclasses adding new user interface items, this method is typically overridden in the following manner:

    QWidget * MyOwnViewer::buildWidget(QWidget * parent)
    {
    QWidget * superw = <superclass>::buildWidget(parent);
    // [then move superw within MyOwnViewer framework and add own
    // user interface components]
    }

  18. #18
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: another setMouseTracking(true) not working; QWidget

    If it has a setter( setBaseWidget) then it also should have a getter( getBaseWidget or baseWidget).

    You need a function in the model that returns the widget created in the constructor( widget ). The event filter has to be installed on that widget.

    Regards

  19. #19
    Join Date
    Apr 2007
    Location
    Rakovnik, Czech Republic
    Posts
    175
    Thanks
    43
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: another setMouseTracking(true) not working; QWidget

    gotcha. yeah, there's a getBaseWidget.

  20. #20
    Join Date
    Nov 2006
    Location
    Shrewsbury, UK
    Posts
    97
    Thanks
    3
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: another setMouseTracking(true) not working; QWidget

    I've sort of been watching this thread from a distance. I'm at work a the moment so I can't view the Coin3d, or the code we played with a while back, source, but in the original thread starter on http://www.qtcentre.org/forum/f-newb...lots-6895.html you used to have a line
    Qt Code:
    1. SoQtExaminerViewer *eviewer = new SoQtExaminerViewer(this);
    To copy to clipboard, switch view to plain text mode 
    that created a SoQtExaminerViewer within the CoinWidget. This effectively overlays a coin opengl rendering window over your QWidget, and hooks in the Coin model. I've been trying to read the coin source SVN respository but its responding slowly - but its possible that on Windows this is done by subclassing the native WindowProc. The Coin3d / OIV viewers don't inherit from QWidget, so event won't bubble up the heirarchy chain - I'll take look at the coin source tonight on my pc to check how its linked in. But some of the coin examples show classes inherited from SoQt...Viewer as well as QWidget.

    This might mean that trying to hook a Qt event filter won't work in this way, and this is what your seeing.

    Pete

    p.s Missed all the latest posts!
    Last edited by pdolbey; 5th September 2007 at 14:10. Reason: postscript added

Similar Threads

  1. Problem with Parent QWidget and Buttons not working
    By VireX in forum Qt Programming
    Replies: 7
    Last Post: 11th May 2007, 22:24

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.