问题描述
所以基本上我想使用 PySide 和 Qt 框架创建一个 GUI 应用程序.我正在使用 QT 设计器进行初始 UI 设计.该应用程序的第一个版本将在 Mac 上运行,我希望它像其他 Mac 应用程序一样,其中应用程序的名称以粗体显示,并且一直向左显示关于"、首选项"和退出".
So basically I want to create a GUI app using PySide and the Qt Framework. I am using the QT designer to make the initial UI design. The first version of the app will run on Mac and I want it to be like other Mac applications where the name of the app is in bold and all the way to the left with an "About", "Preferences", and "Quit".
问题是,每当我添加这些类型的字符串时,下拉菜单就会停止工作.
The problem is that whenever I add these types of strings the drop down stops working.
任何有关这方面的提示都会有所帮助,这是我使用 PySide、QT 框架和 QT 设计器的第一个 GUI.
Any tips on this would be helpful this is my first GUI using PySide, QT Framework, andd QT Designer.
推荐答案
以下是在 Mac 上使用 C++ 使 About
菜单项正常工作的示例.关键是要setMenuRole
到正确的角色.有退出、关于、首选项和关于 Qt 的角色.应用程序名称以粗体显示的菜单项由操作系统提供,您无需执行任何特殊操作即可获得该菜单项.Qt 会自动将具有正确角色的项目移动到它们所属的位置.您无需执行任何操作即可获得退出"菜单项,如果您不提供,它会自动添加.
Below is an example of getting the About
menu item working correctly on Mac, in C++. The key is to setMenuRole
to the correct role. There are roles for Quit, About, Preferences, and About Qt. The menu item with application's name in bold is provided by the OS, you don't need to do anything special to get that. Qt will automatically move items with correct roles where they belong. You don't need to do anything to get the Quit menu item, it's added automatically if you don't provide one.
如果您在 Qt Designer 中制作菜单,您只需设置这些菜单 QAction 的 menuRole
属性.这就是菜单转到正确位置所需的全部内容.不要添加带有您的应用程序名称的菜单.只需创建通常的 Windows 样式菜单(文件、编辑、帮助),项目就会根据其角色进行适当的重新排列.
If you're making menus in Qt Designer, you simply set the menuRole
property of those menu QActions. That is all that's needed for the menus to go to correct places. Do not add a menu with your application's name. Simply create usual Windows-style menus (File, Edit, Help), and the items will be rearranged appropriately to their roles.
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
a.setApplicationVersion(...);
a.setOrganizationName(...);
a.setOrganizationDomain(...);
a.setApplicationName(...);
MainWidget w; // MainWidget is your widget class
QMessageBox * aboutBox = new QMessageBox(&w);
QImage img(":/images/youricon.png");
aboutBox->setIconPixmap(QPixmap::fromImage(
img.scaled(128, 128, Qt::KeepAspectRatio, Qt::SmoothTransformation)));
QString txt;
txt = txt.fromUtf8(
"fooapp %1\nCopyright \xC2\xA9 2012 Ed Hedges\n"
"Licensed under the terms of ....");
txt = txt.arg(a.applicationVersion());
aboutBox->setText(txt);
QMenuBar menu;
QMenu * submenu = menu.addMenu("Help");
QAction * about = submenu->addAction("About", aboutBox, SLOT(exec()));
about->setMenuRole(QAction::AboutRole);
w.show();
return a.exec();
}
这篇关于Qt 不允许我创建一个以我的应用程序命名的菜单项,其中包含字符串“About"、“Preferences"或“Quit?有小费吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!