I'm trying to create a Ctrl + Mousewheel macro to zoom in and out of an image view in my application.
Currently I am trying to use the current code:
new QShortcut(QKeySequence(Qt::CTRL + QWidget::wheelEvent(QWheelEvent *event)), this, SLOT(zoom()));
However I get the error QWheelEvent does not refer to a value. I have all the necessary includes in my header file so I do not understand why I'm getting the error.
Is it illegal to bind the widget event in conjunction within a QKeySequence? If so, how should I handle the event?
You can't use QKeySequence in this way. You should reimplement wheelEvent or use next event filter (it is example how to zoom in/out in textEdit, you can use this code for your special case):
bool MainWindow::eventFilter(QObject *obj, QEvent *event)
{
if(obj == ui->plainTextEdit && event->type() == QEvent::Wheel )
{
QWheelEvent *wheel = static_cast<QWheelEvent*>(event);
if( wheel->modifiers() == Qt::ControlModifier )
if(wheel->delta() > 0)
ui->plainTextEdit->zoomIn(2);
else
ui->plainTextEdit->zoomOut(2);
}
return QObject::eventFilter(obj, event);
}
Main idea: catch wheel event and check is Ctrl modifier is pressed.
To use eventFilter you should also:
protected:
bool eventFilter(QObject *obj, QEvent *event);//in header
and
qApp->installEventFilter(this);//in constructor
Note: I showed example with event filter because it is not require subclassing, it is not better or something else, reimplement wheelEvent with similar code and you will get absolutely same result.
Related
I've subclassed Qt QAbstractSpinBox to create ComplexSpinBox that handles complex numbers.
To do this, I'm overriding keyPressEvent(), mousePressEvent(), and wheelEvent() and if the keyboard Ctrl modifier is active then the imaginary portion of the complex number is incremented or decremented. Otherwise the real portion is updated. (Also including a custom validator for complex numbers.)
All is cool except that holding the up and down arrow buttons down doesn't make them repeat.
What am I leaving out?
Well, one, if not the way to do this is to add
private:
QTimer *timer;
in the definition and create it in the constructor:
timer = new QTimer(this);
connect (timer, &QTimer::timeout,
[=]() {
// do your thing
});
Then somewhere in the mousePressEvent(QMouseEvent *mouseEvent) method
timer->start (100);
For reasons that aren't clear to me, mouseReleaseEvent() doesn't get triggered in this environment so you have to use an event filter. Back in the definition, you'll need:
protected:
bool eventFilter(QObject *object, QEvent *event);
and the method
bool ComplexSpinBox::eventFilter(QObject *object, QEvent *event)
{
if (object == this && event->type() == QEvent::MouseButtonRelease) {
timer->stop ();
}
return false;
}
I am looking for an event if the mouse is clicked outside of the groupBox in Qt. I tried FocusOutEvent but was not able to get the event:
ui.groupBox->installEventFilter(this);
void myClass::focusOutEvent(QFocusEvent *event) { ui.groupBox->hide(); }
Any kind of help would be greatly appreciated!
You have the following options:
Subclass QGroupBox and override mousePressEvent()
Install an event filter on that group box and catch QMouseEvents
If you want to catch only right mouse clicks (context menu), implement a custom context menu handler.
The problem is that the events of the monitored object are not forwarded to the native event handlers of the filter object such as focusOutEvent, but to a special virtual event function, i.e. eventFilter(QObject *obj, QEvent *event) as documented in installEventFilter. So, your event handler should look like this:
bool myClass::eventFilter(QObject *obj, QEvent *event)
{
if (obj == ui.groupBox && event->type () == QEvent::FocusOut)
ui.groupBox->hide();
return false;
}
I've Dialog that shows folders (in treeView) and files (in listView) respectively. In listView doubleClick signal is handled by a slot that Qt created while I used Designer with aproppriate slot to be implemented. The problem is that I'm not able to handle RIGHT MOUSE click. Is there a solution?
P.S.
I've googled for a while to solve this problem, it seems that inheriting QListView and overriding solve the problem. But in my case I've already populated Qt's standart QListView using Designer.
In this case you can use event filter:
bool MainWindow::eventFilter(QObject *obj, QEvent *event)
{
if (obj == ui->listView->viewport() && event->type() == QEvent::MouseButtonDblClick)
{
QMouseEvent *ev = static_cast<QMouseEvent *>(event);
if (ev->buttons() & Qt::RightButton)
{
qDebug()<< "double clicked" << ev->pos();
qDebug()<< ui->listView->indexAt(ev->pos()).data();
}
}
return QObject::eventFilter(obj, event);
}
To use eventFilter you should also:
protected:
bool eventFilter(QObject *obj, QEvent *event);//in header
and
qApp->installEventFilter(this);//in constructor
Possible addition to your problem. If you want do different things when user clicks left or right mouse buttons you should handle lest and right clicks in filter, without doubleClick signal (because it emits signal in both cases) and your code can be something like:
QMouseEvent *ev = static_cast<QMouseEvent *>(event);
if (ev->buttons() & Qt::RightButton)
{
qDebug()<< "RightButton double clicked";
//do something
}
if (ev->buttons() & Qt::LeftButton)
{
qDebug()<< "LeftButton double clicked";
//do something
}
In my case, I started trying to catch mouse events when a user right-clicked on a line in the QListView, but they never came through. However, all I really wanted to do was popup a context menu, and it turns out the contextMenuEvent did get through! So I didn't have to subclass QListView, just added a contextMenuEvent() to my widget that contained the QListView.
This was Qt3, so your mileage will most definitely differ.
For example, consider a main menu item that has the Delete key as a shortcut (with Qt::WindowShortcut as context). I want another QWidget to handle the Delete key when focused. This is not possible because the Delete key is processed by the main menu. I've tried grabbing the keyboard on QWidget focus but that doesn't do anything. Is this event possible?
I was able to get the behavior I wanted by installing an event filter on qApp when the QWidget is focused (remove it when losing focus), and returning true for all QEvent::Shortcut types.
void MyWidget::focusInEvent( QFocusEvent *event )
{
qApp->installEventFilter(this);
}
void MyWidget::focusOutEvent( QFocusEvent *event )
{
qApp->removeEventFilter(this);
}
bool MyWidget::eventFilter( QObject *target, QEvent *event )
{
if (event->type() == QEvent::Shortcut)
{
// If I care about this shortcut, then return true to intercept
// Else, return false to let the application process it
}
return false;
}
If there's a better way, I'd love to hear it!
I have a QLineEdit which I set an eventFilter using installEventFilter(this). Is it possible to pass in parameters to this eventFilter? For example, I want multiple QLineEdits to all call the same eventFilter, but I need to pass in a parameter in order to be able to tell which QLineEdit box caused the event to occur.
Thanks in advance!
The sender object is already passed to eventFilter as first parameter. So you are able to determine which QLineEdit is dispatched like this:
bool eventFilter(QObject *obj, QEvent *ev) {
if (obj == lineEdit1) {
// event from lineEdit1
} else if (obj == lineEdit2) {
// event from lineEdit2
}
}