Qt - Symbian Mobile - Soft key menus not displaying correctly - qt

I create and show a QWebView with soft key options at the bottom. When I click "Options", a menu shows up, but it's tiny, black, and in the upper left hand corner (it should look like the standard blue soft keys and be directly above them). I followed this example.
//create webview
webView = new QWebView;
webView->setUrl(QString(":html/internal.html"));
//create menu
QAction *option1 = new QAction(tr("Back"), webView);
option1->setSoftKeyRole(QAction::PositiveSoftKey);
connect(option1, SIGNAL(triggered()), this, SLOT(deleteView()));
//create right softkey action to launch the "options" menu
QAction *option2 = new QAction(tr("Options"), webView);
option2->setSoftKeyRole(QAction::NegativeSoftKey);
connect(option2, SIGNAL(triggered(), this, SLOT(showMenu()));
QMenu *menuOptions = new QMenu(webView);
menuOptions->addAction(tr("Sub Menu 1"), this, SLOT(aboutView()));
menuOptions->addAction(tr("Sub Menu 2"), this, SLOT(aboutView()));
option2->setMenu(menuOptions);
//add softkey menus
QList < QAction* > softKeys;
softKeys.append(option1);
softKeys.append(option2);
webView->addActions(softKeys);
webView->show();

This example works well on simulator and also in phone. It is included in the QTDIR/examples/widgets folder since Qt 4.6.x

Related

QAction doesn't show QMenu

I'm creating my UI from Qt Designer and it generares this code:
toolBar = new QToolBar(MainWindow);
QIcon icon;
icon.addFile(QStringLiteral(":/main"), QSize(), QIcon::Normal, QIcon::Off);
MainWindow->addToolBar(Qt::TopToolBarArea, toolBar);
actionConvert = new QAction(MainWindow);
actionConvert->setObjectName(QStringLiteral("actionConvert"));
actionConvert->setIcon(icon);
toolBar->addAction(actionConvert);
Now, back in my frame code:
QMenu *menuAdd = new QMenu (this);
menuAdd->addAction (tr("&Files..."));
menuAdd->addAction (tr("&Directory..."));
ui->actionConvert->setMenu (menuAdd);
When I run the application I can see the qaction in the toolbar even the arrow pointing down, which indicates that there is a menu, but when I click it, the menu doesn't appear...any ideas?
There does not seem to be anything wrong with your example code.
It's possible that the reason you aren't seeing the menu is that you need to press and hold the button for a few seconds in order for the menu to appear. A single click will just execute the button's normal action.
See: QToolButton::ToolButtonPopupMode.
You should add menu with menuBar() method as in my case:
void MainWindow::ueInitMenu()
{
this->ueSetCodeRegisterPlacesAction(new QAction(tr("Places"),
this));
this->ueCodeRegisterPlacesAction()->setShortcut(tr("Ctrl+P"));
this->ueCodeRegisterPlacesAction()->setStatusTip(tr("Shows places code register"));
connect(this->ueCodeRegisterPlacesAction(),
SIGNAL(triggered()),
this,
SLOT(ueSlotShowPlacesView()));
this->ueSetCodeRegisterMenu(this->menuBar()->addMenu(tr("Code register")));
this->ueCodeRegisterMenu()->addAction(this->ueCodeRegisterPlacesAction());
} // ueInitMenu
especialy the line:
this->ueSetCodeRegisterMenu(this->menuBar()->addMenu(tr("Code register")));
so in your case:
this->menuBar()->addMenu(tr("System menu");
and then add actions. Also take a look at Menus Example.

Simple ribbon: how to use QActions within QTabBar?

I'm trying to implement simple tabbed interface with Qt5. I use QTabWidget with QToolBars placed inside its tabs and I add QActions to the QToolBars.
That works but causes the following issue: any action remains accessible only while its parent tab is active. If I try to use keyboard shortcut for currently "invisible" action, I will have no success. Since there's no menu etc, the tabs are the only place, where the actions are placed.
Here's how I add the elements to the toolbar:
QTabWidget *ribbon = new QTabWidget(window);
QToolBar *tool_bar_game = new QToolBar(tab_game);
QAction *action_go_to_next_level = new QAction(window);
action_go_to_next_level->setText(QApplication::translate("Window", "&Next", 0));
action_go_to_next_level->setIcon(QIcon::fromTheme("go-last"));
action_go_to_next_level->setShortcut(QApplication::translate("Window", "PgDown", 0));
ribbon->addTab(tool_bar_game, tr("Game"));
tool_bar_game->addAction(action_go_to_next_level);
and a screenshot:
How can I make the action accessible with shortcuts, even when the action's parent tab is not currently opened?
I'm not surprised that this doesn't work, effectively you try to use a shortcut on a hidden widget. It would be very confusing if this worked.
The obvious workaround for this is to add the shortcut instead of to the QAction to a widget that is always active. Personally, I suggest the window.
Without having tested the code, I believe this should work:
QTabWidget *ribbon = new QTabWidget(window);
QToolBar *tool_bar_game = new QToolBar(tab_game);
QAction *action_go_to_next_level = new QAction(window);
action_go_to_next_level->setText(QApplication::translate("Window", "&Next", 0));
action_go_to_next_level->setIcon(QIcon::fromTheme("go-last"));
QShortcut *page_down = new QShortcut(QKeySequence("PgDown"), window);
// trigger the action when the shortcut is activated
QObject::connect(page_down, &QShortcut::activated,
action_go_to_next_level, &QAction::trigger);
ribbon->addTab(tool_bar_game, tr("Game"));
tool_bar_game->addAction(action_go_to_next_level);

How to undock tab with osgViewer from QTabWidget?

I want to undock a QWidget from a QTabWiget (is set as centralWidget). The tab contains some Open Scene Graph content (OpenGL Window). When removing the Tab from the list and putting it into a new Dialog Window (=> undocking from tab) the scene data seems to be corrupt. It works with "standard widgets" but the osg seems to forget the scene.
Surprisingly, undocking works when using a QDockWidget (scene is visible after undocking the window).
Anyone knows how to undock a tab without corrupting the osgViewer?
Code called for to undock from tab and show in new dialog window:
QWidget* gv = // points to an osgViewer in a qt widget
QDialog* dlg = new QDialog(this);
dlg->setWindowTitle("hello earth");
QHBoxLayout* pMainLay = new QHBoxLayout;
gv->setMinimumSize(100,100);
gv->setGeometry(100,100,300,300);
pMainLay->addWidget(gv);
dlg->setLayout(pMainLay);
ui->tabWidget->removeTab(0); // removes the tab at position 0 (docked window)
dlg->show(); // should show the undocked dialog
There is nothing to see in the new dialog. Did I missed something?
How to "copy" the osg view properly into a new widget/dialog? Should I use a composite viewer for this kind of task? It seems there is not even the empty osg view visible (no blue canvas)...
It could be that something's going screwy when you add the osgViewer to another widget before removing it from the QTabWidget. Changing the order might help.
QWidget* gv = // points to an osgViewer in a qt widget
ui->tabWidget->removeTab(0); // removes the tab at position 0 (docked window)
QDialog* dlg = new QDialog(this);
dlg->setWindowTitle("hello earth");
QHBoxLayout* pMainLay = new QHBoxLayout;
pMainLay->addWidget(gv);
dlg->setLayout(pMainLay);
dlg->show(); // should show the undocked dialog

Qt setting text under tool button

I want to show text for the tool button icons using setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
I can see the text for actions directly added to Toolbar (Close & Save), but not for an action(Load) that is added to a QMenu in a Toolbutton. I have added this action in Qmenu to work it as a toggle with other actions(recent files).
I tried to set the text for the Toolbutton too using setText() and setWindowIconText(), but it doesn't work. This is how it looks right now.
Below is the code snippet for the same.
actionLoad = new QAction(QIcon(QString("%1/cn_open.png").arg(imageDir)),tr("Load"), this);
actionLoad->setShortcut(tr("Ctrl+L"));
actionLoad->setStatusTip(tr("Load the model"));
connect(actionLoad, SIGNAL(triggered()), this, SLOT(loadModelDlg()));
actionClose = new QAction(QIcon(QString("%1/cn_close.png").arg(imageDir)),tr("Close"), this);
actionClose->setShortcut(tr("Ctrl+X"));
actionClose->setStatusTip(tr("Close the Model"));
connect(actionClose, SIGNAL(triggered()), this, SLOT(closeModel()));
actionSave = new QAction(QIcon(QString("%1/cn_save.png").arg(imageDir)),tr("Save"), this);
actionSave->setShortcut(tr("Ctrl+S"));
actionSave->setStatusTip(tr("Save the Model"));
connect(actionSave, SIGNAL(triggered()), this, SLOT(saveModel()));
m_FileToolBar = addToolBar(tr("File"));
// Show text under the icon in toolbar
m_FileToolBar->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
// Add a menu for recent file items
m_FileMenu = new QMenu();
m_FileMenu->addAction(actionLoad); // Add load button as the first item
for (int i = 0; i < MaxRecentFiles; ++i)
m_FileMenu->addAction(recentFileActions[i]);
updateRecentFileActions();
// Create a tool button. Load button and recent files will be added as a drop down menu
m_FileToolButton = new QToolButton();
m_FileToolButton->setText(tr("Load")); // Not working
m_FileToolButton->setWindowIconText(tr("Load")); // Not working
m_FileToolButton->setMenu(m_FileMenu);
m_FileToolButton->setDefaultAction(actionLoad);
// This creates a dropdown arrow to click.
m_FileToolButton->setPopupMode(QToolButton::MenuButtonPopup);
m_FileToolBar->addWidget(m_FileToolButton);
// These actions show text under the icon
m_FileToolBar->addAction(actionClose);
m_FileToolBar->addAction(actionSave);
Any help to resolve this is appreciated.
Why don't you try something like that:
QToolBar bar;
QToolButton button;
button.setPopupMode(QToolButton::MenuButtonPopup);
button.setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
QAction loadAction(QIcon(":/img/openfile"),"Load",&button);
button.addAction(&loadAction);
button.setDefaultAction(&loadAction);
QAction loadAction2("Load 2",&button);
button.addAction(&loadAction2);
bar.addWidget(&button);
bar.show();
I didn't use a QMenu as you can see above.

Creating a Symbian's like application menu at S60

I wanted to make an app which is when started it shows a menu. It is just like when you open a Messaging app in S60. Here is a screenshot.
How do I make it? I have tried to make the centralWidget of the QMainWindow as QMenu, and adding QAction to the QMenu. But when I'm running it, the app don't show anything. And I have tried to make the QMenu using QMenuBar. And it is show okay. But I can't use the up/down key to select menu in the device. And when I press the options key (Qt::PositiveSoftKey), the menubar shows up too. And I didn't even add that to menuBar() which is owned by QMainWindow.
Here is my first code:
QAction* act1= new QAction(tr("act1"),this);
QObject::connect(tes,SIGNAL(triggered()),this,SLOT(close()));
QAction* act2= new QAction(tr("act2"),this);
QObject::connect(tes,SIGNAL(triggered()),this,SLOT(close()));
QMenu* menu = new QMenu(this);
menu->addAction(act1);
menu->addAction(act2);
setCentralWidget(menu);
And it shows nothing at the apps.
And here is my second try:
Qt Code: Switch view
QAction* act1= new QAction(tr("act1"),this);
QObject::connect(tes,SIGNAL(triggered()),this,SLOT(close()));
QAction* act2= new QAction(tr("act2"),this);
QObject::connect(tes,SIGNAL(triggered()),this,SLOT(close()));
QMenuBar* menubar = new QMenuBar(this);
QMenu* menu = menubar->addMenu(tr("menu"));
menu->addAction(act1);
menu->addAction(act2);
setCentralWidget(menu);
It shows the menu. But when I deploy to the device, I can't use the keypad to select the menu. And at the simulator, if I click other place than the QAction item, the menu lost.
I am using another approach by using QPushButton with Vertical Layout. Here is the code:
QWidget* centralWidget = new QWidget(this);
QScrollArea* scrollArea = new QScrollArea(this);
scrollArea->setWidget(centralWidget);
scrollArea->setWidgetResizable(true);
setCentralWidget(scrollArea);
QVBoxLayout* centralLayout = new QVBoxLayout(centralWidget);
QPushButton* button1 = new QPushButton(tr("button 1"));
QPushButton* button2 = new QPushButton(tr("button 2"));
centralLayout->addWidget(button1);
centralLayout->addWidget(button2);
centralLayout->setContentsMargins(0,0,0,0);
button1->setFocus();
Here it's look:
Okay, and that's look good enough. But if that's have 8 button. If it is only have 2 button, it looks like this:
looks kind of weird, huh? Any way to prevent this?
For me, the UI that you're trying to replicate is a list of options, so, why don't try to create the UI based on a list widget?
Asumming that you're going to use a list, you have to options from which you have to choose based on your needs and your app requirements:
Qt Widgets: Available in all the Qt supported Plataforms. Created for desktop and works great on it, but in feel strange on mobile devices. Take a look to the QListWidget and QListView.
Qt Quick: It isn't available for S40 phones or S60 3rd edition, not available for non-touch phones, basically. Created for "touch enabled UI's", it doesn't offer a stable set of widgets (buttons, comboboxes) yet, but it offers a set of primitives (like rectangles or images) that gives you a lot of freedom to create your UI's and it looks pretty good. I think that this is what Lucian used to create the UI from his answer.
This examples could be of particular interest: http://doc.qt.io/archives/qt-4.7/all-examples.html
Run the QtDemo to see all the examples live!
Hope it helps!
EDIT: Adding example of QListWidget
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
for(int i=0; i<10; i++) {
QListWidgetItem *item = new QListWidgetItem(QIcon("Qt.png"), QString("Item %1").arg(i));
ui->listWidget->insertItem(i, item);
}
ui->listWidget->setIconSize(QSize(64, 64));
connect(ui->listWidget, SIGNAL(itemClicked(QListWidgetItem*)), this, SLOT(onItemClicked(QListWidgetItem*)));
}
void MainWindow::onItemClicked(QListWidgetItem *item)
{
QMessageBox::information(this, "", QString("%1 pressed").arg(item->text()));
}
You have all the freedom in the world to create your layout the way you like. Want those buttons closer together? Want them centered in the view?
The attached image took me 30 seconds to create and looks already decent (according to my pour designer skills).

Resources