How to populate Qtablewidget with editable item and none editable item - qtablewidget

every body
i have a problem in populating Qtablewidget with editable items for the first row , and then
non editable items for the rest of rows her is my implementation so far
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTableWidgetItem>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QTableWidgetItem *item= NULL;
for(int row=0; row < ui->tableWidget->rowCount(); row++)
{
for (int col=0; col< ui->tableWidget->columnCount(); col++)
{
if(row == 1)
{
item = new QTableWidgetItem;
item->setFlags(Qt::ItemIsEditable | Qt::ItemIsEnabled);
ui->tableWidget->setItem(row,col,item);
}else{
item = new QTableWidgetItem;
item->setFlags(Qt::NoItemFlags);
ui->tableWidget->setItem(row,col,item);
}
}
}
}
MainWindow::~MainWindow()
{
delete ui;
}

Also, you need to set number of rows and columns before populating tableWidget:
ui->tableWidget->setRowCount(numberOfRows) and
ui->tableWidget->setColumnCount(numberOfColumnss)
Since you do not know how many rows you'll need (because you want the user to have the ability to edit lines from line to line infinity) I would do it like this:
ui->tableWidget->setRowCount(2);
...add only 2 rows ("line zero and one"), so user will have visible only two lines that he must fill. You'll need mechanism to check if all cells are filled (for example: every time cell item isChanged you check all other items if they are filled - not empty) so only when they are, user will be able to pass to editing new line, which you will add to table dynamically for every next row like this:
rowNumber = ui->tableWidget->rowCount();
ui->tableWidget->insertRow(rowNumber);
for (int col=0; col< ui->tableWidget->columnCount(); col++){
item = new QTableWidgetItem;
item->setFlags(Qt::ItemIsEditable | Qt::ItemIsEnabled);
ui->tableWidget->setItem(rowNumber ,col,item);
}
I hope this helps.

Related

Change value of widget inside GWT flex table when other widget's value changes?

Ok, so I have a pretty specific and to me quite complicated issue, as I'm a GWT newbie.
I have a GWT flex table, which I use to dynamically add rows, whose cells contain GWT widgets. The row number changes, but the number of columns in static, always 6. Each row contains a cell with a remove button and five cells each with their own textbox.
What I need to do is somehow code a kind of relationship between the textbox in cell 6 of one row and the textbox in cell 5 in the next row (and vice versa).
To illustrate: when something changes in the textbox at [1,6] the content of textbox at [2,5] needs to be overwritten with the same value. If the textbox at [2,5] changes the textbox at [1,6] needs to change as well. I cannot use a button to commit the changes, it needs to happen via onValueChange or Blur or something similar, which doesn't require the user to perform a specific action.
My problem stems mostly from trying to figure out how to address specific cells in the flex table and their content. For the remove button the solution was easy enough with a click event handler, but for this issue I just can't seem to be able to come up with a solution.
Sadly I also cannot provide any of the code which I have up until now, since it's a business secret. I can only give a broad description of the problem like the one above.
EDIT:
Actually, it's probably more a problem of not having much code in terms of this specific problem.
What I have is a flex table, which has initially only the header row. Upon clicking a button below this table the addNewField() method is called, which just contains the creation, setting of default values and adding of the text fields into a new row.
addNewField() {
int rows = flextable.getRowCount();
Button removeBtn = new Button("x");
removeBtn.getElement().setId(Integer.toString(rows));
//then the button's event handler
TextBox name = new TextBox();
name.setText("something");
flextable.setWidget(rows, 0, "name");
//repeat 4 more times with incrementing columns for the other widgets
}
This way I add entire rows of editable TextBoxes. What I need is a way to influence the values of the 6th column TextBox of a chosen row and the 5th column TextBox of chosen row + 1.
EDIT2: I've tried the dirty option just to see how it would go and somehow the compare inside the if breaks the app. The compiler detects a nullpointerexception and I can't even debug it with breakpoints because it fails to compile and won't start. I can't figure out why though. I threw the code directly into the event for testing purposes, so pardon the ugliness.
TextBox bis = new TextBox();
bis.setText(rows + ":10:00");
subs.setWidget(rows, 5, bis);
bis.addValueChangeHandler(new ValueChangeHandler<String>()
{
#Override
public void onValueChange(ValueChangeEvent<String> event)
{
allRows: for (int i = 0; i < subs.getRowCount(); i++)
{
for (int j = 0; j < subs.getCellCount(i); j++)
{
if ( subs.getWidget(i, j) == bis )
{
TextBox widgetAtColumnSix = ((TextBox) subs.getWidget(i, 5));
String text = widgetAtColumnSix.getText();
TextBox widgetAtColumnFiveRowPlusOne = ((TextBox) subs.getWidget(i + 1, 4));
widgetAtColumnFiveRowPlusOne.setText(text);
break allRows;
}
}
}
}
});
EDIT: Since you edited your question and you dont want to use EventBus you could iterate over your FlexTable and set your TextBox value depending on your current rowIndex and cellIndex... Its not nice but it should work:
public class CellWidget extends Composite {
private TextBox nameBox;
public CellWidget() {
FlowPanel flowPanel = new FlowPanel();
Button deleteButton = new Button("x");
nameBox = new TextBox();
nameBox.addValueChangeHandler(new ValueChangeHandler<String>() {
#Override
public void onValueChange(ValueChangeEvent<String> event) {
notifiyTextBox(CellWidget.this, event.getValue());
}
});
flowPanel.add(nameBox);
flowPanel.add(deleteButton);
initWidget(flowPanel);
}
public void setText(String text) {
nameBox.setText(text);
}
}
public void notifiyTextBox(CellWidget source, String string) {
rows: for (int i = 0; i < flextable.getRowCount(); i++) {
columns: for (int j = 0; j < flextable.getCellCount(i); j++) {
if (flextable.getWidget(i, j) == source) {
CellWidget widgetAtColumnSix = ((CellWidget) flextable.getWidget(i, 5));
widgetAtColumnSix.setText(string);
CellWidget widgetAtColumnFiveRowPlusOne = ((CellWidget) flextable.getWidget(i + 1, 4));
widgetAtColumnFiveRowPlusOne.setText(string);
break rows;
}
}
}
}
I still would recommend using an eventbus. To make it even more convenient there is the GWT Event Binder lib, which makes using events a breeze.
So when you change a value in your textbox[2,5] it also fires your CustomEvent. All Widgets, that need to change their textbox value just need to catch...

Eclipse Scout Neon Focus inside rows after import data

I have some dependencies hierarchy on my form, so I implemented hierarchy check on server side of the scout. If one field is changed, it triggered check if other need to be changed as well. This is done with export/import form data.
MyFormData input = new MyFormData();
FormDataUtility.exportFormData(this, input);
input = BEANS.get(IMYService.class).validate(input, field);
FormDataUtility.importFormFieldData(this, input, false, null, null);
validate function change all other fields that need to be changed.
My problem is with editing cells in editable tables.
If I change value in cell, and this chain validation is triggered, after importing form data I lose focus in cell. So instead, tab will move me to another cell, tab trigger import and focus in cells are lost. And this is a really bad user experience.
How to fix this?
How to stay in focus (of next cell) after import has been called?
Marko
I am not sure, if this applies for you, but you can try the following:
I assume, that you do your export/validate/import logic in the execCompleteEdit(ITableRow row, IFormField editingField) of your column class. I suggest, that you calculate your next focusable cell by yourself and request its focus after importing the form data.
As an example, you can do this like that:
#Override
protected void execCompleteEdit(ITableRow row, IFormField editingField) {
super.execCompleteEdit(row, editingField);
// create form data object
// export form data
// call service and validate
// import form data
// request focus for next cell
focusNextAvailableCell(this, row);
}
with focusNextAvailableCell(this, row) as following:
private void focusNextAvailableCell(IColumn<?> col, ITableRow row) {
if (col == null || row == null) {
return;
}
IColumn<?> nextColumn = getColumnSet().getColumn(col.getColumnIndex()+1);
ITableRow nextRow = getTable().getRow(row.getRowIndex());
if (nextColumn == null) {
// no next column (last column lost focus)
// check if next row is available
nextRow = getTable().getRow(row.getRowIndex()+1);
// maybe select first cell again?
if (nextRow == null) {
nextColumn = getColumnSet().getColumn(0);
nextRow = getTable().getRow(0);
}
}
if (nextColumn != null && nextRow != null) {
getTable().requestFocusInCell(nextColumn, nextRow);
}
}
You should be aware, that you have to call this after every form data import in your execCompleteEdit method of your column. Also this is triggered not only when switching cells through pressing the tab key, but also when clicking with the mouse button.
Best regards!

GWT Cell Table Column Sort Handler

Im using a GWT Cell Table with sorting enabled on the columns. I am also providing an option of filtering the column. This is roughly how the header looks.
----ColumnName (Image) -------
When the user clicks on the image, i start off a filtering process. I achieved this using the Cell Table's addCellPreviewHandler. The issue I am facing is , when the image is clicked , the column sorts itself too. Can anyone tell me how to prevent this from happening ? To be clearer, I want the sort to be called when the user clicks anywhere in the header but the image.
Thanks.
breakDownCellTable.addColumn(indexedColumn, columnHeader);
final int currentColumnIndex = columnIndex;
// Add a ColumnSortEvent.ListHandler to connect sorting to the
// java.util.List.
ListHandler<List<GwtContributorCellData>> columnSortHandler = new ListHandler<List<GwtContributorCellData>>(contributorList);
columnSortHandler.setComparator(indexedColumn, new Comparator<List<CustomClass>>() {
public int compare(List<CustomClass> o1, List<CustomClass> o2) {
if (o1 == o2) {
return 0;
}
// Compare the columns.
if (o1 != null) {
return (o2 != null) ? o1.get(currentColumnIndex).compareTo(o2.get(currentColumnIndex)) : 1;
}
return -1;
}
});
breakDownCellTable.addColumnSortHandler(columnSortHandler);
and in the onBrowserEvent method:
if("click".equals(event.getType())){
EventTarget eventTarget = event.getEventTarget();
if(eventTarget.toString().contains("img") && !eventTarget.toString().contains("<th")){
//event.stopPropagation();
// Window.alert("here");
//breakDownCellTable.fireEvent()
ColumnSortEvent.fire(breakDownCellTable, breakDownCellTable.getColumnSortList());
Your can implement a subclass of the cell you're using and override the onBrowserEvent() method.
This post explains how to find out which DOM element is the exact source of the event.
Once you know that the image has been clicked or not, fire the filtering / sorting event accordingly.

SWT - TableViewer - Refreshing Selection

I have a button that runs a method. The method gets the selected Rows in the table and adds them to an arraylist. This works well the first time executed. But if the user selected the wrong row, they will be able to re select a different row and add that selection data to the arraylist.
But with my current the code, it does not matter what row the user selects the second time the first selected data is always added to the arraylist. It is like the selection needs to be reset or refreshed before the user selects the new row.
Button Code
Button pdfButton = new Button(composite, SWT.PUSH);
pdfButton.setText("Get Plotter List");
pdfButton.setEnabled(true);
pdfButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
getPlotterSelection();
}
});
Method Code
public void getPlotterSelection() {
selectedPlotters.clear(); <-- Clearing the ArrayList
int[] row = viewer.getTable().getSelectionIndices(); <-- Getting Current Selections
Arrays.sort(row);
if (row.length > 0) {
for(int i = row.length-1; i >= 0; i--){
PrinterProfile pp = new PrinterProfile(aa.get(i).getPrinterName(), aa.get(i).getProfileName());
selectedPlotters.add(pp);
}
}
viewer.getTable().deselectAll();
}
As I am writing this, I think maybe the problem is in the getSelectionIndices(). It seems to be getting the number of rows selected, but not the actual row number
Edit
The problem was in my logic. I was getting the correct Indices but using i varaible in the for loop to get the value.
for(int i = row.length-1; i >= 0; i--){
PrinterProfile pp = new PrinterProfile(aa.get(i).getPrinterName(), aa.get(i).getProfileName());
changed it to
aa.get(row[i].getPrinterName(), etc...
and it works like I thought it would
Since you are already using a TableViewer, why not get the selection from it?
IStructuredSelection selection = (IStructuredSelection) viewer.getSelection();
YourObject[] array = (YourObject[])selection.toArray();
Then you can iterate over the array and add them to your ArrayList.

Scrolling SWT Table programmatically

How to do vertical scroll of SWT table programatically?
I'm implementing search function on the table. When an item was found then it will be scrolled to the item found.
There are several methods you might want to try:
Table.showItem(TableItem)
Table.showSelection()
Table.showColumn(TableColumn)
Table.setTopIndex(int)
Other than that, I suggest using a TableViewer from JFace. Then you'd scroll to an item with this method:
TableViewer.reveal(Object)
My full time job is to develop SWT (on Linux), I hope to be able to provide a comprehensive answer:
From a SWT code point of view (at least on on GTK), there are only 3 Table functions that affect scrolling via an internal native call gtk_tree_view_scroll_to_*()
setTopIndex();
showSelection();
showColumn();
To explain what they do & how to use them:
Vertical Scrolling
This is done by setting focus or selecting a particular table item.
For Table:
setTopIndex(int) // Btw for Tree it's setTopItem(..)
showSelection() // which is also reached via setSelection().
setTopIndex(int) moves the view programatically to the desired position.
Below is a modified version of [Snippet52][1] that performs the desired job:
public static void main (String [] args) {
Display display = new Display ();
Shell shell = new Shell (display);
Table table = new Table (shell, SWT.BORDER | SWT.MULTI);
Rectangle clientArea = shell.getClientArea ();
table.setBounds (clientArea.x, clientArea.y, 200, 200);
for (int i=0; i<128; i++) {
TableItem item = new TableItem (table, SWT.NONE);
item.setText ("Item " + i);
}
table.setTopIndex(95); // <<<< This is the interesting line.
shell.pack ();
shell.open ();
while (!shell.isDisposed ()) {
if (!display.readAndDispatch ()) display.sleep ();
}
}
showSelection() on the other hand scrolls the view to the currently selected item. This method is also called by various setSelection(..) methods.
I.e setSelection(..) is typically used to scroll to the desired item and set keyboard focus on it. This is useful if you search for an item in a tree and would like user input (e.g 'enter') to act upon the item that you found. Snippet52 (mentioned above) performs this task.
Now it's worth noting that setSelection(..) doesn't trigger selectionListeners(...), so calling this method wouldn't invoke the associated action.
Horizontal Scrolling
This is done by focusing on a particular column via 'showColumn()'.
Below is a sample snippet that creates some rows & columns and then
scrolls to the last column.
public static void main (String [] args) {
Display display = new Display ();
Shell shell = new Shell (display);
Table table = new Table (shell, SWT.BORDER | SWT.MULTI);
table.setHeaderVisible (true);
Rectangle clientArea = shell.getClientArea ();
table.setBounds (clientArea.x, clientArea.y, 100, 100);
String[] titles = {"First", "Second", "Third", "Fourth", "Fifth"};
for (int i=0; i<titles.length; i++) {
TableColumn column = new TableColumn (table, SWT.NONE);
column.setText (titles [i]);
}
for (int i=0; i<128; i++) {
TableItem item = new TableItem (table, SWT.NONE);
item.setText (new String [] {"" + i, ""+i, ""+i, ""+i});
}
for (int i=0; i<titles.length; i++) {
table.getColumn (i).pack ();
}
shell.pack ();
shell.open ();
display.asyncExec(
// Sometimes table column sizes are computed later at runtime,
// to get around it, set the column index after initialization.
() -> table.showColumn(table.getColumn(4))
);
while (!shell.isDisposed ()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
}
Note on Trees and Lists
Internally in SWT, Tree/Table/List all use the same native 'Tree' widget.
The above examples can be used for Lists and Tables as well, with the difference:
in Tree, setTopIndex(..) is setTopItem(..).
Lists don't have columns, so showColumn() is not applicable.
Let me know if you have further questions.
I don't really know what you need the search for, but you might also consider filtering the table to get to your desired element(kind of like a quick search).
Check it out:
http://eclipsesource.com/blogs/2012/10/26/filtering-tables-in-swtjface/
Hope it helps, cheers!