{"id":106,"date":"2013-08-28T20:29:52","date_gmt":"2013-08-29T01:29:52","guid":{"rendered":"http:\/\/eastfist.com\/qt_tutorials\/?p=106"},"modified":"2017-08-26T03:27:47","modified_gmt":"2017-08-26T09:27:47","slug":"solution-qt-setmousetracking-doesnt-work","status":"publish","type":"post","link":"https:\/\/eastfist.com\/qt_tutorials\/solution-qt-setmousetracking-doesnt-work\/","title":{"rendered":"Solution: Qt setMouseTracking Doesn&#8217;t Work"},"content":{"rendered":"<p>By default, Qt will set any QWidget into a QMainWindow unless you set it yourself. This is possibly why you can&#8217;t <a href=\"http:\/\/qt-project.org\/doc\/qt-4.8\/qwidget.html#mouseTracking-prop\">setMouseTracking<\/a> in your default QWidget. You need to enable setMouseTracking if you want real-time mouse position tracking; it is disabled by default. This is how I got it to work:<!--more--><\/p>\n<pre><code>MainWindow::MainWindow(QWidget *parent):\r\n    QMainWindow(parent),\r\n    ui(new Qt::MainWindow)\r\n{\r\n\r\n   ui-&gt;setupUI(this);\r\n\r\n   this-&gt;setMouseTracking(true);\r\n   ui-&gt;centralWidget-&gt;setMouseTracking(true);\r\n\r\n}\r\n<\/code><\/pre>\n<p>If you were working on a custom widget and later instantiated as a child, then this-&gt;setMouseTracking should work. Note, it is redundant to set it twice, but you must do it this way. I&#8217;ve tested it and it doesn&#8217;t work without the other.<\/p>\n<p>To see an example of a functioning implementation, take a look at my <a href=\"http:\/\/eastfist.com\/qt_tutorials\/overriding-qwidget-events\/\">Overriding QWidget Events<\/a> tutorial.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>By default, Qt will set any QWidget into a QMainWindow unless you set it yourself. This is possibly why you can&#8217;t setMouseTracking in your default QWidget. You need to enable setMouseTracking if you want real-time mouse position tracking; it is disabled by default. This is how I got it to work:<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27],"tags":[16,62,56,66,51,55,60,59,58,53,61,63,3,50,57,54,19,65,64,52],"class_list":["post-106","post","type-post","status-publish","format-standard","hentry","category-qt-2","tag-4-8-5","tag-broken","tag-constructor","tag-default","tag-doesnt","tag-false","tag-fix","tag-help","tag-howto","tag-isnt","tag-problem","tag-qmainwindow","tag-qt","tag-setmousetracking","tag-solution","tag-true","tag-tutorial","tag-widget","tag-window","tag-work"],"_links":{"self":[{"href":"https:\/\/eastfist.com\/qt_tutorials\/wp-json\/wp\/v2\/posts\/106","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/eastfist.com\/qt_tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/eastfist.com\/qt_tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/eastfist.com\/qt_tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/eastfist.com\/qt_tutorials\/wp-json\/wp\/v2\/comments?post=106"}],"version-history":[{"count":7,"href":"https:\/\/eastfist.com\/qt_tutorials\/wp-json\/wp\/v2\/posts\/106\/revisions"}],"predecessor-version":[{"id":325,"href":"https:\/\/eastfist.com\/qt_tutorials\/wp-json\/wp\/v2\/posts\/106\/revisions\/325"}],"wp:attachment":[{"href":"https:\/\/eastfist.com\/qt_tutorials\/wp-json\/wp\/v2\/media?parent=106"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/eastfist.com\/qt_tutorials\/wp-json\/wp\/v2\/categories?post=106"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/eastfist.com\/qt_tutorials\/wp-json\/wp\/v2\/tags?post=106"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}