在 QDialog 中添加了 QMenuBar,它显示但子菜单未打开

发布于 2024-11-10 16:51:35 字数 4592 浏览 6 评论 0原文

我使用设计器中设置的布局在 QDialog 中添加了 QMenuBar ,然后手动添加 QMenuBar ,当运行应用程序和 QDialog 运行时,我看到 QMenuBar 仅包含菜单标题,而没有打开子菜单:
这是我的代码:
这是从设计器生成的头文件:

QT_BEGIN_NAMESPACE

class Ui_AutoDialog
{
public:
    QVBoxLayout *verticalLayout;
    QWidget *widget_menuBarHolder;
    QVBoxLayout *verticalLayout_2;
    QVBoxLayout *verticalLayout_menuBarLayOut;
    QWebView *webView;
    QWidget *widget;

    void setupUi(QDialog *AutoDialog)
    {
        if (AutoDialog->objectName().isEmpty())
            AutoDialog->setObjectName(QString::fromUtf8("AutoDialog"));
        AutoDialog->resize(436, 365);
        verticalLayout = new QVBoxLayout(AutoDialog);
        verticalLayout->setSpacing(0);
        verticalLayout->setContentsMargins(0, 0, 0, 0);
        verticalLayout->setObjectName(QString::fromUtf8("verticalLayout"));
        widget_menuBarHolder = new QWidget(AutoDialog);
        widget_menuBarHolder->setObjectName(QString::fromUtf8("widget_menuBarHolder"));
        widget_menuBarHolder->setMinimumSize(QSize(0, 20));
        verticalLayout_2 = new QVBoxLayout(widget_menuBarHolder);
        verticalLayout_2->setSpacing(0);
        verticalLayout_2->setContentsMargins(0, 0, 0, 0);
        verticalLayout_2->setObjectName(QString::fromUtf8("verticalLayout_2"));
        verticalLayout_menuBarLayOut = new QVBoxLayout();
        verticalLayout_menuBarLayOut->setSpacing(0);
        verticalLayout_menuBarLayOut->setObjectName(QString::fromUtf8("verticalLayout_menuBarLayOut"));

        verticalLayout_2->addLayout(verticalLayout_menuBarLayOut);


        verticalLayout->addWidget(widget_menuBarHolder);

        webView = new QWebView(AutoDialog);
        webView->setObjectName(QString::fromUtf8("webView"));
        webView->setUrl(QUrl("about:blank"));

        verticalLayout->addWidget(webView);

        widget = new QWidget(AutoDialog);
        widget->setObjectName(QString::fromUtf8("widget"));
        widget->setMinimumSize(QSize(0, 20));

        verticalLayout->addWidget(widget);


        retranslateUi(AutoDialog);

        QMetaObject::connectSlotsByName(AutoDialog);
    } // setupUi

    void retranslateUi(QDialog *AutoDialog)
    {
        AutoDialog->setWindowTitle(QApplication::translate("AutoDialog", "Dialog", 0, QApplication::UnicodeUTF8));
    } // retranslateUi

};

namespace Ui {
    class AutoDialog: public Ui_AutoDialog {};
} // namespace Ui

QT_END_NAMESPACE

这是 QDialog 构造函数,这是构建 QMenuBAr 的部分:

ui.setupUi(this);
    /*ui.verticalLayout_menuBarLayOut->addWidget(SetMenuBar());
    ui.verticalLayout_menuBarLayOut->setMargin(0);
    ui.verticalLayout_menuBarLayOut->setSpacing(0);*/

    m_actionClose = new QAction(this);
    m_actionClose->setObjectName(QString::fromUtf8("actionClose"));
    m_actionPreferences = new QAction(this);
    m_actionPreferences->setObjectName(QString::fromUtf8("actionPreferences"));

    m_menubar = new QMenuBar(this);
    m_menubar->setObjectName(QString::fromUtf8("menubar"));
    m_menubar->setGeometry(QRect(0, 0, 314, 18));
    m_menuFile = new QMenu(m_menubar);
    m_menuFile->setObjectName(QString::fromUtf8("menuFile"));
    m_menuSettings = new QMenu(m_menubar);
    m_menuSettings->setObjectName(QString::fromUtf8("menuSettings"));

    m_menubar->setMinimumSize(QSize(0, 20));
    ui.verticalLayout_menuBarLayOut->setMenuBar(m_menubar);
    m_menubar->addAction(m_menuFile->menuAction());
    m_menubar->addAction(m_menuSettings->menuAction());
    m_menuFile->addSeparator();
    m_menuFile->addAction(m_actionClose);
    m_menuSettings->addAction(m_actionPreferences);

    m_actionClose->setText(QApplication::translate("MainWindow", "Close", 0, QApplication::UnicodeUTF8));
    m_actionPreferences->setText(QApplication::translate("MainWindow", "Preferences", 0, QApplication::UnicodeUTF8));
    m_menuFile->setTitle(QApplication::translate("MainWindow", "File", 0, QApplication::UnicodeUTF8));
    m_menuSettings->setTitle(QApplication::translate("MainWindow", "Settings", 0, QApplication::UnicodeUTF8));




    bar = new QStatusBar(this);
    bar->setMinimumSize(QSize(0, 20));
    ui.verticalLayout->removeWidget(ui.widget);
    ui.verticalLayout->addWidget(bar);
    ui.verticalLayout->setMargin(0);
    ui.verticalLayout->setSpacing(0);



    pb = new QProgressBar(bar);
    pb->setTextVisible(false);
    pb->hide();

    bar->addPermanentWidget(pb);

这里出了什么问题,为什么我看不到子菜单。偏好?

i added QMenuBar in QDialog using layout that is set in Designer and then manully adding the QMenuBar , when running the application and the QDialog runs i see the QMenuBar with only the menu header without the opening sub menu :
here is my code :
this is the header file generated from the Designer :

QT_BEGIN_NAMESPACE

class Ui_AutoDialog
{
public:
    QVBoxLayout *verticalLayout;
    QWidget *widget_menuBarHolder;
    QVBoxLayout *verticalLayout_2;
    QVBoxLayout *verticalLayout_menuBarLayOut;
    QWebView *webView;
    QWidget *widget;

    void setupUi(QDialog *AutoDialog)
    {
        if (AutoDialog->objectName().isEmpty())
            AutoDialog->setObjectName(QString::fromUtf8("AutoDialog"));
        AutoDialog->resize(436, 365);
        verticalLayout = new QVBoxLayout(AutoDialog);
        verticalLayout->setSpacing(0);
        verticalLayout->setContentsMargins(0, 0, 0, 0);
        verticalLayout->setObjectName(QString::fromUtf8("verticalLayout"));
        widget_menuBarHolder = new QWidget(AutoDialog);
        widget_menuBarHolder->setObjectName(QString::fromUtf8("widget_menuBarHolder"));
        widget_menuBarHolder->setMinimumSize(QSize(0, 20));
        verticalLayout_2 = new QVBoxLayout(widget_menuBarHolder);
        verticalLayout_2->setSpacing(0);
        verticalLayout_2->setContentsMargins(0, 0, 0, 0);
        verticalLayout_2->setObjectName(QString::fromUtf8("verticalLayout_2"));
        verticalLayout_menuBarLayOut = new QVBoxLayout();
        verticalLayout_menuBarLayOut->setSpacing(0);
        verticalLayout_menuBarLayOut->setObjectName(QString::fromUtf8("verticalLayout_menuBarLayOut"));

        verticalLayout_2->addLayout(verticalLayout_menuBarLayOut);


        verticalLayout->addWidget(widget_menuBarHolder);

        webView = new QWebView(AutoDialog);
        webView->setObjectName(QString::fromUtf8("webView"));
        webView->setUrl(QUrl("about:blank"));

        verticalLayout->addWidget(webView);

        widget = new QWidget(AutoDialog);
        widget->setObjectName(QString::fromUtf8("widget"));
        widget->setMinimumSize(QSize(0, 20));

        verticalLayout->addWidget(widget);


        retranslateUi(AutoDialog);

        QMetaObject::connectSlotsByName(AutoDialog);
    } // setupUi

    void retranslateUi(QDialog *AutoDialog)
    {
        AutoDialog->setWindowTitle(QApplication::translate("AutoDialog", "Dialog", 0, QApplication::UnicodeUTF8));
    } // retranslateUi

};

namespace Ui {
    class AutoDialog: public Ui_AutoDialog {};
} // namespace Ui

QT_END_NAMESPACE

and here is the QDialog constructor , the part where is building the QMenuBAr:

ui.setupUi(this);
    /*ui.verticalLayout_menuBarLayOut->addWidget(SetMenuBar());
    ui.verticalLayout_menuBarLayOut->setMargin(0);
    ui.verticalLayout_menuBarLayOut->setSpacing(0);*/

    m_actionClose = new QAction(this);
    m_actionClose->setObjectName(QString::fromUtf8("actionClose"));
    m_actionPreferences = new QAction(this);
    m_actionPreferences->setObjectName(QString::fromUtf8("actionPreferences"));

    m_menubar = new QMenuBar(this);
    m_menubar->setObjectName(QString::fromUtf8("menubar"));
    m_menubar->setGeometry(QRect(0, 0, 314, 18));
    m_menuFile = new QMenu(m_menubar);
    m_menuFile->setObjectName(QString::fromUtf8("menuFile"));
    m_menuSettings = new QMenu(m_menubar);
    m_menuSettings->setObjectName(QString::fromUtf8("menuSettings"));

    m_menubar->setMinimumSize(QSize(0, 20));
    ui.verticalLayout_menuBarLayOut->setMenuBar(m_menubar);
    m_menubar->addAction(m_menuFile->menuAction());
    m_menubar->addAction(m_menuSettings->menuAction());
    m_menuFile->addSeparator();
    m_menuFile->addAction(m_actionClose);
    m_menuSettings->addAction(m_actionPreferences);

    m_actionClose->setText(QApplication::translate("MainWindow", "Close", 0, QApplication::UnicodeUTF8));
    m_actionPreferences->setText(QApplication::translate("MainWindow", "Preferences", 0, QApplication::UnicodeUTF8));
    m_menuFile->setTitle(QApplication::translate("MainWindow", "File", 0, QApplication::UnicodeUTF8));
    m_menuSettings->setTitle(QApplication::translate("MainWindow", "Settings", 0, QApplication::UnicodeUTF8));




    bar = new QStatusBar(this);
    bar->setMinimumSize(QSize(0, 20));
    ui.verticalLayout->removeWidget(ui.widget);
    ui.verticalLayout->addWidget(bar);
    ui.verticalLayout->setMargin(0);
    ui.verticalLayout->setSpacing(0);



    pb = new QProgressBar(bar);
    pb->setTextVisible(false);
    pb->hide();

    bar->addPermanentWidget(pb);

what is wrong here why i can't see the sub menus.. Close & Preferences?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱的故事 2024-11-17 16:51:35

我不熟悉添加菜单的方式,因为我通常以不同的方式将菜单添加到菜单栏。

尝试

m_menubar->addMenu( m_menuFile );
m_menubar->addMenu( m_menuSettings );

而不是

m_menubar->addAction(m_menuFile->menuAction());
m_menubar->addAction(m_menuSettings->menuAction());

让我知道这是否有效。如果没有,我一定会调查另一个问题。

I'm not familiar with the way you're adding the menus, because I usually add menus to a menubar in a different way.

Try

m_menubar->addMenu( m_menuFile );
m_menubar->addMenu( m_menuSettings );

instead of

m_menubar->addAction(m_menuFile->menuAction());
m_menubar->addAction(m_menuSettings->menuAction());

and let me know if that worked. If not, there must be another problem I will look into.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文