Conflic between Qt::StrongFocus and resizing frameless widget by means of nativeEvent
-
In Qt5.1, I have a frameless widget on which I have a QLineEdit, which holds focus currently. If I want to make the QLineEdit lose focus by clicking the space area on the widget(not other controls), I have to add Qt::StrongFocus property to the widget, because by default the widget would not accept "focus" and if no recepient for it the focus status will not be transferred away from QLineEdit. Now with Qt::StrongFocus set on the widget everything is ok.
Now I also want to resize the frameless widget with Windows' native functions. (Here we don't consider other ways such as QSizeGrip or whatever) The nativeEvent also works fine.
But, if we put the above two together in a same widget, the StrongFocus property doesn't work any more. It seems that the event has been blocked by the nativeEvent function. Can anybody help to tell me how I can let the focus event pass through the nativeEvent to the widget? Thanks a million for your help.
mainwindow.h
@ #include <QMainWindow>
#include <windows.h>
#include <windowsx.h>class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = 0); protected: bool nativeEvent(const QByteArray &eventType, void *message, long *result); };@ mainwindow.cpp
@ #include "mainwindow.h"
#include <QLineEdit>MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { this->setWindowFlags(Qt::FramelessWindowHint); this->setFocusPolicy(Qt::StrongFocus); QLineEdit *myLineEdit=new QLineEdit(this); myLineEdit->move(10,10); myLineEdit->setText("hello Qt!"); } bool MainWindow::nativeEvent(const QByteArray &eventType, void *message, long *result) { if(eventType=="windows_generic_MSG") { const MSG *msg=static_cast<MSG*>(message); if(msg->message==WM_NCHITTEST) { int xPos = GET_X_LPARAM(msg->lParam) - this->frameGeometry().x(); int yPos = GET_Y_LPARAM(msg->lParam) - this->frameGeometry().y(); if(this->childAt(xPos,yPos) == 0) { *result = HTCAPTION; }else{ return false; } if(xPos > 0 && xPos < 5) *result = HTLEFT; if(xPos > (this->width() - 5) && xPos < (this->width() - 0)) *result = HTRIGHT; if(yPos > 0 && yPos < 5) *result = HTTOP; if(yPos > (this->height() - 5) && yPos < (this->height() - 0)) *result = HTBOTTOM; if(xPos > 0 && xPos < 5 && yPos > 0 && yPos < 5) *result = HTTOPLEFT; if(xPos > (this->width() - 5) && xPos < (this->width() - 0) && yPos > 0 && yPos < 5) *result = HTTOPRIGHT; if(xPos > 0 && xPos < 5 && yPos > (this->height() - 5) && yPos < (this->height() - 0)) *result = HTBOTTOMLEFT; if(xPos > (this->width() - 5) && xPos < (this->width() - 0) && yPos > (this->height() - 5) && yPos < (this->height() - 0)) *result = HTBOTTOMRIGHT; return true; } } return false; }@