Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt not letting me create a menu item named after my app with the strings "About", "Preferences", or "Quit? Any tips?

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.

Any tips on this would be helpful this is my first GUI using PySide, QT Framework, andd QT Designer.

like image 958
edhedges Avatar asked Jun 18 '12 01:06

edhedges


1 Answers

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.

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();
} 
like image 165
Kuba hasn't forgotten Monica Avatar answered Sep 29 '22 10:09

Kuba hasn't forgotten Monica