Qt combobox: Event when item is selected - c++

i need to know what is the way to run a function when one item of the combobox is selected/clicked, i tried with the standar connect:
connect(ui->combobox,SIGNAL(clicked()),this,SLOT(clickedaction()));
... but it doesnt work.
I dont even need a custom action per element, just the same for all.
Thanks in advance.

There is no signal clicked() in QComboBox.
If you need to detect the item activation either by mouse click or enter pressed then use activated(int index).
connect(ui->combobox,SIGNAL(activated(int)),this,SLOT(clickedaction(int)));

Related

is there a way to minimize/hide and show the QListWidget in my UI

This is my UI, on the right side I have a QListWidget with 3 items, when I click these items, the corresponding entries on the left side of the UI must be populated , anyways The QListwidget on the right side, i do not want it to be always visible, i want the QListwidget to be minimized usually and show up once the "show options>>" button is clicked, and when i click it again, it should be minimized again,
is there any way to accomplish this using QT Designer or code, what widgets or tools i need to use to get this ?
you can set the visibility to false, this will hide the listView
ui->myList.setVisible(false);
maybe define 1 slot taking a bool var to set the visibility to true or false.
then code the slot of the click of the button so that is calls the slot that hide/show the list!
Connect a slot to QPushButton::clicked() "Show Options >>" button and use setVisible(true) on your listwidget.
Connect a slot to QListWidget::itemClicked(QListWidgetItem *item). This is your place where you can handle which item the user clicked and populate the data. And then, use again setVisible(false) function to hide the list.
If you wish to set width of the form, depending of the list appearance, use setFixedWidth() of your form to extend it or shrink it. Use width() function of your list, get its width and to add it or subtract it from the form's width()

Qt Radio Button only call SLOT() when button is actually checked

I've got a group of radio buttons. These are all connected to the same SLOT(updateImage()).
Here's how they are set up:
connect(m_radio1, SIGNAL(toggled(bool)), this, SLOT(updateImage());
connect(m_radio2, SIGNAL(toggled(bool)), this, SLOT(updateImage());
connect(m_radio3, SIGNAL(toggled(bool)), this, SLOT(updateImage());
When changing the radio button from one to the next, this SLOT() is called twice, once for de-selecting the previously selected radio button, and once for selecting the clicked radio button.
I was wondering, is there a way to modify my SLOT() to only occur in one of these cases, when the clicked item becomes checked?
Thanks
You are asking to connect to the toggled signal - you are called whenever the object is toggled. What's surprising?
You could connect to the "clicked" signal which provides a "checked" argument you can test.
Btw; if using a modern Qt version, you should ditch the SIGNAL and SLOTS macros and instead use the new connect() syntax that's checked at compile time.
Since your signal have a bool parameter.
toggled(bool)
you could add that parameter to your SLOT method.Then check if the parameter is checked.
(As a complement: How to do so in Python?) Assume you have a radio button radio_button and you would like to connect it to slot _my_slot while passing args as the arguments, such that radio_button only send a signal once it is selected, then you need to add the following:
radio_button.clicked.connect(lambda: _my_slot(args))

How can I change the information from my window in QT after clicking a push button

I want to have two buttons PREVIOUS and NEXT and I would like to have the same window but every time the NEXT button is clicked I want the information to be changed and something new to appear.
How can I do something like this?
You should write 2 slot functions and connect with buttons. For example slotPrevious() and slotNext() and connect them with connect(ui->pushButtonNext, SIGNAL(clicked()), this,SLOT(slotNext()));. And update your information widget with these functions.
well in order to help what do you want to be changing? Text labels, input fields,buttons?
Please tell what do you want to change to make you a sample...

CancelEvent equivalent in Qt signals

I'm usually used to the .net framework and recently got the chance to work on a project using C++/Qt. In this context, I want to implement the following functionality: (to simplify things lets say I have a ListView and a Textbox/Textedit)
If I select an item of the ListView -> show the corresponding text in the textedit (done)
If I edit the text and click on another item in the list -> show a dialog that asks whether to save or cancel the changes (done). The textedit gets a signal from the list telling it that the selected item has changed.
If the user presses save -> save it back and refresh the item in the list, the new item which the user clicked gets selected in the list (done)
If the user presses cancel -> don't select the other item which the user clicked (this is where my problem is)
Basically I see two solutions (there may be more) to this:
Send a signal back from the textedit to the list, telling the list to restore the previous selection. I personally dont like this solution very much, because I have to couple them stronger (the list sends signals to the textedit and now also the other way around). Also, this 'forced' selection restoration could again trigger subsequent signals that would have to be blocked again...
A much nicer solution (I think) would be to have some Veto-mechanism in the signals. I could imagine the list to send two signals:
lets call the first one aboutToChangeSelection(proposedSelection, vetoObj)
and another one after the action is done: changedSelection(newSelection)
The first signal would be sent to the textedit, which would then eventually make use of its veto. After this, the same signal would be sent to the list itself, do the action depending of the veto and send the second signal if it actually changed the selection.
In the .NET world there exists such a mechanism, with the help of CancelEventArgs. I know that .NET events and Qt signals have quite a different working principle, but is there any Qt equivalent to archieve the same effect?
Thanks a lot for any help!
If you are using a QListWidget instead of a QListView, then you can do the following:
listen to the signal : QListWidget::itemPressed ( QListWidgetItem * item ) rather than the signal saying the item has changed (ie currentItemChanged ( QListWidgetItem * current, QListWidgetItem * previous ) in my example )
The function which opens the dialog is a slot connected to this signal. I don't think it need to be modified.
Listen to the signal : currentItemChanged ( QListWidgetItem * current, QListWidgetItem * previous ) which will be used only to remember the current and last item.
Now in your case, when the user set cancel, programmatically change back to the last item.
Now there is one problem. Which signal will be received first?? Not specified in the doc, and it can be in any order. If a user is on item A, and clicks on item B, you want lastitem to be A, and current item to be B at the moment you are handling itemPressed. So you want to handle currentItemChanged first. Fortunately you can do use Qt::DirectConnection for currentItemChanged and use Qt::QueuedConnection for itemPressed
ALTERNATIVE:
You can use an event filter on the list widget. This filter will be the one to perform the processing you described in your few steps. If the user clicks accepts, you send the event to the list view. If the user reject, you block the event. I am not sure, but it is possible that the events are not processed in a filter which will make this alternative non-viable.
Not 100% sure on this, but you could override QCoreApplication::notify( QObject * receiver, QEvent * event ) and try to find the right event and just call ignore() on it. And to tell when to ignore, you could add a new QEvent subclass, embed the target elements pointer in it and make it act as your veto event - if that is received then you know you are gonna ignore next event of some specific kind for that particular object.

Qt/C++: Signal for when a QListWidgetItem is checked?

In my form I have a QListWidget which contains checkable QListWidgetItems. I'm looking for a way to capture the event of a QListWidgetItem being checked/unchecked. I don't see any such signal existing for this but maybe I'm wrong. What I'm currently doing is using the QListWidget::itemClicked() signal and checking the checkState of the QListWidgetItem, but this isn't what I want because this event happens any time the item is clicked, not just went the checkmark is toggled. Can anyone give some assistance? Thanks!
Apparently no such signal is provided, your best bet is to use QListWidget::itemChanged(QListWidgetItem* item) , and scan the resulting item->checkState(). This should be a slight improvement over using itemClicked
An extra option is to use your own QAbstractListModel with a QListView. At first this does add some extra code as you need to add your own management code . But you do get a lower level access. Basically because the QListView will ask your model what to do. It will also relay input back to your listmodel where you can hook into it.
Alternatively you could subclass QStandardItemModel and catch certain edits related to changing the checkbox.