1

I created myself simple window for debugging purposes. It's completely separate QMainWindow, so that I can put it on the other monitor when testing the app. But when there's a dialog in my application, I cannot access the window. Right now, this is exactly the time I WANT to access it.

I tried to override QWidget::event like so:

DebugWindow.h

#include <QtWidgets/QMainWindow>
QT_BEGIN_NAMESPACE
class QEvent;
QT_END_NAMESPACE
class DebugWindow : public QMainWindow
{
    Q_OBJECT
public:
    DebugWindow(QWidget *parent = 0);
    virtual ~DebugWindow();

protected:
    virtual bool event(QEvent*);
};

DebugWindow.cpp

bool TechadminScript::event(QEvent* e)
{
    if (e->type() == QEvent::WindowBlocked) {
        // accept the event to stop propagation
        e->accept();
        return true;
    }
    return false;
}

I have set up breakpoint in the overriden function and it was hit - that means I did something right. But the window is still blocked as it was before.

So I am probably missing something, can somebody help me finish this?

Tomáš Zato
  • 50,171
  • 52
  • 268
  • 778
  • 1
    The dialog blocks the event loop of the application. I don't think you can do something with your debug window until the execution returns back to the application's event loop. – vahancho Feb 08 '18 at 14:06
  • It surely does not block the event loop - that would stop rendering too. AFAIK, there's nested event loop running in the widget code until it's closed. We have similar widgets written that also use event loop but do not whole app. – Tomáš Zato Feb 08 '18 at 14:10
  • well if u using it for debug purpose, you can move it to separate application, and use udp instead of signal/slots to communicate with it. – Andrew Kashpur Feb 08 '18 at 14:15
  • [That actually worked and I used TCP](https://github.com/Darker/qt-gui-test) to send simple commands to the app. But what I need now cannot be covered using simple telnet protocol in reasonable time. – Tomáš Zato Feb 08 '18 at 14:17
  • 1
    Try setting the [window modality](http://doc.qt.io/qt-5/qwidget.html#windowModality-prop) of your dialog – king_nak Feb 08 '18 at 14:41
  • Can't do, it's `QInputDialog` and `QFileDialog` and so on. I dream of writing better dialogs for our project, but I have no time for that right now. – Tomáš Zato Feb 08 '18 at 14:59

1 Answers1

0

It worked for me setting the window modality as @king_nak suggested. Mind you have to hide() beforehand and show() according to the documentation: https://doc.qt.io/qt-5/qwidget.html#windowModality-prop

bool Object::eventFilter(QObject *, QEvent * const event)
{
  if (event->type() != QEvent::WindowBlocked)
    return false;

  for (auto&& widget : qApp->topLevelWidgets())
  {
    if (!widget->isModal())
      continue;

    widget->hide();
    widget->setWindowModality(Qt::NonModal);
    widget->show();
  }

  return true;
}
fassl
  • 724
  • 7
  • 9