I have QTableView with data generated from .csv file. The QTableView has column called Date (QDateTime) that covers over a month. I want to select a date and split the data for that date from the QTableView, so I can summarize and average the values in the other columns.
Similar to SQL Table Queries, or to this c# LINQ syntax:
var result = context.data.Where(n => n.date == date);
You can iterate over your table view data and extract the information you need. As a demonstration please look at the given example:
void findDates(QTableView *table, const QDateTime &date)
{
auto model = table->model();
const int dateColumn = 1; // The column with the dates data
for (int row = 0; row < model->rowCount(); ++row)
{
auto idx = model->index(row, dateColumn);
auto data = idx.data();
auto d = data.toDateTime();
// Comparison of dates
if (d > date)
{
// Do something
}
else
{
// Do something else
}
}
}
Related
I have done this in java where I sum the values of the price column/row. But I am wondering how to do this in JavaFX.
I want to sum everything in column 1 and display it in a inputField, I used this to do it in java but how do you do this in JavaFX?
tableview.getValueAt(i, 1).toString();
Here is what I'm trying to do:
int sum = 0;
for (int i = 0; i < tableview.getItems().size(); i++) {
sum = sum + Integer.parseInt(tableview.getValueAt(i, 1).toString());
}
sumTextField.setText(String.valueOf(sum));
If you really have a TableView<Integer>, which seems to be what you are saying in the comments, you can just do
TableView<Integer> table = ... ;
int total = 0 ;
for (Integer value : table.getItems()) {
total = total + value;
}
or, using a Java 8 approach:
int total = table.getItems().stream().summingInt(Integer::intValue);
If you have a more standard set up with an actual model class for your table, then you would need to iterate through the items list and call the appropriate get method on each item, then add the result to the total. E.g. something like
TableView<Item> table = ...;
int total = 0 ;
for (Item item : table.getItems()) {
total = total + item.getPrice();
}
or, again in Java 8 style
int total = table.getItems().stream().summingInt(Item::getPrice);
Both of these assume you have an Item class with a getPrice() method, and the column in question is displaying the price property of each item.
public void totalCalculation (){
double TotalPrice = 0.0;
TotalPrice = Yourtable.getItems().stream().map(
(item) -> item.getMontant()).reduce(TotalPrice, (accumulator, _item) -> accumulator + _item);
TexfieldTotal.setText(String.valueOf(TotalPrice));
}
//getMontant is the getter of your column
So I want to use Google spreadsheet to find how many times does five consecutive cells have a value greater than a given value in a row,but one cell cant be a part of two set of consecutive cells.For example i want to count the number of times a particular item was bought in a month for consecutive five days but if it was bought for 7 days at a stretch it will only be counted as one whereas if it is multiple of five it will be counted as many multiples of five.
For Ex:If cells 1-5 have a value greater than the given value it should give me a count of 1, but if cells 1-9 also are greater than the given value even then it should give me count of 1 but if 1-10 have a value greater than the given value then it should give me a count of 2.I hope this was clear.
I want to write this code in Google Drive using custom function, I tried writing a code in C.
*
int x; //no. of rows
int y; //no. of columns
int arr[x][y]; //array to store numbers
int count[x];
int i,j,k; //for loops
for(i=0;i<x;i++) //set count to 0 for all rows
count[x]=0;
for(i=0;i<x;i++)
{
for(j=0;j<y;j++)
{
for(k=1;k<=5, j<y;k++, j++)
{
if(!arr[i][j]>0)
{
break;
}
else if(k==5 && arr[i][j]!<1)
{
count[i]++;
j--;
}
}
}
}
//display the count array now to see result.
*
You can do this without writing code. That's kinda the purpose of a spreadsheet.
You have one column, say column A, with the values.
In the next column, start a counter that increments each row if the value in the first column is >= your preset value, and reset the counter if not. The formula would be something like (for cell B2)
=IF(A2>=$E$1,B1+1,0)
In the next column, calculate the multiples of 5. For cell C2:
=IF(MOD(B2,5)=0,C1+1,C1)
Copy those cells down to the bottom of the list in column A, and the last value will be the count of values that exceeded cell $e1 a multiple of 5 consecutive times.
Another way using native Sheets functions:
=ArrayFormula((ROWS(A:A)-LEN(REGEXREPLACE(CONCATENATE(LEFT(A:A>=1)),"TTTTT","")))/5)
and using a custom function:
function countGroups(range, comparisonValue, groupSize) {
var v = comparisonValue || 1; // default to comparing to 1 if not specified
var n = groupSize || 5; // default to groups of 5 if not specified
var count = 0, counter = 0;
for (var i = 0, length = range.length; i < length; i++) {
if (range[i][0] >= v) {
counter++;
if (counter == n) {
counter = 0;
count++;
}
}
else
counter = 0;
}
return count;
}
I want to change the structure of my DataTable. My datatable shows data in following format:-
I pivoted my datatable, using following code.
public DataTable PivotTable(DataTable source)
{
DataTable dest = new DataTable("Pivoted" + source.TableName);
dest.Columns.Add(" ");
foreach (DataRow r in source.Rows)
dest.Columns.Add(r[0].ToString());
for (int i = 0; i < source.Columns.Count - 1; i++)
{
dest.Rows.Add(dest.NewRow());
}
for (int r = 0; r < dest.Rows.Count; r++)
{
for (int c = 0; c < dest.Columns.Count; c++)
{
if (c == 0)
dest.Rows[r][0] = source.Columns[r + 1].ColumnName;
else
dest.Rows[r][c] = source.Rows[c - 1][r + 1];
}
}
dest.AcceptChanges();
return dest;
}
After pivoting my Datatable shows record in following format:-
But I need result in following format:-
That is if there are 1 Sector it should show till 1 stop, if there are 3 Sector then it should show till 3 stop. It should increase or decrease automatically.
Please help me with the code.
Thanks
I have a table view that requires the same information on multiple rows however these rows keep appearing empty and the same log message appears
'Ignoring duplicate insertion of item'
Basically I iterate over a model setup to contain all information and take the value at each index to populate another model attached to the table.
I tried to assign each index into a variable each time the loop iterates (which seems like overkill)
QString var1, var2, var3;
for ( int row = 0; row < m_infoModel->rowCount(); ++row )
{
item = new QStandardItem;
var1 = m_infoModel->data( m_infoModel->index( row, 0 ) ).toString();
item->setText( var1 );
m_displayModel->setItem( row, 1, item );
item = new QStandardItem;
var2 = m_infoModel->data( m_infoModel->index( row, 1 ) ).toString();
item->setText( var2 );
m_displayModel->setItem( row, 2, item );
item = new QStandardItem;
var3 = m_infoModel->data( m_infoModel->index( row, 2 ) ).toString();
item->setText( var3 );
m_displayModel->setItem( row, 3, item );
}
Is there a correct/more efficient way of getting around this 'duplicate insertion' or am I looking at it the wrong way?
Thanks
In case anyone stumbles across this as I did. The clue is in Marek R's answer about parents. When you insert an item into a model and that item is already in another model it will cause this issue.
To fix it you need to make a new QStandardItem encapsulating the data from the existing QStandardItem.
Hopefully how I fixed it makes sense.
This was my code experiencing the same issue (copying new rows of text from m_logModel to m_model):
for (int i = first; i <= last; i++)
{
QList<QStandardItem*> nextRow;
for (int j = 0; j < m_logModel->columnCount(); j++)
{
nextRow << m_logModel->item(i, j);
}
m_model->appendRow(nextRow);
}
This changed code makes it work as expected:
for (int i = first; i <= last; i++)
{
QList<QStandardItem*> nextRow;
for (int j = 0; j < m_logModel->columnCount(); j++)
{
nextRow << new QStandardItem(m_logModel->item(i, j)->text());
}
m_model->appendRow(nextRow);
}
Hopefully this will help the next person who finds this issue.
I have created a dojox.grid.datagrid programmatically and I need custom sorting on the columns.
For this I tried using ItemFileWriteStore.comparatorMap['field'] = comparatorFunc. But my comparator function is never called.
Any idea on what I am missing here?
I Have done custom sorting on data grid. (Dojo-1.4)
sample code here:
function(response, ioArgs){
queryGrid.queryOptions={ignoreCase:true};
queryGrid.setStore(new dojo.data.ItemFileReadStore(response[responseResult]));
setCustomSort(queryGrid.store);
..
}
function setCustomSort(store){
if(!store.comparatorMap){
store.comparatorMap = {};
}
store.comparatorMap["unresolvedHrs"] = sortNum;
store.comparatorMap["tat"] = sortNum;
}
function sortNum(a, b){
var _a = convertTimeToNum(a);
var _b = convertTimeToNum(b);
var ret = 0;
if (_a > _b) {
ret = 1;
}
if (_a < _b) {
ret = -1;
}
return ret;
}
Here convertTimeToNum is to convert time format value to number in minutes.