Qt how to know the widget, in which elements user set focus last time - qt

Qt. I have a form. There is 2 widgets on it, that contains other widgets, buttons, line edits etc.
I need: When user sets focus, clicks by mouse, or does something with first widget or elements, that it contain - I need to set variable to 0. If he do something same with second widget - variable must be set in 1.
How to do that?

QApplication::focusWidget() returns pointer to widget which has focus at this moment. Also there is QApplication::focusChanged(QWidget *old, QWidget *now) signal and you can connect it to slot to change variable.

You can always redefine/(create subclasses by inheritance) the widgets with your own slots and signals.
As far as I can understand your requirement, you can do.
QObject::connect(wid1,SIGNAL(clicked()),yourvariableclass,SLOT(setMyVariable_wid1()));
QObject::connect(wid2,SIGNAL(clicked()),yourvariableclass,SLOT(setMyVariable_wid2()));
If my answer isn't apt for your question, please explain the problem a little more. I can help you with it. :)

Related

How did I retrieve changes in the content of all text edit boxs in a widget

Now, I have a widget that used for conguring some parameters, There were some QlineEdit with default value and a save button on this widget. People may change the content of QlineEdit. And click the save button, so that the modified parameters can take effect. Here is my question:
How do retrieve changes in the content of all text line edit in this QWidget?
Once I know which edit content has changed, I can judge whether the modified values is legal, and then let the change take effect.
Can anyone give me some ideas?
OS: Windows10
QT: qt 5.9.0
For this situation, It's better to do one more step before manually validating the user's input. The step is to limit the user to enter invalid settings. If your setting value is a number, use QSpinBox or QDoubleSpinBox for floating-point values. If you want to let the user select from multiple predefined values, like gender(Male, Female), use QComboBox or QRadioButton and so forth. Here is the list of Qt's widgets. So bear in mind, using QLineEdit for all of the inputs is not a good idea.
If your input is something more complex, you can use validators. For getting the idea see this question.
At last, you connect the save button's clicked signal to the slot defined in your widget class using Qt's signals and slots mechanism and get values from all of your inputs and check them, and if everything is OK, apply them to your system.

Add a delete Button to each Item in QListView

Is it somehow possible to add to each Item in a QListview a Button which is deleting the Object onClick? As shown in the following Picture:
EDIT: As I'm new in QT it would be nice to have some example, to understand it better. And as it seems there are three different Ways? What will be the best? Do use a QAbstractItemView?
Yes. You'll need to use:
QAbstractItemView::setIndexWidget ( const QModelIndex & index, QWidget * widget )
QListView inherits QAbstractItemView and when you're trying to customize list/tree views that's usually the place to look. Be careful though, without a delegate this doesn't scale very well. Check out this thread: http://www.qtcentre.org/threads/26916-inserting-custom-Widget-to-listview
You can also go for a generic approach that can work on variety of containers, including the underlying model of your list view.
Each item in the list has a requestRemoval(Item*) signal and a removeMe() slot, connect the X button to the removeMe() slot in each item constructor, in removeMe() you emit the requestRemoval(this) signal, which is connected to a removeHandler(Item*) slot in your "parent" object upon creation of that item, which receives the pointer of the item which has requests deletion, and removes it from the underlying container being used.
Basically, pressing the remove button causes that particular item to send a pointer of itself to the parent's remove handler which removes that entry.
EDIT: Note that this is a generic approach, as noted in the comments below it can be applied without signals and slots as well, and even though it will work it is not the most efficient solution in your particular case.

How to draw something in a tooltip?

I am trying to develop a functionality using Qt which I don't know whether it is possible to implement. Here is the requirement:
When the user hovers over a node (an object derived from QGraphicsItem), a window will be shown near the node, in the window there might be some histograms or buttons that can be clicked to show further information. When the mouse leaves the window, it will automatically close.
I tried to use a tooltip, because it can pop-up near the node and close when the mouse leaves it, but it can only show text. So, it still cannot work that way. I am wondering if there is another way to do this? I did lots of google search, but there is still no answer.
Thanks so much for helping me with this.
If you're ok with using a 3rd party library, Qxt provides a class that provides a tooltip that is QWidget based, which will let you use an arbitrary widget as the tooltip rather than just text.
See: Qxt::ToolTip
you don't have to use tooltip for your app
you can use or call widget or dialog, on hover mouse event
Please refer Qt Example EmbeddedDialog Example, It is advanced, But you can understand how hover Enter/Leaving events are working. I personally prefer don not create instance of Popupdialog for each item, create it if only nesessary. Otherwise create one dialog and pass its reference to all the items through the constructor initialization.
1. These are the API you are intrested on, reimplemet this.
QGraphicsItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event) and void QGraphicsItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
2. When You create Dialog, You can pass Qt::WindowFlags as Qt::ToolTip.

Customize QListWidgetItem

I am creating my first app in QT and wanted to design a list. The listitem has two texts and one icon.
The problem is, i cant find any example or helping material, Only helping link i found is : Customize QListWidgetItem but i dont understand it. Although i have the same problem which this links points to...
What i understand is, i have two options:
1- Customize QListWidgetItem to use with QListWidget
2- Make some delegate to use with QListView
I was hoping to get started with option 1. Now in the link, some reply talks about "myItem" which is taking 2 texts as input. I want to know the implementation of "myItem".
In the link you posted, MyItem is just a normal QWidget. This means that you can create a widget in Qt Designer and then set that new widget you created as the widget that the QListWidgetItem should use for display. In the above example, MyItem takes two strings because there is a vertical layout with two labels in it (that's my assumption at least).
You should also note, and is discussed in the above link, that taking the approach of setting an item widget to use for every item in the list is an expensive thing to do in terms of performance and memory consumption. Because a QListWidget is a QListView, you can set an item delegate on it just like any other view and come out with a lighter weight solution (one instantiation of class vs. one instantiation for every item in your list)

How can I tell a QTableWidget to end editing a cell?

I'm showing a popup menu to select some values in a QTableWidget. The lowest item is a "Modify list" entry, when I select it a new window should automatically appear and the QComboBox should vanish and the cell return to a Qt::DisplayRole state.
Now Qt has all those nice API-calls like QTableWidget.edit() and QTableWidget.editItem(), what I'm really looking for is a QTableWidget.endEditing(), preferably without specifying the index of the cell, though I could get that using this call:
table.currentIndex()
… but I don't know if I can guarantee that the current cell is the cell being edited at all times.
Is there an API to close those kind of editors?
QTableWidget inherits 19 public slots from QWidget. One of those is setDisabled(), which should disable input events for that widget and all of its children.
I would try:
table.setDisabled( true );
table.setDisabled( false );
Although you said it does not work for you, there is an alternative method:
If you don't like that (the table loses focus, I believe), you can try using EditTriggers. For example:
table.setEditTriggers( QAbstractItemView::NoEditTriggers );
table.setCurrentItem(None) is what worked for me. (Don’t forget to block signals if you use some cellChanged/itemChanged slot function.)
This is with PyQt. For C++ I think replace None with NULL.
You may be able to use QTableWidget.closePersistentEditor() to close the editor. However, QAbstractItemView.closeEditor() may be closer to what you want, especially since you seem to be comfortable with the QModelIndex-based API and are already using a custom editor widget.
In my case, none of the options worked properly. So, I figured: I need to send the key press event to the line edit itself. The following works with QTreeView but probably does work with any other view or widget that opens a line edit to edit cells.
QWidget* editingWidget = treeView->findChild<QLineEdit*>();
if(editingWidget)
{
QKeyEvent keyPressEvent(QEvent::KeyPress, Qt::Key_Return, Qt::NoModifier);
QApplication::sendEvent(editingWidget, &keyPressEvent);
QApplication::processEvents(); // see note below
}
In my case, I wanted to start editing another field directly when having finished editing one item. That is why I put processEvents there, in most cases you can probably remove that line.
PS: yeah, it's C++, but should be easily adaptable to Python. I found this thread when I searched for the C++ solution, so maybe it helps anyone else, too.
I can't speak for list widgets. But, I got here trying to do something similar.
I was double-clicking a cell, and based on the column, bringing up a sub-form with a list, then when that was closed move to the next appropriate column based on the value selected.
My problem was I could get the value in the cell and "select" the next appropriate cell, but the original cell stayed selected in edit mode!
It finally dawned on me that my double-click was selecting the cell, ie. editing.
A single-click selects the cell but doesn't open an edit mode.
Side note: Never could get that sub-form to act truly modal, so I created a loop in the calling form: while the sub form was visible, with the only code being app.processEvents()

Resources