Setting button focus in a graphcisview - qt

I have placed a few buttons in a Qgraphicsscene, but I don’t know how to navigate to the button from a keyboard.
How would I set the focus to a button from the keyboard?

I assume that you used QGraphicsScene::addWidget() to add the button to the scene? It gives you a proxy object back, QGraphicsProxyWidget *, which inherits QGraphicsItem::setFocus(). But remember that it needs to have set the ItemIsFocusable flag and needs to be visible and active as well.
Additionally (from the setFocus() documentation):
As a result of calling this function, this item will receive a focus in event with focusReason. If another item already has focus, that item will first receive a focus out event indicating that it has lost input focus.

Related

Let QTabWidget switch to corresponding page when mouse cursor hover on the tab

I want to show the corresponding page when mouse cursor hover on the tab of a QTabWidget.
For example, when the mouse cursor hover on tab ‘page2’ here , I hope the QTabWidget shows the corresponding page automatically instead of clicking. How to implement this feature?
You may try adding an event filter on the QTabWidget object's QTabBar in order to trap the mouse move event. In the filter handler, use QTabBar::tabAt( QPoint ) to find which tab is below the cursor. Set up a timer when the cursor first enters a given tab, reset time when cursor leaves it. When the timer fires, switch active tabs.
You may try using setTabToolTop function.
ui->tabWidgetHz->setTabToolTip(0,"tooltip for tab1.");
ui->tabWidgetHz->setTabToolTip(1,"tooltip for tab2.");
ui->tabWidgetHz->setTabToolTip(2,"tooltip for tab3");

Intercepting mousePressEvent on Child widget (qt)

I'm trying to implement the following functionality based on QPlainTextEdit - by default it should show a "please enter something here" message, on click, it would vanish and allow editing.
To do so I need to intercept whether the widget has been clicked. Can I do so without sub classing QPlainTextEdit?
Can the parent's widget onMousePressEvent obtain which child's widget the event belongs to?
Can I do so without sub classing QPlainTextEdit?
Yes, you can implement an event filter:
http://qt-project.org/doc/qt-4.8/eventsandfilters.html#event-filters
Essentially, you can filter out the mouse events destined for the plain text edit. Whenever your filter encounters a mouse press event, you can clear the contents of the plain text edit.
Can the parent's widget onMousePressEvent obtain which child's widget
the event belongs to?
Not without event filtering. Qt's event propagation system sends events to child widgets first and then only to parent widgets if the child widget does not accept the event.

QTableView: Best way to change activation-trigger to double-click

In my application, I have one tableview of items, and a side-panel "preview":ing the latest selected item.
I want clicking on an item to change the selection, and double-clicking to cause a "run"-action to be performed. More specifically, I want the "run"-action (including key-navigation and pressing enter) to be bound to the "activation" of the item in the table-row.
My problem is; single-clicks does not only change the selection, but fires the "activated" signal on the item. I would like to tweak it such that:
Navigation Keys, Single Mouse Click: Selection-change, update preview-panel
Enter Key, Double Mouse Click: Activate/run/open action triggered.
Is there a nice clean way to do it, or are overriding the onclick/doubleclick events my best option? Or is there some other tabular list-widget better suiting my needs?
I would connect the slot for the preview action to the currentChanged() signal of the table view's selectionModel(). This covers single clicks and key navigation.
Then there's two options for the double clicks and Enter key presses:
Subclass your tableview, override doubleClickEvent() and keyPressEvent() and fire your custom signal in there, with maybe the model index or something else as an argument. Then just connect your run method to your own signal as you have full control over when it is fired.
If you don't want to subclass, you can use the installEventFilter() mechanism.
Either I'm getting your approach wrong or I'm too tired, but if you want to trigger a run event you should avoid the activated signal completely. Set the signal slot mechanism so that your double click and Enter key press event trigger your run() function, and then the single click/nav buttons should trigger the 'activated' slot which will return your index in the tableview.
I'm pretty certain Qt wants you to be explicit about which signal points to which slot or it'll ignore it or point to a default.

Flex Panel doesn't call focusInHandler() when it gets focus

I want to be notified when a FLex Panel gets or loses focus. I've overridden the focusInHandler() and the focusOutHandler(), but they don't get called when I click on the panel.
The panels style changes indicating that it has the focus, but the handler doesn't get called.
What am I missing?
Containers (and implicitly panels) aren't really focusable. Meaning that simply clicking on an empty container won't give it focus, and in consequence, won't trigger the event handler for "focusIn". In order for a container to "gain" focus, a child of that container, that implements IFocusManagerComponent interface, has to gain focus.
So if you want your panel to trigger the "focusIn" event when clicking on it, you should focus a focusable child of that panel on mouse click.
Have you tried to explicitly listen for that event:
myPanel.addEventListener(FocusEvent.FOCUS_IN, myEventHandler);
and made sure that it was getting called?

How to remove spacebar action from QToolButton

I have a QToolBar on which there is a QToolButton. When QToolButton is pressed by mouse click then it perform some action. The same action is performed when space bar is pressed. I dont want the action to be fired on space bar press, but I want it on mouse click. How I can do this?
Subclass QToolButton using inheritance and override QWidget::keyPressEvent(). There, check if the key you get is Qt::Key_Space and if it is, return and do nothing. if it isn't pass the event to QToolButton.
Use the set focus policy with no focus, this prevents the keyboard from having focus but still allows mouse clicks.
Button->setFocusPolicy(Qt::NoFocus)
Make sure you didn't accidentally assign 'Space' as the shortcut to the action associated with the QToolButton in question (check out Qt Creater's action editor).

Resources