How to detect if a window has been activated? - qt

Focus events don't work because they're not sent if you activate your window by clicking on its non-client frame. Also, if you click the internal components of the window THEY will get the focus event, not your window, but the window will still be activated, even if it wasn't active or focused before.

The event you want is QEvent::WindowActivate. Override event() to process it:
bool YourWidget::event(QEvent *e)
{
if (e->type() == QEvent::WindowActivate) {
// window was activated
}
return QWidget::event(e);
}

Qt provides several virtual event handling functions you can use. Since the activation of a window changes its state, you want to handle some change events:
void MyWidget::changeEvent(QEvent * e) {
if(e->type() == QEvent::ActivationChange && this->isActiveWindow()) {
// .. this is now the active window
}
}
References
changeEvent
isActiveWindow

Related

qt mouse event filter

I have a QWidget with a QGraphicsView and a push button. The QGraphicsView has to take mouse press and release events to detect a swipe .At the same time push button should run a small function on clicked. I used an event filter in the QWidget to detect the mouse events.
bool Widget::eventFilter(QObject * obj, QEvent * event)
{
// Capturing keyboard events for moving
if( event->type() == QEvent::KeyPress )
{
//Do something
}
//Capturing mouse events for swipe
else if( event->type() == QEvent::MouseButtonPress)
{
QMouseEvent* mouseEvent = static_cast<QMouseEvent*> (event);
swipe_startPoint = mouseEvent->pos();
}
else if( event->type() == QEvent::MouseButtonRelease)
{
QMouseEvent* mouseEvent = static_cast<QMouseEvent*> (event);
swipe_endPoint = mouseEvent->pos();
swipeDirection();
}
else
{
QWidget::eventFilter(obj,event);
}
}
In the constructor of the Widget class i have the following
ui->graphicsView->installEventFilter(this);
The problem is that the button is getting clicked but the MouseButtonRelease event that sets the 'swipe_endPoint' value is not working.
When i set
ui->graphicsView->grabMouse();
the mouse pressed and released events are working perfectly but the button stops accepting the events.
Other things that i have tried are :-
Set the event filter to the viewPort
ui->graphicView->viewPort()->installEventFilter(this);
Please help me get this mouse event working. Thanks in advance.
Note : Using QT 5.6.0 in Ubuntu

graphicsview receives mouse event before the item

I have implemented the panning view on the QGraphicsView, using the mouse move event using
void View::mouseMoveEvent(QMouseEvent* event) {
pan();
QGraphicsView::mouseMoveEvent(event);
}
and in the scene of this view I have added few items where some of the items are resizable, so I implemented
void Item::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
if(m_resizeMode)
{
resize();
e->accept();
}
}
I tried to filter the mouse move not to propagate to any further using e->accept()
but my View mouseMove event has been called first , so when ever I tried to resize the item, the view started to pan all the way.
How can I avoid this event propagation from view to scene.
You can call the base class implementation of the QGraphicsView and check if the event is accepted. However I would do this in the mousePressEvent instead of the mouseMoveEvent. After all this is where you determine if you should resize an item or do some panning. This is how I do something similar in my project:
void View::mousePressEvent(QMouseEvent *event)
{
...
QGraphicsView::mousePressEvent(event);
if(event->isAccepted())
move = false;
else
move = true;
}
void View::mouseMoveEvent(QMouseEvent *event)
{
if(!move)
{
QGraphicsView::mouseMoveEvent(event);
return;
}
... // you would do the panning here
QGraphicsView::mouseMoveEvent(event);
}
void View::mouseReleaseEvent(QMouseEvent *event)
{
if(!move)
{
QGraphicsView::mouseReleaseEvent(event);
return;
}
else
{
...
move = false;
}
QGraphicsView::mouseReleaseEvent(event);
}
The view will always receive the mouse event first.
So, in the view, check to see if the mouse is over an item before allowing it to pan by getting the mouse pos in scene coordinates and retrieving the items at that position with QGraphicsScene::items( )

How to make a Qt dialog read-only?

How to make a QT dialog read-only? Any general way to implement it easily? For example
(1) set all its containing widgets disable. (how to implement it?)
(2) Intercept edit events like key pressed, mouse pressed but how not to intercept the one to close the dialog?
I think this feature should be very helpful.
Disabling the widgets can be done similar to the following:
void myDialog::disableWidgets()
{
QList<QWidget *> widgets = this->findChildren<QWidget *>();
foreach(QWidget* widget, widgets)
{
widget->setEnabled(false);
}
}
To intercept events, QDialog includes the function installEventFilter(QObject*).
This allows you to use a separate object to receive all events passed to the dialog. You can then choose to handle the event in the object, or pass it on to the dialog itself by calling the base class QObject::eventFilter
class MyEventHandler : public QObject
{
Q_OBJECT
protected:
bool MyEventHandler::eventFilter(QObject *obj, QEvent *event)
{
// handle key press events
if (event->type() == QEvent::KeyPress)
{
// Do something
// ...
return true; // event handled by the class
}
else
{ // ignore this event and pass it to the dialog as usual
return QObject::eventFilter(obj, event);
}
}
return false;
};
QDialog* dlg = new QDialog;
MyEventHandler evtHandler = new MyEventHandler;
dlg->installEventFilter(evtHandler);
Read-only is a strange term to apply to a dialog. Disabling all widgets as above does the trick. If you only wanted to make the input part of a QInputDialog read-only (while leaving scrollbars, buttons, etc. enabled), you could adapt that code as below:
QInputDialog dialog(this);
dialog.setOptions(QInputDialog::UsePlainTextEditForTextInput);
dialog.setWindowTitle("Title");
dialog.setLabelText("Label");
dialog.setTextValue("1\n2\n3\n");
QList<QWidget *> widgets = dialog.findChildren<QWidget *>();
foreach(QWidget* widget, widgets) {
if (strcmp(widget->metaObject()->className(),"QPlainTextEdit")==0) {
QPlainTextEdit *t = static_cast<QPlainTextEdit*>(widget);
t->setReadOnly(true);
}
}
dialog.exec();

In a QWizard, the next button cannot accept key press event, but accepts mouse press event?

I have a class inherits QWizard, and add 2 independent QWizardPage(s). Before go to the next page, I want to do some job (i.e. check internet connection) on the first page. User may click 'next' button by mouse, or directly press enter/return by keyboard since the 'next' button is focused by default. So I install a eventfilter for the button:
button(QWizard::NextButton)->installEventFilter(this);
Then in the wizard class implement the event handling code:
bool MyWizard::eventFilter(QObject *watched, QEvent *event)
{
if (watched == button(QWizard::NextButton))
{
if (currentId() == startPageId)
{
if (event->type() == QEvent::KeyPress)
{
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
if (keyEvent->key() == Qt::Key_Return || keyEvent->key() == Qt::Key_Enter)
{
// Do something. Cannot be reached.
}
}
else if (event->type() == QEvent::MouseButtonPress)
{
// Do something. Can be reached.
}
}
}
return QWizard::eventFilter(watched, event);
}
As I tried many times, clicking mouse can always make my event processing code run, but pressing key makes nothing to do. Another curious thing is, press mouse on button without releasing, then move away and release, the wizard stays on first page, now I can press key and all that is OK.
Could any one help to find the reason, is that a Qt bug? how can I make the key event work properly?
Try another simple solution without installing event filter.Use QkeyPressEvent in your main App.
void YourAppClass::keyPressEvent(QKeyEvent *e)
{
if(Nextbutton->hasFocus() && e->key()== Qt::Key_Return || e->key() == Qt::Key_Enter)
{
Do your job//I guarantee this will be reached upon pressing return with button focus
}
}
You can add mouse-click functionality in nextbutton's clickevent().

Respond to application-wide "hotkey" in Qt

I've got a simple Qt app, and I just want to respond to the F12 key, regardless of which widget has focus.
Is there some easy signal or something I can hook in to?
I want to use the F12 key to toggle the main window fullscreen on/off.
I haven't tried, but here is what I would do :
Create a QShortcut and make sure its context (with setContext()) is Qt::ApplicationShortcut.
shortcut = new QShortcut(QKeySequence(Qt::Key_F12), parent);
shortcut->setContext(Qt::ApplicationShortcut);
Then you just need to connect a slot to the QShortcut::activated() signal.
If you have a "central widget" which all of the other widgets are children of, then you can simply set that as the widget argument for QShortcut.
(Python, qt5)
self.centralwidget = QtWidgets.QWidget(MainWindow)
QtWidgets.QShortcut(QtGui.QKeySequence("F12"), self.centralwidget, self.goFullScreen)
I added this as an answer because the shortcut context flag: Qt.ApplicationShortcut did not work for me.
Setting the shortcut context to Qt::ApplicationShortcut has a serious flaw. It will not work in modal dialogs. So if you want a trully real pan-application-wide shortcut, then you need to override application's notify() method. The alternative is to install event filter for the application object but that I suspect would be slower and requires slightly more code. With notify() it is very simple:
class MyApplication : public QApplication
{
// TODO: constructor etc.
protected:
bool MyApplication::notify(QObject *receiver, QEvent *event) override
{
if (event->type() == QEvent::KeyPress)
{
auto keyEvent = static_cast<QKeyEvent*>(event);
if (keyEvent->key() == Qt::Key_F12 && keyEvent->modifiers() == Qt::NoModifiers)
{
// TODO: do what you need to do
return true;
}
}
return QApplication::notify(receiver, event);
}
}

Resources