python QWidget::setLayout: Attempting to set QLayout "" on MenuDemo ""

在继承QMainWindow时setLayout报错了,可以通过QWidget设置

# -*- coding: utf-8 -*-
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *


class MenuDemo(QMainWindow):
    def __init__(self, parent=None):
        super(MenuDemo, self).__init__(parent)
        wid = QWidget(self)
        self.setCentralWidget(wid)
        layout = QHBoxLayout()
        bar = self.menuBar()
        file = bar.addMenu("File")
        file.addAction("New")
        save = QAction("Save", self)
        save.setShortcut("Ctrl+S")
        file.addAction(save)
        edit = file.addMenu("Edit")
        edit.addAction("copy")
        edit.addAction("paste")
        quit = QAction("Quit", self)
        file.addAction(quit)
        file.triggered[QAction].connect(self.processtrigger)
        wid.setLayout(layout)
        self.setWindowTitle("menu 例子")
        self.resize(350, 300)


    def processtrigger(self, q):
        print(q.text() + " is triggered")


if __name__ == '__main__':
    app = QApplication(sys.argv)
    demo = MenuDemo()
    demo.show()
    sys.exit(app.exec_())

posted @ 2021-05-25 13:33  fly_bk  阅读(240)  评论(0编辑  收藏  举报