QSlider focus with a hotkey? - qt

The QSlider documentation says, "A slider accepts focus on Tab and provides both a mouse wheel and a keyboard interface." Is there an easy way to assign a hotkey to it to give it focus similar to how one can assign an Alt key for QCheckboxes?

If you have an associated label, you should be able to use QLabel::setBuddy(QWidget*). Otherwise, you could override the keyPressEvent for the parent form that would have focus and then call QSlider::setFocus(Qt::FocusReason).

Related

Qt QTreeView mouse entered/left event?

There is a "entered" signal for a QTreeView Widget, which I can use to check if my mouse moves over an item.
Is there a way to implement an "left" signal?
Why:
I have a treeView with a lot of items which I also plot via QChart. I want to highlight the plot when someone mouse over the data in the treeview.
I can highlight it via the "entered" signal, but I have no idea how to change it back to the default if the mouse left/mouse is over another item...
entered is a member of QAbstractItemView. You should probably reimplement QAbstractItemView::dragLeaveEvent virtual method.
See this method documentation: http://doc.qt.io/qt-5/qabstractitemview.html#dragLeaveEvent

Qt Squared Radio Button

I want to create a squared radio button, with the text inside the button. Is that possible? I think the button shape could be changed trough css, but what about the text?
Any hint?
Rather than trying to deform a QRadioButton into something which visually resembles a QPushButton, I would simply use a QPushButton with some custom logic.
You won't have to worry about the visual aspect then, while the logic itself is not all that hard to write.
As stated by #besworland, QPushButton inherits from QAbstractButton, which already has the option to be checkable or not. You can set this via setCheckable(bool).
To mimic the "exclusive" behaviour of a set of QRadioButtons, you can add your buttons to a QButtonGroup and make it an exclusive one. As stated in the documentation "An exclusive button group switches off all checkable (toggle) buttons except the one that was clicked." You can use a QButtonGroup's setExclusive(bool) method for that.
In any case, I would consider those easier options than transforming a QRadioButton to fit your needs.

How to make a QLabel turn to be a combobox or edit control, when you clicked this label

It looks like a label normally, once you move the mouse over it, or clicked it, then will turn to be a edit control or combobox likewise.
Use a QStackedWidget. According to the docs, "The QStackedWidget class provides a stack of widgets where only one widget is visible at a time." You can then reimplement QWidget::enterEvent and QWidget::leaveEvent to detect mouse overs and show the appropriate widget.

QAction vs QToolButton and when to override the Basic class?

I've recently been studying Qt, and have the following questions:
What is the difference between QAction and QToolButton?
How do I know when to override QPushButton? For example, should I override in order to be informed when the mouse enters a QPushButton's bounds? I don't need to in order to get the signal click().
Question 1:
QActions are used to define tasks performed by an application in a way which can be understood by a number of different user interface objects. Using an example from the Qt docs for context:
A QAction is defined; it is given an icon image, a text description, a keyboard shortcut and a longer tooltip description, as well as being linked to a user defined function.
newAct = new QAction(QIcon(":/images/new.png"), tr("&New"), this);
newAct->setShortcuts(QKeySequence::New);
newAct->setStatusTip(tr("Create a new file"));
connect(newAct, SIGNAL(triggered()), this, SLOT(newFile()));
Later in the implementation, the QAction is added both to a textual menu bar...
fileMenu->addAction(newAct);
... and an icon-based tool bar:
fileToolBar->addAction(newAct);
The menu bar uses the text description to create an entry in the menu corresponding to the action. Similarly the toolbar uses the icon set on the QAction to create an icon in the toolbar which corresponds to the action. Selecting the menu item, clicking the toolbar icon or pressing the keyboard shortcut will all result in the same effect: that defined by the linking of QAction::triggered() to newFile().
So to directly answer your question: a QAction is an abstract way of defining the various parameters and behaviour of a particular task performed by the application. A QToolbarButton is a UI object (derived from QWidget) created by a QToolbar in response to QToolbar::addAction()
Question 2:
Yes, QPushButton has a clicked() signal inherited from QAbstractButton, but it does indeed lack a way to inform when the mouse has entered its bounds. You have a couple of options in order achieve this, but first you need to first set the mouseTracking property to be enabled. This will allow you to receive mouse move events on the QPushButton even if no mouse buttons are pressed. With that done you need to explore one of the following options:
As you suggest, you could subclass QPushButton and reimplement mousePressEvent in order to respond to the mouse position.
You could install another widget as an eventFilter on the QPushButton, and watch for events of type (QEvent::MouseMove).

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