【问题标题】:How to add a menubar(QMainWindow) alongside QGraphicsView in PyQt?如何在 PyQt 中的 QGraphicsView 旁边添加菜单栏(QMainWindow)?
【发布时间】:2018-09-19 17:34:44
【问题描述】:

在窗口上显示带有滚动条的图片。我们可以借鉴它。我想在同一个窗口中添加一个菜单栏。我尝试了以下,它没有工作。当我运行它时,窗口上什么也没有显示。

#!/usr/bin/env python

from PyQt5.QtCore import (QLineF, QPointF, QRectF, Qt)
from PyQt5.QtGui import (QIcon, QBrush, QColor, QPainter, QPixmap)
from PyQt5.QtWidgets import (QAction, QMainWindow, QApplication, QGraphicsView, QGraphicsScene, QGraphicsItem,
                             QGridLayout, QVBoxLayout, QHBoxLayout,
                             QLabel, QLineEdit, QPushButton)

class TicTacToe(QGraphicsItem):
    def __init__(self):
        super(TicTacToe, self).__init__()

    def paint(self, painter, option, widget):
        painter.setPen(Qt.black)
        painter.drawLine(0,100,300,100)


    def boundingRect(self):
        return QRectF(0,0,300,300)

    def mousePressEvent(self, event):
        pos = event.pos()
        self.select(int(pos.x()/100), int(pos.y()/100))
        self.update()
        super(TicTacToe, self).mousePressEvent(event)

class MyGraphicsView(QGraphicsView):
    def __init__(self):
        super(MyGraphicsView, self).__init__()
        scene = QGraphicsScene(self)
        self.tic_tac_toe = TicTacToe()
        scene.addItem(self.tic_tac_toe)

        scene.addPixmap(QPixmap("exit.png"))

        self.setScene(scene)
        self.setCacheMode(QGraphicsView.CacheBackground)
        self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
        self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)


    def keyPressEvent(self, event):
        key = event.key()
        if key == Qt.Key_R:
            self.tic_tac_toe.reset()
        super(MyGraphicsView, self).keyPressEvent(event)

class Example(QMainWindow):

    def __init__(self):
        super(Example, self).__init__()

        self.y = MyGraphicsView()

        self.initUI()


    def initUI(self):               

        menubar = self.menuBar()
        menu = menubar.addMenu('File')
        db_action = menu.addAction("Open file")

        self.setGeometry(30, 30, 30, 20)
        self.setWindowTitle('Menubar')    
        self.show()        


if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)
    mainWindow = Example()

    mainWindow.showFullScreen()
    sys.exit(app.exec_())

【问题讨论】:

    标签: python qt pyqt qgraphicsview qmainwindow


    【解决方案1】:

    如果窗口小部件是窗口的某个组件的子组件,则它会显示在窗口中,在您的情况下,self.y 不是 Example 的子组件,而只是一个属性,可能的解决方案是将其设置为centralWidget:

    class Example(QMainWindow):
    
        def __init__(self):
            super(Example, self).__init__()
    
            self.y = MyGraphicsView()
            self.setCentralWidget(self.y)
    
            self.initUI()
    
    
        def initUI(self):               
    
            menubar = self.menuBar()
            menu = menubar.addMenu('File')
            db_action = menu.addAction("Open file")
    
            self.setGeometry(30, 30, 30, 20)
            self.setWindowTitle('Menubar')    
            self.show()        
    

    【讨论】:

    • 我试过这个。现在正在显示图片,但不是菜单栏。怎么看菜单栏?
    • @Aquarius_Girl 你想要菜单栏还是工具栏?
    • 好吧,toobar 是合适的,因为我必须向它添加几个按钮。
    • @Aquarius_Girl 菜单栏有一个添加操作的菜单,在左上角显示的图像中。你想要什么?
    • 我试过这个self.toolbar = self.addToolBar('Tools'),现在顶部显示了一个栏。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-12
    相关资源
    最近更新 更多