相关疑难解决方法(0)

在PyQt5中缺少menuBar

我一直在使用PyQt5开发GUI,并希望包含一个菜单栏.但是,当我编写此功能时,我的菜单不会出现.弄清楚我对如何在PyQt5中实现菜单栏的理解是关闭的,我在网上找了一个预先存在的例子.通过一些调整,我开发了以下测试用例:

import sys
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QApplication, QMainWindow, QMenuBar, QAction, qApp

class Example(QMainWindow):

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

        exitAction = QAction(QIcon('exit.png'), '&Exit', self)
        exitAction.triggered.connect(qApp.quit)

        menubar = self.menuBar()
        fileMenu = menubar.addMenu('&Testmenu')
        fileMenu.addAction(exitAction)

        self.show()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())
Run Code Online (Sandbox Code Playgroud)

然而,当我运行它时,Testmenu无处可寻.

我还尝试在QTCreator中创建菜单栏(以及我的GUI布局的其余部分),然后使用pyuic5将.ui文件转换为可导入的.py文件.我认为这会消除我的一些编程错误,但菜单栏仍然不会显示.有什么想法吗?

编辑:

我在Jupyter笔记本4.1版本中使用Python 3.5(Anaconda 4.1)运行此代码.我也在使用运行os 10.1l,PyQt 5.7和Qt 5.7.0版本的Macbook.

我已经意识到,如果我单击应用程序窗口然后单击返回窗口,菜单栏将变为响应 - 有效地不聚焦并聚焦应用程序.有了这些信息,我意识到我不是第一个注意到这个问题的人(见https://github.com/robotology/yarp/issues/457).不幸的是,我仍然不确定如何解决这个问题.

python macos pyqt menubar pyqt5

7
推荐指数
3
解决办法
5918
查看次数

MenuBar在MAC OS LION上发布QT 4.7.4

我是QT编程的新手.我想创建一个简单的菜单栏,其中包含两个菜单和几个操作(FILE菜单有3个操作,VIEW菜单有一个操作).我有createMenus()方法,我创建那些菜单和操作然后我将创建的菜单添加到菜单栏,但是当我运行应用程序时,它没有显示这个菜单栏,我不知道为什么.谁能说出问题是什么?

MainWindow.cpp的源代码

#include <QtGui>
#include <QAction>
#include "MainWindow.h"

MainWindow::MainWindow() {

        // Creeam fereastra principala
    QWidget *window = new QWidget;
    setCentralWidget(window);

    // Creeam eticheta unde vom afisa titlul item-ului selectat
    // Implicit va avea un titlu predefinit
   infoLabel = new QLabel("Selectati un item va rog ");

    createActions();
    createMenus();

    // Creeam un layout pentru pozitionarea etichetei
    QHBoxLayout *layout = new QHBoxLayout;

    layout->addWidget(infoLabel);
    window->setLayout(layout);

    setWindowTitle("GUI");
    setMinimumSize(300, 300);
    resize(480,320);
}

void MainWindow::contextMenuEvent(QContextMenuEvent *event) {

    QMenu menu(this);
    menu.addAction(newAction);
    menu.addAction(openAction);
    menu.addAction(closeAction);
    menu.addAction(preferencesAction);
    menu.exec(event->globalPos());
}

void MainWindow::new_() …
Run Code Online (Sandbox Code Playgroud)

qt4 menubar qt-creator

6
推荐指数
1
解决办法
1904
查看次数

标签 统计

menubar ×2

macos ×1

pyqt ×1

pyqt5 ×1

python ×1

qt-creator ×1

qt4 ×1