Qt4 QTableView and checkbox click event - c++

I have created QTableView and add an QStandardItem checkbox into with this code:
....
QStandardItem *chkItem = new QStandardItem(true);
chkItem->setCheckable(true);
chkItem->setData(Qt::AlignCenter, Qt::TextAlignmentRole);
tblModel->appendRow(chkItem);
...
where tblModel is QStandardItemModel.
Now i wonder how can i catch the checkbox changed state event?
I have tryed catch the table click event but this is triggered every times user click on a cell (i have no idea to know whether the checkbox state has changed or not). Please give me a way to solve this problem.

Try QStandardItem::checkState() to retrieve the Qt::CheckState of the checkbox. If you need to know, if the state changed, then save the old state somewhere and check if the state changed on the click event.

Related

How to get a signal from QTreeWidget when its selection is changed by user action, but not programmatically?

I need to get a signal from a QTreeWidget that its selection has been changed by the user. This includes the user clicking on an item in the widget, or using the keyboard to change the selection.
QTreeWidget does provide the signal itemSelectionChanged but it fires anytime the selection changes for any reason, including it being changed programmatically, so it doesn't fulfill my requirements.
How can I accomplish this?

QTreeWidgetItem Change - Detect Enter/ESC

I'm developing a Qt/C++ app with a QTreeWidgetItem. When a new item is created I set it setEditable and it allow me to fill directly in the UI the new name.
I'm currently using itemChanged as shown below to catch the change and save it. By default, I set the new item name to new folder and after I can change it manually. My issue is when I'm creating the item, it becomes editable and if I press Enter or Esc without any changes, the itemChanged is not generated.
Is there a command I can use based on SIGNAL/SLOT which can catch the Enter/Esc event. The goal is to triggered the same signals
connect(this, SIGNAL(itemChanged(QTreeWidgetItem*, int)),
this, SLOT(onTreeItemChanged(QTreeWidgetItem*)));
I want to connect Enter/Esc signals to onTreeItemChanged as it's done for itemChanged.
I have tried the use of itemActivated, but it's not triggered even if Enter is pressed.
connect(this, SIGNAL(itemActivated(QTreeWidgetItem*, int)),
this, SLOT(onTreeItemChanged(QTreeWidgetItem*)));
Any idea,
Seb
Sorry to write this in answer, but i still cant comment:
I had some (maybe) similar problems concerning a class derived from QCombobox, which had some spetial behaviour, when to show the popup and when not to.
Everytime Return was pressed domething happend but it wasent a QEventKeyEvent, the solution was to catch QEvent::Shortcut, because the element interpreted this key as shortcut for accept.
-- EDIT --
In such cases i often install a event fiolter and let it just write the events to the output, using a switch statement to filter out uninteresting elements, till i get the culprit.

How to get UI objects to only appear in dialog AFTER button is clicked

I have a dialog that initially has several buttons, let's call them Write, View, OK, and Cancel.
The way it should to is to have the dialog upon creation only have those three buttons and nothing more.
When the Write button is cancelled, it's supposed to create a QLineEdit object in the window above the buttons where the user can enter a new string,which when OK is then clicked will be added to an external QStringList.
When View is clicked, LineEdit should go away (if it's up) and a QListView to come up instead to view everything in that list.
The problem is, I know how to use hide() to get objects that are already in the dialog to NOT appear.
but I am having trouble figuring out how to get an object not currently on the table to appear. I'm new to using Qt so it may be something easy I'm just accidentally overlooking (in fact I hope it is).
Could anyone please offer advice? Thanks!
Just create the items normally and then set:
ui->control->setVisible(false);
after you have created the UI (after ui->setupUi(this);) possibly in the constructor (in case you use code generated by Qt Creator).
And when you need them:
ui->control->setVisible(true);
Doc for this:
http://qt-project.org/doc/qt-4.8/qwidget.html#visible-prop
when using a QListView you should also have a QListModel that provides the data to it, if you only have QStrings then a QStringListModel is premade for you to use
to add a row you can do:
int rows = model->rowCount();
model->addRow(rows,1);
QModelIndex index = model->index(rows,0);
model->setData(index, string);

How can I check if QPushButton is clicked

I want to use an if-else statement to check if QPushButton is clicked or not.
How can I do this?.
The question does not make sense. Being clicked is not a state that you can check; clicking a button is an event. It is important to distinguish between states and events.
You handle a button click event by connecting a slot to the signal QAbstractButton::clicked().
Maybe you mean "How do I check if a button is down?". Being down is a state; you check that state using the method QAbstractButton::isDown().
QAbstractButton, QPushButton's parent class, has a checked property (setChecked/isChecked) if you're trying to determine if the button is depressed with the checkable property set to true.

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.