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

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.

Related

Buttons in vertical layout in QDialog with return value

I want to create a QDialog with Qt that looks somewhat like this:
The desired properties here are:
I can add a text to the dialog explaining the question behind it
I can add several buttons that are in a vertical layout
I can retrieve the value of the clicked button, i.e. I know if the user cancelled or clicked 1, 2 or 3 - ideally I can emit a signal with the corresponding value as parameter.
The dialog has a certain minimum height and width.
I have used a QMessageBox before for this purpose, but I can't get it to use a vertical layout. I have experimented with a QDialogButtonBox and a QDialog, but I can't really figure out how to get my desired return value easily.
A piece of code creating this dialog with code how to retrieve the clicked value would be great!
Did you try a QDialog widget? you can add QDialog without buttons to your project. In UI designer, add Vertical layout, label, and buttons.
In your class define your signals and emit it when the user clicked on buttons.

qml how to keep focus on TextField

I want to keep focus on TextField. For example i am typing something then tap to button. The focus on TextField is moving to button, that's why keyboard is automatically hiding on Android. I am using Qt 5.9.2. Thanks in advance!
In Qt Quick Controls 2, each control has a focusPolicy property which determines how the control gets focus. The default for controls like Button is Qt.StrongFocus, which means that buttons get focus after being clicked or tabbed into. If you're seeing that a control has focus and you don't want it to, just set its focusPolicy to Qt.NoFocus:
focusPolicy: Qt.NoFocus

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

Remap context menu call on qwidget

I have my custom widget inherited from QWidget, and I've connected the widget's menu-calling signal to my slot.
connect(m_ontologyView, SIGNAL(customContextMenuRequested(QPoint)), SLOT(showContextMenuSlot(QPoint)));
Now I want user to be able to change button calling the context menu. Normally it's called with right mouse button, but how do I tell the widget to call the menu with a button of my choice?
I'm on Qt 5.4.0
Instead of using QWidget::customContextMenuRequested, you will need to reimplement the widgets mouse event functions, QWidget::mousePressEvent, QWidget::mouseReleaseEvent and QWidget::mouseMoveEvent. Inside of these events, you can then show you menu using QMenu::popup. (The point can be extracted from the mouse events).

Qt: How to click and drag in a QGraphicsView to rubber band select items that accept mouse clicks?

According to the QGraphicsView docs, dragMode's behavior "only affects mouse clicks that are not handled by any item." It then says "You can define a custom behavior by creating a subclass of QGraphicsView."
In my case I'd like clicks on an item that accepts mouse clicks to call the item's mouse clicks as normal. And I'd like clicks not on those items to start a rubber band drag (also as normal). However, I'd like to be able to ctrl-click the view, and have that start a rubber band drag selection rather than call the item's mouse event methods.
I know how to assess whether or not ctrl is associated with a mouse click event:
if (event->modifiers().testFlag(Qt::ControlModifier))
However, I don't know what to do with this condition, or where exactly to put it. Should I put it in QGraphicsItem, QGraphicsScene, or QGraphicsView? QGraphicsView seems most likely, as the docs somewhat hint.
You need to put the condition to QGraphicsItem like this:
if (event->modifiers().testFlag(Qt::ControlModifier))
{event->setAccepted(false); return;}
Now You can handle the event in Your QGraphicsView.

Resources