How to get the selected item in a QTreeView? - select

void CatchSelection(QModelIndex item)
{
if(item.row() == -1)
{
return;
}
if (item.parent().row() != -1)
{
QModelIndex idx = model()->index(item.row(), 1, item.parent());
__int64 value = model()->data(idx).toLongLong();
}
}
The above code doesn't work. I'm trying to get data out of a Qt 5.1 QTreeView when the user clicks on an item. Then I'm trying to select the 2nd column of that item (it has a number I need).
What am I missing?

Related

Vaadin Grid always selects last row

I need help debugging the follow issue:
When I click on a Vaadin Grid with 3 rows, it ignores the click on any but the last row and always selects the 3rd row.
Code is as follows:
// declare Grid objects
public final Grid<SoapKPIContainer> soapKPIOverviewGridDisplay;
..
public SoapKPIOverviewView(SoapKPIRepository soapKPIRepository, Navigator navigator,
BpspSoapCheckCommunications bpspSoapCheckCommunications,
UIMessageByLocaleService messageByLocaleService) {
..
this.soapKPIOverviewGridDisplay = new Grid<>(SoapKPIContainer.class);
this.soapKPIOverviewGridDisplay.setColumns();
Grid.Column<SoapKPIContainer, ?> lastAlarmStatusIconColumn = this.soapKPIOverviewGridDisplay.addColumn("lastAlarmStatusIcon", new ImageRenderer<>());
lastAlarmStatusIconColumn.setSortable(false);
lastAlarmStatusIconColumn.setResizable(false);
lastAlarmStatusIconColumn.setWidth(80.0f);
lastAlarmStatusIconColumn.setCaption(messageByLocaleService.getMessage("label.lastalarmstatus"));
Grid.Column<SoapKPIContainer, ?> activationIconColumn = this.soapKPIOverviewGridDisplay.addColumn("activationIcon", new ImageRenderer<>());
activationIconColumn.setSortable(false);
activationIconColumn.setResizable(false);
this.soapKPIOverviewGridDisplay.getDefaultHeaderRow().getCell("lastAlarmStatusIcon").setHtml(messageByLocaleService.getMessage("label.lastalarmstatus"));
this.soapKPIOverviewGridDisplay.getDefaultHeaderRow().getCell("activationIcon").setHtml(messageByLocaleService.getMessage("label.activationicon"));
this.soapKPIOverviewGridDisplay.addColumn(SoapKPIContainer::getKpiName).
setCaption(messageByLocaleService.getMessage("header.kpiName"));
..
this.setSizeFull();
this.soapKPIOverviewGridDisplay.setSizeFull();
this.soapKPIOverviewGridDisplay.setHeight(400, Unit.PIXELS);
this.soapKPIService = new SoapKPIService(soapKPIRepository);
this.soapKPIOverviewGridDisplay.setItems( soapKPIService.toContainer( soapKPIService.findAllSoapKPI() ) );
..
// adding Listener for the Grid to enable for the user to select single KPIs
soapKPIOverviewGridDisplay.setSelectionMode(SelectionMode.SINGLE);
soapKPIOverviewGridDisplay.asSingleSelect().addValueChangeListener(e -> {
log.debug("asSingleSelect().addValueChangeListener " + e.getValue());
if ( e.getValue() != null) {
log.debug("asSingleSelect().addValueChangeListener findSoapKPIById #" + e.getValue().getKpiId());
testKPIDefView.setEnabled(true);
soapKPI = soapKPIService.findSoapKPIById( e.getValue().getKpiId() );
changeEnabled.setVisible(true);
if( soapKPI.getEnabled() == 1)
changeEnabled.setCaption(messageByLocaleService.getMessage("button.disable"));
else
changeEnabled.setCaption(messageByLocaleService.getMessage("button.enable"));
}
else {
testKPIDefView.setEnabled(false);
changeEnabled.setVisible(false);
soapKPI = null;
}
});
..
soapKPIOverviewLayout.addComponent(soapKPIOverviewGridDisplay);
I'm still not sure what line in the code cause it to break, but I found that overriding the equals method of the row object makes the problem go away:
#Override
public boolean equals(Object o) {
if (o == this) return true;
if (!(o instanceof SoapKPIContainer)) {
return false;
}
SoapKPIContainer container = (SoapKPIContainer) o;
return this.getKpiId() == container.getKpiId();
}

Context Menus on Right Click in GtkTreeView (only for specified column)

I want to show a popup menu if 'right click' in a row from GtkTreeView.
It is possible that the popup menu shows up only if 'right click' in first column (or specificate column)?
I have use code but this sown up menu for entire row.
gboolean
on_tree_view_button_pressed(GtkWidget *treeview, GdkEventButton *event, gpointer data)
{
if (event->type == GDK_BUTTON_PRESS && event->button == 3) {
GtkTreePath *path;
selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview));
if (gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(treeview),
event->x, event->y,
&path, NULL, NULL, NULL)) {
gtk_tree_selection_unselect_all(selection);
gtk_tree_selection_select_path(selection, path);
gtk_tree_path_free(path);
}
do_popup_menu(treeview, event, data);
return TRUE;
}
return FALSE;
}
I have GtkTreeView with 3 columns and I want to show up menu only for first column
You are already calling gtk_tree_view_get_path_at_pos. This function can also obtain the treeview column under the mouse. Instead of passing NULL for the column argument, make sure to get the column and compare it to your desired column:
if (event->type == GDK_BUTTON_PRESS && event->button == 3) {
GtkTreePath *path;
GtkTreeViewColumn *column;
selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview));
if (!gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(treeview),
event->x, event->y,
&path, &column, NULL, NULL))
// if we can't find path at pos, we surely don't
// want to pop up the menu
return FALSE;
if (column != gtk_tree_view_get_column(GTK_TREE_VIEW(treeview), 0)) {
// wrong column, don't bother
gtk_tree_path_free(path);
return FALSE;
}
gtk_tree_selection_unselect_all(selection);
gtk_tree_selection_select_path(selection, path);
gtk_tree_path_free(path);
do_popup_menu(treeview, event, data);
return TRUE;
}

How to hide tab pages in a tab control

I am learner in c# and I have a small doubt.
In my windows form page, I have a tabControl contains 3 tabPages.I also have a comboBox outside the tabControl with 3 items listing the names of tab pages.
What i want to do is select the name of the first tabPage from the comboBox, and only that tabPage should be displayed by hiding the other tabPages.
Visibility property is not applicable to tabPages. So how can i do this?
Thanks in advance.
According to Hiding and Showing Tabpages in a Tabcontrol by Debasmit Samal:
Visiblity property has not been implemented on the TabControl, and
there is no Insert method also. Some workaround on this
private void HideTabPage(TabPage tp)
{
if (tabControl1.TabPages.Contains(tp))
tabControl1.TabPages.Remove(tp);
}
private void ShowTabPage(TabPage tp)
{
ShowTabPage(tp, tabControl1.TabPages.Count);
}
private void ShowTabPage(TabPage tp , int index)
{
if (tabControl1.TabPages.Contains(tp)) return;
InsertTabPage(tp, index);
}
private void InsertTabPage(TabPage tabpage, int index)
{
if (index < 0 || index > tabControl1.TabCount)
throw new ArgumentException("Index out of Range.");
tabControl1.TabPages.Add(tabpage);
if (index < tabControl1.TabCount - 1)
do
{
SwapTabPages(tabpage, (tabControl1.TabPages[tabControl1.TabPages.IndexOf(tabpage) - 1]));
}
while (tabControl1.TabPages.IndexOf(tabpage) != index);
tabControl1.SelectedTab = tabpage;
}
private void SwapTabPages(TabPage tp1, TabPage tp2)
{
if (tabControl1.TabPages.Contains(tp1) == false || tabControl1.TabPages.Contains(tp2) == false)
throw new ArgumentException("TabPages must be in the TabControls TabPageCollection.");
int Index1 = tabControl1.TabPages.IndexOf(tp1);
int Index2 = tabControl1.TabPages.IndexOf(tp2);
tabControl1.TabPages[Index1] = tp2;
tabControl1.TabPages[Index2] = tp1;
//Uncomment the following section to overcome bugs in the Compact Framework
//tabControl1.SelectedIndex = tabControl1.SelectedIndex;
//string tp1Text, tp2Text;
//tp1Text = tp1.Text;
//tp2Text = tp2.Text;
//tp1.Text=tp2Text;
//tp2.Text=tp1Text;
}

Header of Sortable CellTable Column not Responsive

I am using GWT 2.5 to create a CellTable with a sortable date column.
My code is as follows:
CellTable<Activity> table = new CellTable<Activity>();
table.setRowStyles(new RowStyles<Activity>() {
#Override
public String getStyleNames(Activity row, int rowIndex) {
return TABLE_ROW_STYLE_NAME;
}
});
// create date column
TextColumn<Activity> dateColumn = new TextColumn<Activity>() {
#Override
public String getValue(Activity a) {
return dateFormat.format(a.getDate());
}
};
dateColumn.setSortable(true);
dateColumn.setDefaultSortAscending(false);
// add column to table
table.addColumn(dateColumn, myConstants.dateColumnHeader());
// attach provider to table
activityProvider.addDataDisplay(table);
// create sort handler
ListHandler<Activity> sortHandler = new ListHandler<Activity>(activityProvider.getList());
sortHandler.setComparator(dateColumn, new Comparator<Activity>() {
#Override
public int compare(Activity a1, Activity a2) {
if (a1 == a2) {
return 0;
}
// compare the date columns
if (a1 != null) {
if (a2 != null) {
long a1Val = a1.getDate().getTime();
long a2Val = a2.getDate().getTime();
if (a1Val == a2Val) {
return 0;
}
else if (a1Val > a2Val) {
return 1;
}
else {
return -1;
}
}
else {
return 1;
}
}
return -1;
}
});
// add sort handler to table
table.addColumnSortHandler(sortHandler);
// add date column to table's sort list
table.getColumnSortList().push(dateColumn);
table.setWidth("100%");
getView().getActivityPanel().add(table);
With this code, the data is displayed in the table and the sorting arrow on the column appears. However, nothing happens when I click on the sortable column's header. The sorting order doesn't change, rows are not rearranged.
Can anyone spot the problem here? This code is almost the same as what they have in Google's own example.
This is what I use:
dateColumn.setSortable(true);
sortHandler.setComparator(dateColumn, new Comparator<ObjectPobject>() {
public int compare(ObjectPobject o1, ObjectPobject o2) {
return o1.getDate().compareTo(o2.getDate());
}
});
It should be
a1.getDate().getTime().compareTo(a2.getDate().getTime())
or
a1.getDate().after(a2.getDate())
This happens because GWT uses JavaScript comparisons, and compareTo does not work for dates.

GWT CellTable getVisibleRange() is not what i except

GWT2.4, CellTable with SimplePager, every page show 5 items(rows), suppose i have 8 items totally, at the first page i can see [1-5], when i press next, it will show [4-8] ( getVisibleRange() is [4-8]), i want it to show [6-8], Is there any way i can achieve this?
Thanks in advance.
Try setting
setRangeLimited(false)
with SimplePager
Or you can also override
#Override
public void setPageStart(int index) {
if (getDisplay() != null) {
Range range = getDisplay().getVisibleRange();
int pageSize = range.getLength();
// Removed the min to show fixed ranges
//if (isRangeLimited && display.isRowCountExact()) {
// index = Math.min(index, display.getRowCount() - pageSize);
//}
index = Math.max(0, index);
if (index != range.getStart()) {
getDisplay().setVisibleRange(index, pageSize);
}
}