09/09/2017

My first PyQt5 application

Traditionally, when we start to learning new programming language or framework, the first application is hello world application.
I start to learn PyQt5. The first application below:


import sys
from PyQt5.QtWidgets import QApplication, QWidget,QLabel
 
class App(QWidget):
 
    def __init__(self):
        super().__init__()
        self.title = 'PyQt5'
        self.left = 10
        self.top = 10
        self.width = 640
        self.height = 480
        self.initUI()
 
    def initUI(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height) 

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    b = QLabel(ex)
    b.setText("Welcome to PyQt5!")
    b.move(50,20)
    ex.show()
    sys.exit(app.exec_())


08/09/2017

Qt/QML: Four-finger swipe gesture bug

Problem:
I am developing an application for Android/iOS.
I am using MouseArea to make my button.
I am stuck in the following case:
Touch on screen with 4 fingers, 1 finger among them touch on the button.
Swipe the screen.
The application become inactive state. I display a item as a popup (the button will go into disabled state) when the application become active state again. Then the popup will be hidden, the button is enabled state.
Now the button doesn't receive mouse event again.
Solution:
I override notify method of QGuiApplication:

bool MyApplication::notify(QObject *receiver, QEvent *event){
    QEvent::Type t = event->type();
    switch(t){
    case QEvent::MouseButtonPress:{
        QString classname(receiver ->metaObject()->className());
        lastReceiver = receiver;
    }
        break;
    case QEvent::MouseButtonRelease:
        lastReceiver = NULL;
        break;
    case QEvent::ApplicationStateChange:
        //To background
        if (lastReceiver != NULL){
            QQuickItem*item = qobject_cast< QQuickItem*>(lastReceiver);
            if (item != NULL){
                if(QGuiApplication::applicationState() == Qt::ApplicationInactive){
                    item ->ungrabMouse();
                }
                if(QGuiApplication::applicationState() == Qt::ApplicationActive ){
                    item ->grabMouse();
                    lastReceiver = NULL;
                }
            }
        }

        break;
    default:
        break;
    }
    return QGuiApplication::notify(receiver,event);
}