Disable mouse wheel scroll on QscrollArea - python

I would like to simply disable mouse wheel scroll on QScrollArea, in order to scroll down only by clicking on right scrollbar, but I can't find any solution on the Internet.
app = QtGui.QApplication([])
sa = pg.QtGui.QScrollArea()
win = pg.GraphicsWindow()
sa.setWidget(win)
The problem is that I have a lot of graphs in my scroll area, and when I try to mousewheel on one of them, the page will scroll up or down together with the graph.
I can't find a method to call on "sa" to disable mouse wheel scroll.
I found some posts discussing about install event filter but I can't understand how to use them in this case. For example, I tried to use this:
sa.viewport().installEventFilter(???)
but I really didn't understand what arguments to pass and how to check the event.
Thank you in advance if you can help me with this problem.

You have the right idea. Event-filtering requires an object that inherits QObject to watch for the relevant events. Such objects have an eventFilter method which can be overriden to provide custom handing of all events for the watched object. If this method returns True for a given event, it will not be propagated any further. Usually the main-window is used to provide the event-filtering, like this:
import sys
from PyQt4 import QtCore, QtGui
class Window(QtGui.QWidget):
def __init__(self):
super(Window, self).__init__()
self.scroll = QtGui.QScrollArea()
self.widget = QtGui.QGraphicsView()
self.widget.setFixedSize(600, 600)
self.scroll.setWidget(self.widget)
layout = QtGui.QVBoxLayout(self)
layout.addWidget(self.scroll)
self.scroll.viewport().installEventFilter(self)
def eventFilter(self, source, event):
if (event.type() == QtCore.QEvent.Wheel and
source is self.scroll.viewport()):
return True
return super(Window, self).eventFilter(source, event)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
window = Window()
window.setGeometry(600, 100, 400, 300)
window.show()
sys.exit(app.exec_())

I finally managed to resolve this, using this solution:
class Scroller(pg.QtGui.QScrollArea):
def __init__(self):
pg.QtGui.QScrollArea.__init__(self)
def wheelEvent(self, ev):
if ev.type() == QtCore.QEvent.Wheel:
ev.ignore()
app = QtGui.QApplication([])
sa = Scroller() # <======
win = pg.GraphicsWindow()
sa.setWidget(win)

Related

Increasing avaliable space in main window

I am trying to create an application using pyqt python.Application's Main window is filled with many dock widgets, some dock widgets are just used to list certain string data. These widgets are occupying more space.
the drawer to the left in the image is my interest. That drawer opens on mouse click.
Is there any way I could hide these widgets to the side of main window and open when mouse is hovered over it?
or if you know any pyqt UI element which could do this. please suggest.
The logic is to detect the desired event and show the widget, in the following example the click on the QGraphicsView is detected and then the QDockWidget that was initially hidden is shown.
from PyQt5 import QtCore, QtGui, QtWidgets
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.dock_widget = QtWidgets.QDockWidget()
self.addDockWidget(QtCore.Qt.RightDockWidgetArea, self.dock_widget)
list_widgets = QtWidgets.QListWidget()
list_widgets.addItems(["item{}".format(i) for i in range(100)])
self.dock_widget.setWidget(list_widgets)
self.scene = QtWidgets.QGraphicsScene(self)
self.view = QtWidgets.QGraphicsView(self.scene)
it = self.scene.addRect(QtCore.QRectF(0, 0, 300, 400))
it.setBrush(QtGui.QColor("white"))
self.view.viewport().installEventFilter(self)
self.setCentralWidget(self.view)
self.dock_widget.hide()
self.resize(640, 480)
for i in range(4):
self.menuBar().addAction("Action{}".format(i))
def eventFilter(self, obj, event):
if obj is self.view.viewport():
if event.type() == QtCore.QEvent.MouseButtonPress:
self.dock_widget.show()
return super().eventFilter(obj, event)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())

PyQt: Emit signal when cell entered in QCalendarWidget

In my Qt application I'm using the QCalendarWidget and I would like to get notified when the mouse enters a new cell of the calendar. I know that the QCalendarWidget is using a QTableView internally which inherits from QAbstractItemView and this has an entered signal:
This signal is emitted when the mouse cursor enters the item specified
by index. Mouse tracking needs to be enabled for this feature to work.
I tried to receive the signal with following code:
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class MyCalendar:
def __init__(self):
app = QApplication(sys.argv)
window = QMainWindow()
cal = QCalendarWidget(window)
window.resize(320, 240)
cal.resize(320, 240)
table = cal.findChild(QTableView)
table.setMouseTracking(True)
table.entered.connect(self.onCellEntered)
window.show()
sys.exit(app.exec_())
def onCellEntered(self, index):
print("CellEntered")
if __name__ == "__main__":
window = MyCalendar()
But my callback function is never called. Do you have any ideas why?
The QCalendarWidget class uses a custom table-view which bypasses the normal mouse-event handlers - so the enetered signal never gets emitted. However, it is possible to work-around that by using an event-filter to emit a custom signal that does the same thing.
Here is a demo script that implements that:
import sys
from PyQt4 import QtCore, QtGui
class Window(QtGui.QWidget):
cellEntered = QtCore.pyqtSignal(object)
def __init__(self):
super(Window, self).__init__()
self.calendar = QtGui.QCalendarWidget(self)
layout = QtGui.QVBoxLayout(self)
layout.addWidget(self.calendar)
self._table = self.calendar.findChild(QtGui.QTableView)
self._table.setMouseTracking(True)
self._table.installEventFilter(self)
self._index = None
self.cellEntered.connect(self.handleCellEntered)
def eventFilter(self, source, event):
if source is self._table:
if event.type() == QtCore.QEvent.MouseMove:
index = QtCore.QPersistentModelIndex(
source.indexAt(event.pos()))
if index != self._index:
self._index = index
self.cellEntered.emit(QtCore.QModelIndex(index))
elif event.type() == QtCore.QEvent.Leave:
self._index = None
return super(Window, self).eventFilter(source, event)
def handleCellEntered(self, index):
print(index.row(), index.column())
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
window = Window()
window.setGeometry(600, 100, 300, 200)
window.show()
sys.exit(app.exec_())
I've investigated a bit and I think I know why this happens.
QCalendarWidget creates a private subclass of QTableView called QCalendarView and instantiates it as a child of itself. (This instance is called qt_calendar_calendarview.)
If you look at QCalendarView's code (Qt 5), you'll see this:
void QCalendarView::mouseMoveEvent(QMouseEvent *event)
{
[...]
if (!calendarModel) {
QTableView::mouseMoveEvent(event);
return;
}
[...]
}
This means only if there is no calendarModel, the superclasses mouseMoveEventis called which is responsible for emitting the entered signal. All of this is an implementation detail of `QCalendarWidget, so it's probably best not to rely on any of this anyway.
So for a way around this, I'm not sure what the best approach is. You'll have to catch the event before it gets to the table. This can be done using QObject.installEventFilter() or re-implementing QWidget.mouseMoveEvent(), but then you don't get the model index directly. You could probably use QAbstractItemView.indexAt() for this purpose.

Implement dragMoveEvent on QWidget in pyqt5?

does anyone know how I can implement the dragMove event on my QWidget? So basically what I want is to move my mouse over the Widget hold down my mouse button and drag it. While dragging, the widget should not be moved it should only capture the mouse coordinates while the mouse is pressed.
I have already googled and just find some drag and drop tutorials where they have dragged something into a widget etc. like text. This wasn't really helpful.
This has got nothing to do with dragging. What you actually need to do is enable mouse-tracking and then monitor mouse-move events.
Here's a simple demo:
from PyQt5 import QtCore, QtGui, QtWidgets
class Window(QtWidgets.QWidget):
def __init__(self):
super(Window, self).__init__()
self.setMouseTracking(True)
def mouseMoveEvent(self, event):
if event.buttons() & QtCore.Qt.LeftButton:
print(event.globalPos().x(), event.globalPos().y())
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
window = Window()
window.setGeometry(500, 150, 100, 100)
window.show()
sys.exit(app.exec_())
I think you are looking for mousePressEvent rather than dragMoveEvent. You would need to subclass QWidget and implement the mousePressEvent method providing your implementation:
from PyQt5.QtWidgets import QWidget
class MyWidget(QWidget):
def mousePressEvent(self, event):
print(event.pos())

Qt/PyQt: How do I use a QMenu as a permanent widget?

I would like to use a QMenu as a permanent widget in the gui. (I like its appearance and layout, and the fact that as soon as I hover over it, the requisite menu pops up, no clicking needed. It would be a pain in the neck to try and emulate it with a custom widget.) I have tried adding it to a parent widget's layout, but after the first time it is used, it disappears. How would I go about keeping it there?
I can't find any option in QMenu that would disable auto-hide, so simplest way would be a subclass that overrides hideEvent. hideEvent is fired just before hide() completes. That means you can't intercept/ignore hide() but you can re-show it:
class PermanentMenu(QtGui.QMenu):
def hideEvent(self, event):
self.show()
Just make your top-level menu from PermanentMenu and it should be fine.
A simple example using it:
import sys
from PyQt4 import QtGui
class PermanentMenu(QtGui.QMenu):
def hideEvent(self, event):
self.show()
class Window(QtGui.QWidget):
def __init__(self, parent=None):
super(Window, self).__init__(parent)
self.menu = PermanentMenu()
self.menu.addAction('one')
self.menu.addAction('two')
self.submenu = self.menu.addMenu('submenu')
self.submenu.addAction('sub one')
self.submenu.addAction('sub two')
self.submenu2 = self.menu.addMenu('submenu 2')
self.submenu2.addAction('sub 2 one')
self.submenu2.addAction('sub 2 two')
layout = QtGui.QHBoxLayout()
layout.addWidget(self.menu)
self.setLayout(layout)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
w = Window()
w.show()
sys.exit(app.exec_())

Hide window from taskbar

I'm trying to minimize a window to the tray, but it seems it refuses to hide from the taskbar. I've spent a little time and distilled the problem code down to this. It's not mcuh so I'm wondering if I need something else to hide my app to tray in Windows 7.
import sys, os
from PyQt4 import uic
from PyQt4.QtGui import QMainWindow, QApplication
class MyClass(QMainWindow):
def __init__(self, parent = None):
QMainWindow.__init__(self, parent)
self.ui = uic.loadUi(os.path.join("gui", "timeTrackerClientGUI.ui"), self)
def hideEvent(self, event):
self.hide()
def showEvent(self, event):
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
wnd = MyClass()
wnd.show()
app.exec_()
It seems the application icon does hide but then another one pops up, If I click the taskbar icon multiple times I can get these two icons flickering, looks kind of like this for a splitsecond before the first one hides:
It took a quite ugly hack to get it working but here's the final code if anybody is interested, ph is my platform-specific module, you can use platform.name or similar function instead:
def hideEvent(self, event):
self.hide()
if ph.is_windows():
self.hidden = True
self.setWindowFlags(Qt.ToolTip)
def showEvent(self, event):
if ph.is_windows() and self.hidden:
self.setWindowFlags(Qt.Window)
self.hidden = False
self.show()
calling show/hide in showEvent()/hideEvent() doesn't make sense - those events are the result of show()/hide() calls (and the like), not the trigger. If you want to toggle the window visiblity by clicking the tray icon, try setVisible(!isVisible()) on the widget, if you want to hide the window when the user clicks the window close button try reimplementing closeEvent():
MyMainWindow::closeEvent( QCloseEvent* e ) {
hide();
e->accept();
}
In Python, that is
def closeEvent(self, event):
self.hide()
event.accept()

Categories

Resources