JasonGenX
JasonGenX

Reputation: 5434

Detecting click outside QWidget

My application has non-rectangular popup widgets.

their class defines the following to achieve that transparency:

setAttribute(Qt::WA_NoSystemBackground, true);
setAttribute(Qt::WA_TranslucentBackground, true);

I also use:

 this->setWindowFlags(Qt::Popup| Qt::FramelessWindowHint);

The problem is that on windows 7, an automatic "shadow" is being drawn on the bottom and right sides of my window. This is highly undesirable.

So, I tried using Qt::Tool instead of Qt::Popup

 this->setWindowFlags(Qt::Tool | Qt::FramelessWindowHint);

This works visually. No shadow, but now a mouse click outside my widget window will not automatically close/hide it as it would have done with a Qt::Popup.

So, I need one of these two solutions:

  1. A way to have Qt::Popup and get rid of that automatic windows shadow decoration
  2. A way to let the Qt::Tool window a mouse click occurred outside of it.

One note: My application is built for Windows XP and up. I cannot use a Vista/Win7 only runtime DLLs, nor can i have a "Windows XP" and "Vista and up" separate builds.

Any advice will be welcome.

Upvotes: 1

Views: 8591

Answers (4)

Yash
Yash

Reputation: 7064

I set my QListView

d->setWindowFlags(Qt::Popup | Qt::FramelessWindowHint);

Install eventfilter and used MousePressEvent to hide qlistview widget.

MousePressEvent on list never comes to filter they produce other events which I didn't debug.

So if you want to design auto completer this will be perfect. Tested in Qt5.3.1.

Upvotes: 0

Unnamed
Unnamed

Reputation: 11

My solution for Windows 7:

QDialog *d = new QDialog;
d->setStyleSheet("background:transparent;");
d->setAttribute(Qt::WA_DeleteOnClose, true);
d->setAttribute(Qt::WA_TranslucentBackground, true);
#ifdef Q_OS_WIN
d->createWinId();
#endif
d->setWindowFlags(Qt::Popup | Qt::FramelessWindowHint);
d->show();

Upvotes: 1

JasonGenX
JasonGenX

Reputation: 5434

Finally after realizing that no amount of "SetFocusPolicy" calls will allow me to receive those events for a Qt::Tool window, I've resorted to something else to fix my problem:

  1. I kept the Qt:tool as Qt::Popup caused an undesired shadowing effect, tarnishing my owner draw frame. Removing this style cannot be done in Qt and I didn't want to mess with platform specific conditional code.
  2. I installed an event filter with my Qt::tool window and I began receiving events that assisted me in understanding when other parts of my application were clicked, or if the application itself lost focus to another application. This was what I needed, functionality wise. I could also get an event when users click the non-client areas of the application's main window, such as the windows caption so that I can close it when dragging begins etc.

Upvotes: 1

phyatt
phyatt

Reputation: 19112

You could manually watch for when the focus changes from your Qt::Tool window. So basically you watch for when the focus goes onto another window of your process or when your application loses focus.

How to detect that my application lost focus in Qt?

Hope that helps.

Upvotes: 1

Related Questions