PyQt5 QMenu no focus on exec_() - qt

I have an already fully functioning app, with a custom widget that opens a custom QMenu when clicked, by calling exec_():
menu.exec_(QPoint)
I recently migrated from PyQt4->PyQt5 and noticed that the focusOutEvent method I was overriding was no longer responsive. I already have a fix, which works just fine:
menu.setFocus() #calling this prior to exec_()
Nevertheless, I would like to understand what exactly is different after the Qt migration, that I had to manually set the focus in order to keep desired functionality.
The menu is set to have StrongFocus.
Another apparent issue that might be related is that the setting of the cursor to hand fails to change back to regular cursor when the cursor leaves the menu rect.
Thank you! =)

Related

JavaFX ComboBox border is wrong if the drop-down drops-"up"

I am using "javafx.scene.control.ComboBox" on Java 8 and I noticed that whenever the combobox does not have room below and instead pops up, the bordering styling of the elements switches as if it still pops down.
How can I access the styling for that to fix it?
Managed to fix this by actually extending the ComboBoxListViewSkin. In there, I've stuck a method that updates the styling, and does that by calling super.getPopup(), gets the AnchorY of that and compares it with the combo-box Y. After determining if the popup is below or above the combo, I set the correct styling on super.getListView...
Also, that method I've added, has to be called from the "ON_SHOWN" event of the combo-box.
I've tried several other variants but the damn thing just yields unstable behavior.

Google VR Reticle Click on UI Button

So I am having this issue with using Google VR reticle where I cannot click a button. I have an image attached showing the heirarchy and the PlayButton is what I am trying to click. The Canvas has a Graphic Raycaster, the button has an Event Trigger that calls the method to navigate to the next scene. The UpScrollPanel, and DownScrollPanel work just fine. The EventSystem has the Gaze Input Module, as well as Event System, and Touch Input Module.
Any ideas on how to get this working? I have watched a few videos from NurFACEGAMES and while they helped a little, I haven't gotten the click to work yet.
Oh, and I am using Unity 5.3.4f
Sometimes things can get in the way of the button, make sure that no other UI elements overlap it, for example text borders (which are actually larger than they appear). You can also fix this by moving the button up the hierarchy among its siblings, I believe the first child is top.
Also try moving the button up the hierarchy if possible, sometimes UI having certain parents makes them not work
The canvas object should have a graphic raycaster
I found the issue to be unrelated to anything I thought it was. The menu I was using is a prefab I also use in another view that isn't VR. The scrollrect was loading that prefab, instead of the modified one I was using in the VR menu, and therefore the triggers I had added to the button were no being used when the app loaded.

How to prevent QWebEngineView to grab focus on setHtml(...) and load(...) calls?

I have created simple Qt Window Layout with QTreeView and QWebEngineView: after selecting some item in the tree view, the Web engine view shows some content. The problem is what when QWebEngineView::setHtml(...) or load(...) is called the tree view loses keyboard focus and Web engine view gets it. This causes difficulties when selecting items with keyboard in the tree view. So, how to prevent the tree view focus lost?
I tried to use QTextBrowser instead of QWebEngineView. It doesn't have this problem, but it is not suitable for complex HTML pages.
Suppose we have:
QWebEngineView *webView = new QWebEngineView;
For Qt 5.8 and newer
The problem can be solved by tweaking settings:
webView->settings()->setAttribute(QWebEngineSettings::FocusOnNavigationEnabled, false);
The sample code: https://github.com/rmisev/FocusWidget/tree/if-qt-5.8
References:
QWebEngineView::settings()
QWebEngineSettings Class
QWebEngineSettings::setAttribute(WebAttribute attribute, bool on)
QTBUG-52999
For Qt 5.7 and earlier
The simplest solution (also pointed by #Netrix) is to call:
webView->setEnabled(false);
But this disables keyboard input to the webView.
To solve this problem I created the simple FocusWidget class as parent widget for webView, which works as follows:
Initially it disables webView (webView->setEnabled(false)), so prevents to take focus on load(...), setHtml(...) calls.
When FocusWidget gets focus, it enables and forwards focus to webView, so enables keyboard input.
When webView and its children loses focus, FocusWidget disables webView again
The source code and sample application: https://github.com/rmisev/FocusWidget
I encountered the same problem in my app in PyQt5. What I managed to do is to disable the whole widget like this (in Python):
view = PyQt5.QtWebEngineWidgets.QWebEngineView()
view.setEnabled(False)
This resulted in view being operational with mouse (it accepts mouse clicks and things can be changed on web page) but i won't take focus from different widget.
I would definitely call it a bug instead of feature in QtWebEngineWidgets.
Thanks to all the others who provided answers for this. After encountering this bug I was somewhat stumped, but after learning of the way setEnabled(false) affects the focus stealing from the other answers I discovered that simply disabling the webview, setting the html or reloading it, then re-enabling it circumvents the issue (at least in Qt 5.7):
I.E.
myWebView->setEnabled( false );
myWebView->setHtml( html );
myWebView->setEnabled( true );
or
myWebView->setEnabled( false );
myWebView->reload();
myWebView->setEnabled( true );
This allows you to fix the problem without needing to subclass anything as one of the other answers suggests.

How do I get rid of this anonymous draggable bar in qtcreator?

I wrote a program in qt creator, but it includes this anonymous draggable toolbar that I would like to get rid of. It does not show up in design mode. If I right click on it, it gives me a menu with a checkbox, which I can select to get rid of it while the program is running. What is this bar? How do I get rid of it? If I knew what it was, I could just programmatically tell it to hide when the program loads.
Here are some screenshots demonstrating this weird problem. In the first screenshot, the mouse is pointed at the bar. The second one demonstrates the right-click context menu. The third one shows the program without the bar. The box can also be dragged out of the program's window entirely. I can also upload a picture of that, if necessary.
(mouse pointing at bar)
(bar has a right-click menu!)
(bar is gone)
First things first, make sure you haven't added the toolbar yourself in code (the Designer form is nothing magical, it just generates normal Qt code to ui_XXXX.h, which you should totally study, so you understand what is really happening "under the hood").
Second, perhaps that is the main menu bar, not the main tool bar.
Then, on to remove the nasty bar. On the upper right corner of Design view, you have a tree of all items in your UI. Find the QToolBar there. It is probably called mainToolBar. Right click it and remove it there. Or, if you actually have menu bar, then find QMenuBar, probably called menuBar, and remove it instead. Remember to make version control commit (or other backup) before doing this, so you can easily revert changes.
If this fails for whatever reason, and you just want to get it to work, you can always find and delete (be careful about the dangling pointers left behind, set them to nullptr if possible):
ui->setupUi(this);
// a hack: we want to get rid of the toolbar and the menubar,
// but can't remove it from the .ui, so we delete them like this.
delete findChild<QToolBar *>(); // NULL return value is ok for delete
delete findChild<QMenuBar *>(); // NULL return value is ok for delete
Doc links for findChild and alternative findChildren.
What you have there is probably the mainToolBar QToolBar object you get with every QDesigner-created QMainWindow .ui file.
If you want to get rid of it, locate it in your .ui file and just delete it.
But better think twice: you might later decide you want a QToolBar object and (at least AFAIK) there is no way to recreate it in QDesigner...
you must use this code in your constructor.
ui->mainToolBar->toggleViewAction()->setVisible(false);
mainToolBar is my toolbar name.

QSystemTray problems

I am trying to use QSystemTrayIcon for my application and i am facing some problems.
It is the first time i use qt so i am not really used to it.
I followed this tutorial to make a system tray icon but i fail to customize it.
I want to have a button show/hide and not 3 show, hide, restore. These actions are really confusing for a newbie and i dont know what to do and what to connect.
I tried some things but with no luck.
Also when the system tray menu appears if you click somewhere else, the menu stays open.
Any way to solve this thing too?
If you want to remove one of the menu items, modify the createTrayIcon function so that it only adds the actions you need (and clean up the unused members once you get it to work). It's that simple.
If you want a single menu item or button to toggle between visible and hidden, you'll need to create a custom slot that calls show() or hide() (or setVisible(bool)) depending on whether the widget is hidden or not (use isVisible() for that for example). Then connect your action to that slot.
Read the Signals and Slots documentation and examples for information about how to create a new slot.

Resources