My custom widget not react to my paintEvent on other sessions

196 Views Asked by At

Im trying to develop a program which have many of my custom widget. in my widget i have a crosshair mouse pointer and when i move it in one of my widgets, all of them must sense it and the crosshair must react on every single widget. I have a self.update() on the end of the paintEnent function.

  1. How can i force the other widgets on my app to react it?

  2. paintEvent is not a simple code like this and as i add every functions on this part, it make a very high CPU usage. How can i manage it?

     import sys
     from PyQt5.QtWidgets import QApplication, QDialog, QListWidget, 
     QComboBox, QVBoxLayout, QPushButton, QLabel, QMainWindow, QWidget, 
     QHBoxLayout, QVBoxLayout, QSizePolicy
     from PyQt5.QtCore import Qt, QSize, QRect
     from PyQt5.QtGui import QPalette, QColor, QPen, QPixmap, QPainter, 
     QBrush
    
    
    
     class Boom(QWidget):
         xPos=-1.0
         def __init__(self,bgColor,xSpace=1,ySpace=1):
             super().__init__()
             self.bgColor=bgColor       
             self.x = -1
             self.y = -1
         self.setSizePolicy(QSizePolicy.MinimumExpanding,QSizePolicy.MinimumExpanding)
         self.setMouseTracking(True)
         layout = QVBoxLayout()
         self.setLayout(layout)
    
     def mouseMoveEvent(self, event):
         global xPos
         self.x = event.x()
         self.y = event.y()
         xPos=self.x/self.width()
         self.update()
    
    
     def paintEvent(self, e):
         painter = QPainter(self)
    
         font = painter.font()
         font.setFamily('Times')
         font.setPointSize(8)
         painter.setFont(font)
    
         brush = QBrush()
         brush.setColor(QColor(self.bgColor))
         brush.setStyle(Qt.SolidPattern)
    
         pen = QPen()
         pen.setWidth(1)
         pen.setColor(QColor('black'))
         painter.setPen(pen)
    
         rect = QRect(0, 0, painter.device().width(), painter.device().height())
         painter.fillRect(rect, brush)
    
         pen.setColor(QColor('blue'))
         painter.setPen(pen) 
    
         painter.drawLine(self.x, 0, self.x,painter.device().height()) #V
         painter.drawLine(0, self.y, painter.device().width(),self.y) #H
    
         self.update()
    
         painter.end()
    
    
     class MainWindow(QMainWindow):
     def __init__(self):
         super(MainWindow, self).__init__()
    
         self.setWindowTitle("Graph")
    
         MainPanelLayout = QHBoxLayout()
    
         MainPanelLayout.setContentsMargins(1,1,1,1)
         MainPanelLayout.setSpacing(1)
         B1=Boom('pink',0)
         B2=Boom('gray',1)
         MainPanelLayout.addWidget(B1)
         MainPanelLayout.addWidget(B2)
    
         widget = QWidget()
         widget.setLayout(MainPanelLayout)
         self.setCentralWidget(widget)
    
     if __name__ == "__main__":
     app = QApplication(sys.argv)
     mainWindow = MainWindow()
     mainWindow.show()
     # mainWindow.showMaximized()
     app.exec_()
    
1

There are 1 best solutions below

0
On

Iv found a way! I used signal and slots to communicate between instances

May it works for others too :)

import sys
from PyQt5.QtWidgets import QApplication, QDialog, QListWidget,QComboBox, QVBoxLayout, QPushButton, QLabel, QMainWindow,QWidget, QHBoxLayout, QVBoxLayout, QSizePolicy
from PyQt5.QtCore import Qt, QSize, QRect, QObject, pyqtSignal
from PyQt5.QtGui import QPalette, QColor, QPen, QPixmap, QPainter,QBrush
import FxMainForm

class BoomCommunicate(QObject):
 signal = pyqtSignal()

class Boom(QWidget):
 xPos=-1
 def __init__(self,bgColor,nChannel=-1,xSpace=1,ySpace=1):
    super().__init__()
    self.bgColor=bgColor
    self.xSpace=xSpace
    self.ySpace=ySpace        
    self.x = -1
    self.y = -1
    self.nChannel=nChannel
    self.setSizePolicy(QSizePolicy.MinimumExpanding,QSizePolicy.MinimumExpanding)
    self.setMouseTracking(True)
    self.layout = QVBoxLayout()
    self.setLayout(self.layout)
    self.communicate = BoomCommunicate()
    self.communicate.signal.connect(self.trigger_refresh)

def mouseMoveEvent(self, event):
    self.x = event.x()
    self.y = event.y()
    if self.nChannel==0:
        Boom.xPos=self.x/self.width()
    self.communicate.signal.emit()


def paintEvent(self, e):
    painter = QPainter(self)
    font = painter.font()
    font.setFamily('Times')
    font.setPointSize(8)
    painter.setFont(font)

    brush = QBrush()
    brush.setColor(QColor(self.bgColor))
    brush.setStyle(Qt.SolidPattern)

    pen = QPen()
    pen.setWidth(1)
    pen.setColor(QColor('black'))
    painter.setPen(pen)

    ## Border
    rect = QRect(0, 0, painter.device().width(), painter.device().height())
    painter.fillRect(rect, brush)

    pen.setColor(QColor('blue'))
    painter.setPen(pen)  

    # self.Drawings(painter,pen,brush)

    if self.nChannel==0:
        painter.setPen(QPen(Qt.red, 1, Qt.SolidLine))  
        painter.drawLine(self.x, self.ySpace, self.x,painter.device().height()-self.ySpace-1) ##V
        painter.drawLine(self.xSpace, self.y, painter.device().width()-self.ySpace-1,self.y)  ##H
        if not FxMainForm.studyItem==None:
            painter.setPen(QPen(Qt.black, 1, Qt.SolidLine))     
            painter.drawText(5, 15,FxMainForm.studyItem[0]+' ('+FxMainForm.studyItem[1]+') ')
            Boom.xPos=self.x/self.width()
    else:
        painter.setPen(QPen(Qt.red, 1, Qt.SolidLine))  
        painter.drawLine(int(Boom.xPos*painter.device().width()), self.ySpace,int(Boom.xPos*painter.device().width()),painter.device().height()-self.ySpace-1)
    
    painter.end()

def trigger_refresh(self):
    self.update()

def sizeHint(self):
    return QSize(100, 100)





class MainWindow(QMainWindow):
def __init__(self):
    super(MainWindow, self).__init__()
    self.setWindowTitle("Graph-Boom")

    self.MainPanelLayout = QHBoxLayout()

    self.MainPanelLayout.setContentsMargins(1,1,1,1)
    self.MainPanelLayout.setSpacing(1)
    self.B1=Boom('pink',0)
    self.B2=Boom('gray',1)
    self.MainPanelLayout.addWidget(self.B1)
    self.MainPanelLayout.addWidget(self.B2)
    self.B1.communicate.signal.connect(self.B2.update)    


    
    self.widget = QWidget()
    self.widget.setLayout(self.MainPanelLayout)
    self.setCentralWidget(self.widget)

if __name__ == "__main__":
 app = QApplication(sys.argv)
 mainWindow = MainWindow()
 mainWindow.show()
 # mainWindow.showMaximized()
 app.exec_()