从上下文菜单向QMainWindow添加小部件

2024-09-24 02:18:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我可以将QWidget添加到QMainWindow,并将其位置设置为光标位置。你知道吗

但是每次,我都想给QMainWindow添加新的QWidget。我可能没有给代码添加新的QWidget,但在canvas中,我只看到一个QWidget。这是我的密码:

from PyQt5.QtWidgets import QMainWindow, QApplication, QMenu, QMenuBar, QAction, QFileDialog, QWidget, QLabel
from PyQt5.QtGui import QIcon, QImage, QPainter, QPen, QBrush
from PyQt5.QtCore import Qt, QPoint
import sys


class Window(QMainWindow):
    def __init__(self):
        super().__init__()


        title = "Paint Application"
        top = 400
        left = 400
        width = 800
        height = 600
        self.objStr = "berkayy"
        self.count = 0

        # icon = "icons/pain.png"
        self.setAcceptDrops(True)
        self.setWindowTitle(title)
        self.setGeometry(top, left, width, height)
        # self.setWindowIcon(QIcon(icon))

        self.image = QImage(self.size(), QImage.Format_RGB32)
        self.image.fill(Qt.white)

        self.drawing = False
        self.brushSize = 2
        self.brushColor = Qt.black
        self.lastPoint = QPoint()

        mainMenu = self.menuBar()
        fileMenu = mainMenu.addMenu("File")
        brushSize = mainMenu.addMenu("Brush Size")
        brushColor = mainMenu.addMenu("Brush Color")

        saveAction = QAction(QIcon("icons/save.png"), "Save",self)
        saveAction.setShortcut("Ctrl+S")
        fileMenu.addAction(saveAction)
        saveAction.triggered.connect(self.save)

        clearAction = QAction(QIcon("icons/clear.png"), "Clear", self)
        clearAction.setShortcut("Ctrl+C")
        fileMenu.addAction(clearAction)
        clearAction.triggered.connect(self.clear)

        threepxAction = QAction( QIcon("icons/threepx.png"), "3px", self)
        brushSize.addAction(threepxAction)
        threepxAction.triggered.connect(self.threePixel)

        fivepxAction = QAction(QIcon("icons/fivepx.png"), "5px", self)
        brushSize.addAction(fivepxAction)
        fivepxAction.triggered.connect(self.fivePixel)

        sevenpxAction = QAction(QIcon("icons/sevenpx.png"),"7px", self)
        brushSize.addAction(sevenpxAction)
        sevenpxAction.triggered.connect(self.sevenPixel)

        ninepxAction = QAction(QIcon("icons/ninepx.png"), "9px", self)
        brushSize.addAction(ninepxAction)
        ninepxAction.triggered.connect(self.ninePixel)

        blackAction = QAction(QIcon("icons/black.png"), "Black", self)
        blackAction.setShortcut("Ctrl+B")
        brushColor.addAction(blackAction)
        blackAction.triggered.connect(self.blackColor)


        whitekAction = QAction(QIcon("icons/white.png"), "White", self)
        whitekAction.setShortcut("Ctrl+W")
        brushColor.addAction(whitekAction)
        whitekAction.triggered.connect(self.whiteColor)


        redAction = QAction(QIcon("icons/red.png"), "Red", self)
        redAction.setShortcut("Ctrl+R")
        brushColor.addAction(redAction)
        redAction.triggered.connect(self.redColor)

        greenAction = QAction(QIcon("icons/green.png"), "Green", self)
        greenAction.setShortcut("Ctrl+G")
        brushColor.addAction(greenAction)
        greenAction.triggered.connect(self.greenColor)

        yellowAction = QAction(QIcon("icons/yellow.png"), "Yellow", self)
        yellowAction.setShortcut("Ctrl+Y")
        brushColor.addAction(yellowAction)
        yellowAction.triggered.connect(self.yellowColor)

    def rectangle(self, e, pos):
        print(pos)
        painter = QPainter(self)
        painter.setPen(QPen(Qt.black, 5, Qt.SolidLine))
        painter.setBrush(QBrush(Qt.green, Qt.DiagCrossPattern))

        painter.drawRect(100, 15, 400, 200)

    def mousePressEvent(self, event):
        if event.button() == Qt.LeftButton:
            self.drawing = True
            self.lastPoint = event.pos()
            #print(self.lastPoint)

    def contextMenuEvent(self, event):
        contextMenu = QMenu(self)
        newAct = contextMenu.addAction("New")
        openAct = contextMenu.addAction("Open")
        closeAct = contextMenu.addAction("Close")

        action = contextMenu.exec_(self.mapToGlobal(event.pos()))
        if action == closeAct:
            self.close()
        elif action == openAct:
            self.berkay(event.pos())

    def mouseMoveEvent(self, event):
        if(event.buttons() & Qt.LeftButton) & self.drawing:
            painter = QPainter(self.image)
            painter.setPen(QPen(self.brushColor, self.brushSize, Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin))
            painter.drawLine(self.lastPoint, event.pos())
            self.lastPoint = event.pos()
            self.update()

    def berkay(self, pos):
        wid = QWidget(self)
        btn = QLabel(wid)
        btn.setText("skjdf")
        btn.setObjectName(self.objStr + str(self.count) )
        btn.move(pos)
        self.setCentralWidget(wid)
        self.count += 1
        # btn.setDragEnabled(True)
        print(btn.objectName())
        # self.show()

    def mouseReleaseEvent(self, event):

        if event.button() == Qt.LeftButton:
            self.drawing = False


    def paintEvent(self, event):
        canvasPainter = QPainter(self)
        canvasPainter.drawImage(self.rect(),self.image, self.image.rect() )

    def save(self):
        filePath, _ = QFileDialog.getSaveFileName(self, "Save Image", "", "PNG(*.png);;JPEG(*.jpg *.jpeg);;All Files(*.*) ")

        if filePath == "":
            return
        self.image.save(filePath)

    def clear(self):
        self.image.fill(Qt.white)
        self.update()

    def threePixel(self):
        self.brushSize = 3

    def fivePixel(self):
        self.brushSize = 5

    def sevenPixel(self):
        self.brushSize = 7

    def ninePixel(self):
        self.brushSize = 9

    def blackColor(self):
        self.brushColor = Qt.black

    def whiteColor(self):
        self.brushColor = Qt.white

    def redColor(self):
        self.brushColor = Qt.red

    def greenColor(self):
        self.brushColor = Qt.green

    def yellowColor(self):
        self.brushColor = Qt.yellow

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = Window()
    window.show()
    app.exec()

我想在用户每次单击上下文菜单项时创建新的QWidget。你知道吗


Tags: posselfeventpngdefconnectqticons
2条回答

首先,对于每个小部件,在列表中存储x和y值。之后,向windowcentral小部件添加新的和当前的小部件。你知道吗

编辑

我们不再需要存储部件x和y了。你知道吗

感谢罗姆哈的优化建议。你知道吗

import sys
from PyQt5.QtWidgets import QMainWindow, QMenu, QApplication, QWidget, QPushButton, qApp
from PyQt5 import QtGui, QtCore
from PyQt5.QtCore import Qt

class Example(QMainWindow):
    def __init__(self):
        super().__init__()
        self.area = QWidget(self)
        self.setCentralWidget(self.area)
        self.initUI()

    def initUI(self):         
        self.setGeometry(300, 300, 300, 200)
        self.setWindowTitle('Context menu')
        self.setStyleSheet("QMainWindow {background: 'white';}")
        self.show()

    def contextMenuEvent(self, event):
        cmenu = QMenu(self)
        addBtnAct = cmenu.addAction("Add Button")
        quitAct = cmenu.addAction("Quit")
        action = cmenu.exec_(self.mapToGlobal(event.pos()))
        if action == quitAct:
            qApp.quit()
        elif action == addBtnAct:
            self.addLabel(event.pos())

    def addLabel(self, pos):
        btn = QLabel("BOOH", self.area)
        btn.move(self.area.mapFromParent(pos)) # Map the pos in the coord system of self.area
        btn.show()

if __name__ == '__main__':

    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

一个QMainWindow只能有一个中心小部件。因此,添加新的QLabel作为中心小部件将删除以前的小部件。这就是为什么你只能看到最后一个标签。你知道吗

创建一个小部件并将其定义为中心小部件。然后,将标签添加为中心小部件的子级:

class Window(QMainWindow):
    def __init__(self):
        super().__init__()
        self.area = QWidget(self)
        self.setCentralWidget(self.area)

    def berkay(self, pos):
        btn = QLabel("BOOH", self.area)
        btn.move(self.area.mapFromParent(pos)) # Map the pos in the coord system of self.area
        btn.show()

相关问题 更多 >