PDA

View Full Version : Android: menuBar()->addAction broken?



sedi
6th July 2014, 23:33
Hi,
I want to have a clickable menu item that exits the app without having to open a subMenu.


Actions added with menuBar().addAction() are *not* added/displayed in Qt5.3.1 on Android 2.3.1
A connection between a subMenu's menuAction() signal and some closing slot returns a "true" but never fires.


A small compileable example:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
public slots:
void endMe();
private:
Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H


und


#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMenuBar>
#include <QDebug>
#include <QMessageBox>

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{

menuBar()->setNativeMenuBar(true);
menuBar()->setEnabled(true);
ui->setupUi(this);

//Not visible with Android
QAction* directAct=menuBar()->addAction("DirectA",this,SLOT(endMe()));
directAct->setVisible(true);
directAct->setEnabled(true);

//Visible but not firing
QMenu* directMenu=menuBar()->addMenu("DirectM");
directMenu->setEnabled(true);
connect(directMenu->menuAction(),SIGNAL(triggered()),this,SLOT(endMe() ));

//Visible, working (but one more click)
QMenu* normalMenu=menuBar()->addMenu("Normal");
QAction* normalActA=normalMenu->addAction("A",this,SLOT(endMe()));
QAction* normalActB=normalMenu->addAction("B",this,SLOT(endMe()));
normalMenu->setEnabled(true);
normalActA->setEnabled(true);
normalActB->setEnabled(true);
}

MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::endMe()
{
QMessageBox::about(this,"End","App will close now");
QApplication::quit();
}


It works on Windows, it doesn't on Android. Any ideas?

sedi
8th July 2014, 01:12
Attached you find the example project that demonstrates the problem.

No ideas anyone?

adutzu89
19th July 2014, 10:37
I don't seem to find a QMenuBar into your project, neither in designer, or code.
I am not an expert, but I use menubars in the following way.

//create menubar
QMenuBar *menuBar=new QMenuBar(this);
//create menu
QMenu *menu=new QMenu(tr("Menu'),this);
//create action
QAction *action=new QAction(tr("Action"),this);
//connect action signal to slot
connect(action,SIGNAL(triggered()),this,SLOT(SLOT( endMe()));

//add action to menu
menu->addAction(action);

//add menu to menubar
menuBar->addMenu(menu);

//set menubar to mainwindow
setMenuBar(menuBar);

Also worth mentioning QMenuBar is not visible in Android, but if you tap on the menu\option "button" on the device it should popup the menus.

sedi
27th July 2014, 01:08
Thank you for your answer! I use the MenuBar that is provided by QMainWindow. I have tried subclassing it to catch the mousePress / mouseRelease event, but it seems to be an event key "43" that triggers the menuBar. After using that to do what I want, the MenuBar is be unusable until the program has been ended and restarted. Weird thing. I've actually given up here.