我的自定义小部件在其他会话中对我的paintEvent没有反应



我正试图开发一个程序,其中包含许多我的自定义小部件。在我的小部件中,我有一个十字光标鼠标指针,当我在其中一个小部件中移动它时,所有的小部件都必须感应到它,十字光标必须对每个小部件做出反应。我在paintEnent函数的末尾有一个self.update((。

  1. 如何强制应用程序上的其他小部件做出反应?

  2. paintEvent并不是一个像这样简单的代码,当我添加这部分的每个函数时,它的CPU使用率非常高。我该怎么办?

    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_()
    
Iv找到了一条路!我使用信号和插槽在实例之间进行通信

愿它也适用于其他人:(

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_()

相关内容

  • 没有找到相关文章

最新更新