I have a QListWidgetItem and I have a PushButton that when clicked has to remove all the items. I use this:
void Model::removeEverything() {
Container<Avatar*>::iterator startValue = list->begin();
Container<Avatar*>::iterator endValue = list->end();
while(startValue != endValue) {
remove(*startValue);
++startValue;
}
}
void Modello::remove(Avatar* a) {
list->removeElement(a);
}
But removeEverything() doesn't delete everything. It deletes all item except the second. And when I have only one item in the list and I try to use removeEverything() the program crashes.
I don't understand what I'm missing. I hope you can help me. Thank you
Related
I'm trying to get items but I can't get to work with selectedItems(). With the following code, the qDebug returns "()" or crash of course if I use selectedItems.last().
I don't understand what I'm doing wrong, I even added a foreach to be sure items were selected but nothing. The following code comes from my View class.
if (event->button() == Qt::LeftButton) {
foreach(auto item, items(event->pos())) {
item->setSelected(true);
}
qDebug()<< scene->selectedItems();
Ask me if I can provide you further details, I have a lot of code and don't really know what could be concerned by my problem.
Moreover, if I change the foreach with :
foreach(auto item, items(event->pos())) {
scene->removeItem(item);
}
the item WILL BE deleted, so it kinda select the item. I don't get why setSelected(true) then selectedItems() doesn't return me items.
I found out that my item wasn't selectable by default. It's a QGraphicsEllipseItem by the way.
My selectedItems is working with the following flag :
item->setFlag(QGraphicsItem::ItemIsSelectable);
Final code :
foreach(auto item, items(event->pos())) {
item->setFlag(QGraphicsItem::ItemIsSelectable);
item->setSelected(true);
}
Iam using syncfusion SFListview in my xamarin forms app. I implemented multiselect of listview cell from https://help.syncfusion.com/xamarin/sflistview/selection?cs-save-lang=1&cs-lang=xaml.It works fine.But the problem iam facing is everytime we need to hold the itemcell for selection. Is it possible for multiselect that hold only for first cell and tap for all other cell?
Is it possible for multiselect that hold only for first cell and tap for all other cell?
Sure can do that.If you want multiselect items,I guess that next steps will do some tasks about multiselect items.The picture below may look like the one you want.
You can look at the content of this chapter in the share link, and the sample code it provides.
Solution One:(Generally acceptable)
If the project doesn't mind adding a control button outside, then this will be the quickest and easiest way.That is to add a ToolbarItems in the NavigationPage, use it to control whether you can click multiple selections without jumping to the next page.
Add ToolbarItems :
<ContentPage.ToolbarItems>
<ToolbarItem x:Name="ToolbarItemsButton" Text="MultipleSelect" Clicked="Edit_Clicked"/>
</ContentPage.ToolbarItems>
<sync:SfListView x:Name="listView"
SelectionGesture="Hold"
SelectionMode="Multiple"
ItemTapped="ListView_ItemTapped"
SelectionBackgroundColor="Transparent"
IsStickyHeader="True" ItemSize="70">
...
In ContentPage ,add Flag to judge SelectionMode of ListView.
int flag = 0;
private void Edit_Clicked(object sender, EventArgs e)
{
if(0 == flag)
{
listView.SelectionGesture = TouchGesture.Tap;
ToolbarItemsButton.Text = "Done";
flag = 1;
}
else
{
ToolbarItemsButton.Text = "MultipleSelect";
listView.SelectionGesture = TouchGesture.Hold;
flag = 0;
}
}
Can judge when you can switch to the next page.
private void ListView_ItemTapped(object sender, Syncfusion.ListView.XForms.ItemTappedEventArgs e)
{
if(0 == flag)
{
Navigation.PushAsync(new ContentPage());
}
}
Solution Two:(Recommended)
SfListView has a method is ItemHolding.Not using another button also can exchange the SelectionMode.
Xaml code different is adding this method of SfListView.
<sync:SfListView x:Name="listView"
SelectionGesture="Hold"
SelectionMode="Multiple"
ItemTapped="ListView_ItemTapped"
SelectionBackgroundColor="Transparent"
ItemHolding="ListView_ItemHolding" // ItemHolding
IsStickyHeader="True" ItemSize="70">
When OnHolding can do something here:
private void ListView_ItemHolding(object sender, ItemHoldingEventArgs e)
{
if (0 == flag)
{
listView.SelectionGesture = TouchGesture.Tap;
ToolbarItemsButton.Text = "Done";
flag = 1;
}
else
{
listView.SelectionGesture = TouchGesture.Hold;
ToolbarItemsButton.Text = "MultipleSelect";
flag = 0;
}
}
Judge when you can switch to the next page.
private void ListView_ItemTapped(object sender, Syncfusion.ListView.XForms.ItemTappedEventArgs e)
{
if(0 == flag)
{
Navigation.PushAsync(new ContentPage());
}
}
Solution Three:(Not recommended here)
Generally, for the multiple selection of the cell of the listview, we will process the template of the custom cell, such as adding a button in the template. When clicking, the item can be marked as selected, and the UI of the item can also be customized as The style when selected.
I have a QGridLayout, that consists of buttons. Then, after pressing another button (which is not in this QGridLayout), all buttons should be removed from this Grid(e.g. there were 20 buttons) and new quantity of buttons should be added (e.g. there will be 40 buttons). Everything is running Ok, but when I close my App, the "APPCRASH" error appears with exclusion code c0000005. I have searched for solution, but with no results.
This is the code I use for deleting buttons from Grid:
if (layout) {
QLayoutItem *item;
while((item = layout->takeAt(0)) != 0) {
if (item->widget()) {
layout->removeWidget(item->widget());
delete item->widget();
}
delete item;
}
}
I hope, that you will help me to solve this problem.
How to Get Checked items in CheckBox TreeView in JavaFx?
I don't know how to achieve this... You only get the selected items...
ObservableList<TreeItem<String>> items = treeHazardsXmi.getSelectionModel().getSelectedItems();
Assuming you are using CheckBoxTreeItems, you can iterate through the tree and test each one's selected property:
ObservableSet<CheckBoxTreeItem<?>> checkedItems = FXCollections.observableHashSet();
findCheckedItems((CheckBoxTreeItem<?>) tree.getRoot(), checkedItems);
// ...
private void findCheckedItems(CheckBoxTreeItem<?> item, ObservableSet<CheckBoxTreeItem<?>> checkedItems) {
if (item.isSelected()) {
checkedItems.add(item);
}
for (TreeItem<?> child : item.getChildren()) {
findCheckedItems((CheckBoxTreeItem<?>) child, checkedItems);
}
}
If you have a very large tree, for which that is computationally prohibitive, you can maintain a set of checked items, and by observing each item's selectedProperty, keep the set updated at all times. The downside here is that you have to be vigilant to always create the CheckBoxTreeItem so that it updates the set:
private ObservableSet<CheckBoxTreeItem<?>> checkedItems = FXCollections.observableHashSet();
private <T> CheckBoxTreeItem<T> createTreeItem(T value) {
CheckBoxTreeItem<T> item = new CheckBoxTreeItem<>(value);
item.selectedProperty().addListener((obs, wasChecked, isNowChecked) -> {
if (isNowChecked) {
checkedItems.add(item);
} else {
checkedItems.remove(item);
}
});
return item ;
}
Now, as long as your items are created via the createTreeItem() method, the checkedItems set will always contain the checked items.
Note that if your tree is dynamic (you add and remove nodes at runtime), you should go further and deregister the listener if the item is removed from the tree (left as an exercise for the reader...).
you might be able to tell that I'm pretty new to QT...
My program contains a window with several Widgets in a QGridLayout. Some of these Widgets have a layout and child widgets themselves. Pressing the Tab key moves the focus like I expect it to, in the order I created the widgets.
Problems occur when a widget changes it's content (I do that by deleting all child widgets and then constructing new ones.) If I do that, new widgets are moved to the end of the tab chain (that indicates to me, that tab order is kind of global for a window). I have tried to use QWidget::setTabOrder() to reorder all widgets (I tried both, setting tab order for only the contends of the main window and setting it for the children too) but the actual order doesn't change. I did this by emitting a signal when the contend of a widget changes and connecting that to a slot on my main Window.
I think I understand the way the setTabOrder() function should work. I do something similar to this:
QWidget* a = firstWidget;
QWidget* b = secondWidget;
QWidget::setTabOrder(a,b);
for (int i = 0; i < widgets.size(); ++i) {
a = b;
b = widgets[i];
QWidget::setTabOrder(a,b);
}
Is there anything special one has to do when changing the tab order?
I also tried to reimplement focusNextPrevChild(bool next) and focusInEvent(QFocusEvent* e) similar to what can be found at this site. 1
I managed to mess up tab order a lot more like this... is this approach a step in the right direction?
I'm sorry if this is something simple that I managed to miss, but I'm struggling for a while now and I can't find a solution.
Any help is very appreciated.
I had the same problem, and resolved it using the info Tim Meyer provided in the comments.
Tab order is not hierarchical - calling setTabOrder on a parent widget that doesn't accept focus will not cause the focus to be passed to the child. You will need to fetch the appropriate children from the widget and set the order on them
In my case, a dynamically constructed QTreeWidget contained editable widgets, and I needed to reset setTabOrder to account for widgets being created out-of-order.
The following code is (most) of the implementation
// Perform a depth-first gather of the child widgets of this item
void gatherTabWidgets( QObject* item, QWidgetList& tabWidgets )
{
if (item->isWidgetType())
{
QWidget* itemWidget = static_cast<QWidget*>(item);
if (itemWidget->focusPolicy() & Qt::TabFocus)
tabWidgets.push_back( itemWidget );
}
const QObjectList& children = item->children();
for (QObjectList::const_iterator itr = children.begin(); itr != children.end(); itr++)
{
gatherTabWidgets( const_cast<QObject*>(*itr), tabWidgets );
}
}
// Perform a depth-first gather of the child items widgets;
void gatherTabWidgets( QTreeWidgetItem* item, QWidgetList& tabWidgets )
{
QWidget* itemWidget = fetchWidgetForItem( item );
gatherTabWidgets( itemWidget, tabWidgets );
for (int i = 0; i < item->childCount(); i++)
{
gatherTabWidgets( item->child( i ), tabWidgets );
}
}
void VETreeWidget::sortTree()
{
// Ensure ordering is maintained.
sortItems( 0, Qt::AscendingOrder );
// Once the items have been re-ordered, re-create the tab ordering
QTreeWidgetItem* baseItem = topLevelItem( 0 );
QWidgetList tabWidgets;
gatherTabWidgets( baseItem, tabWidgets );
if (!tabWidgets.empty())
{
QWidget* lastWidget = tabWidgets.first();
// Connect this treeview to the first widget. This ensures
// if the treeview is tabbed-to, it will tab to the correct sub-widget
setTabOrder( this, lastWidget );
for (QWidgetList::iterator itr = tabWidgets.begin() + 1; itr != tabWidgets.end(); itr++)
{
setTabOrder( lastWidget, *itr );
lastWidget = *itr;
}
}
}