QMenuBar only contains inactive menu items on OS X - c++

For some reason all my menu bar items are greyed out when I use the native menu bar on OS X Mavericks:
I create the menu actions using the following code:
newAct = new QAction(tr("&New"), this);
newAct->setShortcuts(QKeySequence::New);
newAct->setStatusTip(tr("New"));
newAct->setShortcutContext(Qt::ApplicationShortcut);
newAct->setEnabled(true);
newAct->setAutoRepeat(false);
addAction(newAct);
connect(newAct, SIGNAL(triggered()), this, SLOT(newFile()));
...
These actions are then added to the menubar like this:
// _menuBar = new QMenuBar(0);
_menuBar = menuBar();
//_menuBar->setNativeMenuBar(false);
fileMenu = _menuBar->addMenu(tr("&File"));
fileMenu->addAction(newAct);
Uncommenting the first line shows the same behaviour. It does however work fine when I use the the non-native menu bar.
Qt version:
$ /usr/local/qt/5.3/clang_64/bin/qmake -v
QMake version 3.0
Using Qt version 5.3.1 in /usr/local/qt/5.3/clang_64/lib
Any ideas/suggestions?

I suspect this line is your culprit:
addAction(newAct);
You shouldn't be adding the QActions to your window, since you'll be adding them to the fileMenu object instead. Try removing the above line.

I had the same problem.
Setting the windowModality property of my MainWindow to NonModal worked for me.

Related

Scroll Area Added and Set Up but No Scrollbar Appears

I've seen and tried various QT scrollArea solutions over the past 2 days but none of them work for me. Here's my scroll area setup code as it stands in the MainWindow constructor. This builds and runs without error but doesn't do anything. The scrollArea and ui->Contents have already been set up in the form using QTcreator and the needed widgets have been moved into the scrollArea.
ui->scrollArea->installEventFilter(this);
ui->scrollArea->setMouseTracking(true);
ui->scrollArea->setWidget(ui->Contents);
QVBoxLayout *layout = new QVBoxLayout(this);
layout->setSizeConstraint(QLayout::SetMinimumSize);
ui->scrollArea->setLayout(layout);
The last line seems interchangeable with:
layout->addWidget(ui->scrollArea)
but neither one changes the result, which is a fully-functioning application but without the scroll area I need.
I had similar problem which i solved by creating scrollArea and it's contents via code rather than form and only then using setWidget() method. I described the problem in this thread.
In your case code should look something like this:
QScrollArea *scrollArea;
scrollArea = new QScrollArea(this);
scrollArea->installEventFilter(this);
scrollArea->setMouseTracking(true);
scrollArea->setWidget(Contents);//whatever Contents is, i recommend creating it via code
QVBoxLayout *layout = new QVBoxLayout(this);
layout->setSizeConstraint(QLayout::SetMinimumSize);
scrollArea->setLayout(layout);

How to Change the IconSize for Actions in QMenu?

I am trying to resize the Icons of QActions in the QMenu with the following code but it doesn't work.
QMenu *menu;
menu =new QMenu();
menu->setStyleSheet("QMenu::icon{height:20px;width:20px});"
I would really appreciate it if someone could provide a solution.
Here is the solution that worked for me:
QMenu *menu;
menu =new QMenu();
QToolButton *button=new QToolButton(menu);
button->setFixedSize(50,50);
QWidgetAction *action=new QWidgetAction(this);
action->setDefaultWidget(button);
menu->addAction(action);
We can set style sheet to manage icon size like this:
QAction *action = new QAction("Exit", this);
action->setIcon(QIcon(":/images/resources/exit.png"));
QMenu *menu = new QMenu("File");
menu->addAction(action);
menu->setStyleSheet("QMenu {icon-size: 200px;} QMenu::item {background: transparent;}");
ui->menubar->addMenu(menu);
But it will display in an Improper size, so it's better to use QToolBar.
In your cpp file type this:
ui->ToolBarName->setIconSize(QSize(50,50));
In Designer Click on your QToolbar and set iconSize.
Just stumbled across this after all these years. I remember I had this problem once and now again. This time I actually managed to solve it somewhat. It IS kinda weird tho and should receive some love at least documentation-wise.
The key is: You need to style QMenu AND QMenu::item If you just set the icon size via:
QMenu {icon-size: 40px;}
it will remain ignored until you also set something like
QMenu::item {background: transparent;}
Unfortunately this resets the menu stylesheet and you need to do something about the hover state to make it usable. But well.
Seems this works for me.
(also posted this on the qt forums)

Clicking QSystemTrayIcon brings up an empty context menu on Mate desktop

I'd like to create a tray icon for my application for showing the main window on clicking on it after the former was minimized.
Here's the implementation:
TrayIcon.h:
class TrayIcon_t : public QSystemTrayIcon {
Q_OBJECT
public:
TrayIcon_t();
};
TrayIcon.cpp:
TrayIcon_t::TrayIcon_t() {
setIcon(QIcon(":/icons/tray.ico"));
}
Main.cpp (part only, there's no more code related to the tray):
TrayIcon_t *tray = new TrayIcon_t;
QObject::connect(tray, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), &MainWindow, SLOT(show()));
tray->show();
While on LXDE it works just fine, on MATE desktop it mostly opens a small menu (I think) containing no items and doesn't emit the signal required by the main window to be shown. See this picture.
Do you think I've encountered a bug in Qt 5.7.0?
Turns out that this is a bug. Reported it and became accepted at:
https://bugreports.qt.io/browse/QTBUG-55717

Event for QMactoolbar in QT for Mac

I have problem with QT in Mac.
I create QMactoolbar and I want to add textfield ( look line QLineEdit in Windows ), but when I using "connect" for it, I don't see SIGNAL "textChange" (look like textChanged(QString) of QLineEdit in windows). Please help me create SIGNAL for it, or create textfield in Mac ( don't using QLineEdit because it use in Windows, not Mac)
QMacToolBar *mactoolbar = new QMacToolBar();
QList<QMacToolBarItem *> list;
QMacToolBarItemEx *item = new QMacToolBarItemEx(); // create item
item->customViewStyle(QMacToolbarItemEx::SearchTexfieldView);
list.append(item);
mactoolbar -> setItems(list);
connect(item,SIGNAL(activated()),this,SLOT(close()));

QAction shortcut doesnt always work

I have a Qaction on a menu item for deleting selected items in one of my views. Here is how i create the action:
deleteAct = new QAction( tr("Delete Selected"), this);
deleteAct->setShortcut(QKeySequence::Delete);
connect(deleteAct, SIGNAL(triggered()), this, SLOT(deleteSelected()));
I setup a keyboard shortcut (Delete Key) which should trigger the delectAct action. It works most of the time but at some points it stops working... Does anyone know why the shortcut would stop working?
Note: the action still works if i trigger it from the menu item. Its just the shortcut that doesn't...
You need to add the action to a widget, since it's the widget that will be listening for key events.
Assuming "this" is a mainwindow, simply do
addAction(deleteAct);
Note that you can add the same action to multiple widgets (that's the whole point of the separated action concept). So it's fine to add it to the mainwindow and to a menu.
Try changing the shortcut context of the action, for example:
deleteAct->setShortcutContext(Qt::ApplicationShortcut);
The shortcut works depending on the focus of the application views.
I wanted to have shortcuts working on buttons.
In my application I changed the shortcut context of the action,
added the action to the widget
and finally to the subviews of the application.
Then the necessary signals and slots of widget an action must be connected.
const QAbstractButton*button = dynamic_cast<QAbstractButton*>(widget);
action->setShortcutContext(Qt::WidgetWithChildrenShortcut);
widget->addAction(action);
ui->textBrowser->addAction(action);
ui->treeSource->addAction(action);
if (button)
{
if (button->isCheckable())
{
action->setCheckable(true);
if (button->isChecked()) action->setChecked(true);
connect(action, SIGNAL(triggered(bool)), button, SLOT(setChecked(bool)));
connect(button, SIGNAL(clicked(bool)), action, SLOT(setChecked(bool)));
}
else
{
connect(action, SIGNAL(triggered()), button, SLOT(click()));
}
}
Without seeing the complete code, I'd hazard a guess that somewhere it gets enabled/disabled. Make sure that the shortcut is getting hit in the constructor and not 'disabled' somewhere else because of a setting perhaps.
You can use http://doc.qt.io/qt-5/qaction.html#shortcutVisibleInContextMenu-prop property since QT 5.10 for this:
deleteAct->setShortcutVisibleInContextMenu(true);