Get The Row,Column values from a cell in QT - qt

I have a problem, i want to return the selected Rows values, and the columns separately, i found a method to return both of them using the function cell(row, column), but i want to get them separately
Here is my code :
QTableWidgetItem *c = new QTableWidgetItem();
QMap<QString,int> lists;
for(i=0;i<range.rowCount();++i){
for(int j=0;j<range.columnCount();++j){
c=item(i,j);// here i can return the Rows, Columns Data
QMessageBox::information(this,"",c->text());
}
}
As you can see this code is working, but i just want to return the Rows and the Columns separately so i can put them in my QMap<QString,int> list.
And the purpose of all this is to try to draw a piechart from the selected rows and columns
So Any help please

Here is what I understood from the comments, feel free to correct me and I'll update my answer if necessary.
COL1 | COL2
NAME | VALUE
So when you select a cell, you actually care about the whole row, a.k.a the name of the row and the value associated. If this is the case, it would make more sense to only allow the user to select whole rows, instead of cells. setSelectionBehavior(QAbstractItemView::SelectRows); should do the trick.
Provided that the name of the dataset is always in column 1, and the value in column 2, you should update your code with the snippet:
QTableWidgetItem *c; //Deleted memory leak in your code.
QMap<QString,double> myMap; //Don't name it a list if it is explicitly a map.
for(i=0;i<range.rowCount();++i){
QString dataName = item(i,0)->text();
int dataValue;
for(int j=1;j<range.columnCount();++j){
c=item(i,j);// here i can return the Rows, Columns Data
dataValue += c->text().toDouble();
//If you always have 2 columns only, dataValue will be the value you are looking for.
//If you can have more than 2 columns, dataValue will be the sum of all the cells located after the column 0, on the same row.
//Change this depending on how you want to treat those values.
QMessageBox::information(this,dataName,c->text());
}
myMap[dataName]=dataValue;
}
EDIT for QPieSeries, following this example:
QPieSeries *series = new QPieSeries();
QMap<QString,double>::iterator it = myMap.begin();
QMap<QString,double>::iterator end = myMap.end();
for(; it!=end; ++it){
series->append(it->key(), it->value());
}
QPieSlice *slice = series->slices().at(1);
slice->setExploded();
slice->setLabelVisible();
slice->setPen(QPen(Qt::darkGreen, 2));
slice->setBrush(Qt::green);
QChart *chart = new QChart();
chart->addSeries(series);
chart->setTitle("My Data");
chart->legend()->hide();
QChartView *chartView = new QChartView(chart);
chartView->setRenderHint(QPainter::Antialiasing);
/*change with your window here*/
yourWindow.setCentralWidget(chartView);

Related

Merging rows into single using spreadsheet gear while exporting to Excel

I have to merge the rows of excel using spreadsheet gear controls is it possible. Only specific rows of single column
All detail is being included in this screencast
The changes that has been done by me is
DataTable dt = (DataTable)ViewState["dtGrid"]
System.Random rd = new System.Random(DateTime.Now.Millisecond);
int MyValue = rd.Next(1000000, 99999999);
sUniqueName = MyValue.ToString();
// Create a new workbook.
SpreadsheetGear.IWorkbook workbook = SpreadsheetGear.Factory.GetWorkbook();
SpreadsheetGear.IRange cells = workbook.Worksheets[0].Cells;
cells.CopyFromDataTable(dt, SpreadsheetGear.Data.SetDataFlags.None);
cells.Rows[0, 0, 0, 51].Interior.Color = System.Drawing.Color.Navy;
cells.Rows[0, 0, 0, 51].Font.Color = System.Drawing.Color.White;
cells["A:R"].Columns.AutoFit();
string filename = string.Format("{0}-{1}-{2}", "AOMIndoorInventoryReport", DateTime.Now.ToString("MM-dd-yy"), sUniqueName);
Response.Clear();
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition", "attachment; filename=" + filename + ".xls");
workbook.SaveToStream(Response.OutputStream, SpreadsheetGear.FileFormat.Excel8);
Response.End();
What should be added?
You can merge cells by calling IRange.Merge() on the desired cells. Example:
cells["A7:A8"].Merge();
cells[8, 0, 22, 0].Merge();
UPDATE:
You've asked how to dynamically merge a range of cells based on adjacent rows in a column which have the same value. To accomplish this would require looping through each row in this column and comparing each cell's value with the previous value, merging when appropriate as you go down the column.
I am providing some sample code below which demonstrates one way in which you might go about this (there are certainly many other ways). I had to make some assumptions about your underlying data and requirements, so some modification on your end might be needed. Note the use of some handy methods under the IRange interface (see the IRange.Intersect(...) / Subtract(...) / Union(...) methods) which allow you to "interact" two IRanges to create a new third IRange.
...
// Create a new workbook and some local variables for convenience.
SpreadsheetGear.IWorkbook workbook = SpreadsheetGear.Factory.GetWorkbook();
SpreadsheetGear.IWorksheet worksheet = workbook.Worksheets[0];
SpreadsheetGear.IRange cells = worksheet.Cells;
// Copy data from DataTable to worksheet
cells.CopyFromDataTable(dt, SpreadsheetGear.Data.SetDataFlags.None);
// Here I am creating an IRange representing the used part of column a and without
// the header row, which I presume is in Row 1, and should not be included in this
// merging routine.
SpreadsheetGear.IRange usedColA = worksheet.UsedRange.Intersect(
cells["A:A"]).Subtract(cells["1:1"]);
// No point in attempting to merge cells if there's only a single row.
if (usedColA.RowCount > 1)
{
// Some variables to keep track of the content of the "current" and "previous"
// cells as we loop through "usedColA".
string prevCellVal = "";
string curCellVal = "";
// We'll use this variable to keep track of ranges which will need to be merged
// during the routine. Here I seed "mergeRange" with the first cell in usedColA.
SpreadsheetGear.IRange mergeRange = usedColA[0, 0];
// Loop through each cell in the used part of Column A.
foreach (SpreadsheetGear.IRange cell in usedColA)
{
// Get text of current "cell".
curCellVal = cell.Text;
// Your screenshot seems to indicate that you don't care about merging empty
// cells so this routine takes this into account. Either an empty cell or
// mismatched text from the "current" and "previous" cell indicate we should
// merge whatever cells we've accumulated in "mergeRange" and then reset this
// range to look for more ranges to merge further down the column.
if (curCellVal.Length == 0 || curCellVal != prevCellVal)
{
// Only merge if this range consists of more than 1 cell.
if (mergeRange.CellCount > 1)
mergeRange.Merge();
// Reset merged range to the "current" cell.
mergeRange = cell;
prevCellVal = curCellVal;
}
// If the current and previous cells contain the same text, add this "cell"
// to the "mergeRange". Note the use of IRange.Union(...) to combine two
// IRange objects into one.
else if (curCellVal == prevCellVal)
mergeRange = mergeRange.Union(cell);
}
// One last check for any cells to merge at the very end of the column.
if (mergeRange.CellCount > 1)
mergeRange.Merge();
}
...

javafx replacing a row in TableView from a generated string

Im new to Java and JavaFX and Im trying to check it there is a duplicate in a Tableview, and if that is the case I would like to replace it with a new set of data.
So in essence I'm trying to iterate through the data in my TableView and compare it to something. To be more exact I'd like to compre a value of the String on the first column to a new String. I've done some research and I've found that the most common kind of solution for Filtering Data is using a FilteredList but this doesn't return my original set of items.
my current Code looks like this:
#FXML private TableView<STable> TableV;
public void Replace(String s){
ObservableList<STable> getCurrentData;
for(int i = 0; i < getCurrentData.size(); i++){
// Here is where I get Stuck I've tried:
//TableV.getSelectionModel().getSelectedItem().getCajas();
//getCurrentData.get(i)
}
}
Note: The STable is a class that has all the setters and getters for each of the columns, I've also got the CellFactory set up.
Any guidance on how to do this would be great!
Basically you just have to iterate through your data items, and compare the value representing the content of column 1, to your new string. If both values are equal, you update the value in your dataModel:
(I replaced STable with YourData, because I find the name for a dataModel a little confusing)
for (YourData data : tableView.getItems()) {
if (data.getColumOne().equals(textToCompare)) {
data.setColumnOne("newText");
}
}
Or if you want to replace the row:
for (int idx = 0; idx < tableView.getItems().size(); idx++) {
YourData data = tableView.getItems().get(idx);
if (data.getColumnOne().equals(textToCompare)) {
tableView.getItems().set(idx, someOtherData);
return;
}
}

PHPExcel: Can't set index number & last column value as "Date"

i am new to PHPExcel learning from last two days and i am generating one report for the form input data. I have generated dynamically columns in the excel report but not able to set the first column as Index & last column as Date.
My code is:
// setting column names begin
$col = 1;
$row = 0;
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(0, $row, "Index No.");
foreach ($formInfo['fields'] as $fields) {
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $fields['grid-name']);
$col++;
}
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, 'Date & Time of Input');
// setting column names ends!
Thanks in advance for response.
You have achieved the most. I just updated your code. Now it will work like as per you wish.
// Initializing last col variable
$endcolval = 0;
// Storing the value to First Column
$objPHPExcel->setActiveSheetIndex(0)
->setCellValueByColumnAndRow(0, 1, "Index");
// Storing the rest of the values from array to the respect indexes
foreach ($formInfo['fields'] as $col=>$fields)
{
$objPHPExcel->setActiveSheetIndex(0)
->setCellValueByColumnAndRow($col+1, 1, $fields['grid-name']);
// Using the above function you can able dynamically store the values to the cell.
// setCellValueByColumnAndRow(Column_Number, Row_Number, Value_To_Save);
$endcolval = $col+1;// Getting the last column number
}
// Assigning the Date to the Last Column.
$objPHPExcel->setActiveSheetIndex(0)
->setCellValueByColumnAndRow($endcolval+1, 1, "Date");

Extracting max value in GridView column in a Web Performance Test

This pertains to .NET Web Performance Tests.
If I have an ASP.NET page with a GridView that has a column of ints, how do I write an extraction rule to get the largest int in the column?
I tried creating a custom extraction rule by inheriting from ExtractionRule and in the Extract method using e.Response.HtmlDocument.GetFilteredHtmlTags however, the HtmlTags returned don't seem to expose their innerHtml contents.
Perhaps you can write an extraction rule that gets the whole column, then process the numbers to get their maximum value. Alternatively, use a built-in extraction rule to get the whole column, then write a plugin to get the maximum value. In either case your code should expect a mixture of numbers and other text.
Ben Day has a great blog post containing two types that express similar concerns. TableColumnValueValidator and ExtractRandomValueFromTable.
http://www.benday.com/2013/08/19/validation-extraction-rules-for-visual-studio-2012-web-performance-tests/
In the Extract(object, ExtractionEventArgs), you need to parse the ExtractionEventArgs.Response.BodyString. Ben uses the HtmlAgilityPack library for this. http://www.nuget.org/packages/htmlagilitypack
Something like this is roughly the code you'd need. This is simliar logic to ExtractRandomValueFromTable.
This does not account for thead/tbody or cells that span multiple columns/rows.
HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(e.Response.BodyString);
HtmlNode table = doc.GetElementbyId(TableId); // TableId is a test property
HtmlNodeCollection columns = table.SelectNodes("//th");
int columnIndex = FindColumnIndexByName(columns, ColumnName); // ColumnName is a test property
HtmlNodeCollection rows = table.SelectNodes("//tr")
int maxValue = Int32.MinValue;
foreach(HtmlNode row in rows)
{
HtmlNodeCollection cells = row.SelectNodes("./td");
// Todo check for bounds of cells here
HtmlNode cell = cells[columnIndex];
int value = Int32.MinValue;
Int32.TryParse(cell.InnerText.Trim(), out value);
maxValue = Math.Max(value, maxValue);
}
e.WebTest.Context.Add(ContextParameterName, maxValue);
int FindColumnIndexByName(HtmlNodeCollection columns, string columnName)
{
for(int i=0; i<columns.Count; i++)
if (String.Equals(columns[i].InnerText, columnName, StringComparison.OrdinalIgnoreCase))
{
return i;
}
return -1;
}

Is swapping the columns and rows of a flex datagrid possible?

I have a 1 row, many column flex datagrid. I would like to turn the dataGrid on its side, so that the column headers become a single column running down and v.v.
Is there a way to do that in the DataGrid?
Or am I stuck manipulating the data presented to the grid? If so whats your recommendation?
The main idea here is I have an object like:
x=y
b=u
o=p
u=e
w=p
And I'd like a control that is visually similar to that. Currently the datagrid displays the object as:
x b o u w
y u p e p
Which is too horizontal for my case. Thx
I presume that you want to convert your columns in to a single column
this can be done by getting all the columns and put the in array as provide it as a dataprovider.
DataGrid.columns
will return the columns.
and you can do some think like this to create columns.
public function createColumns():Array{
var advancedDataGridColumn:AdvancedDataGridColumn;
var i:int;
var columnsArray:Array = new Array();
for(i=0;i<columns.length;i++){
advancedDataGridColumn=new AdvancedDataGridColumn();
advancedDataGridColumn.headerText=columns[i].dispheader.toString();
advancedDataGridColumn.dataField="#"+columns[i].name.toString();
advancedDataGridColumn.itemRenderer=new ClassFactory(Styler);
if(columns[i].descending!=undefined ){
if(columns[i].descending.toString()=="true")
sortField = new SortField("#"+columns[i].name.toString(),false,true,null);
else
sortField = new SortField("#"+columns[i].name.toString(),false,false,null);
}
if(advancedDataGridColumn.headerText == Constants.price||
advancedDataGridColumn.headerText == Constants.quantity||
advancedDataGridColumn.headerText == Constants.askPrice||
advancedDataGridColumn.headerText == Constants.bidPrice||
advancedDataGridColumn.headerText == Constants.netAmount||
advancedDataGridColumn.headerText == Constants.interestAmount||
advancedDataGridColumn.headerText == Constants.principalAmount||
advancedDataGridColumn.headerText == Constants.accruedInterestAmount){
var currencyFormattor:CurrencyFormatter = new CurrencyFormatter();
currencyFormattor.useThousandsSeparator=true;
currencyFormattor.currencySymbol="";
currencyFormattor.thousandsSeparatorFrom=",";
currencyFormattor.thousandsSeparatorTo=",";
advancedDataGridColumn.formatter=currencyFormattor;
}
columnsArray.push(advancedDataGridColumn);
}
return columnsArray;
}
sorry i just copied the code but i think it will help you.
Set the DataGrid to have only 2 columns and transform the original dataset to an array collection of {propName, propValue}.
Say you have:
var originalDataSet : ArrayCollection;
var dataSet : ArrayCollection;
var columnSet : ArrayCollection;
Once you have the original values, you'll do something like:
dataSet = new ArrayCollection();
for (var i : int; i < originalDataSet.length; i++)
{
dataSet.addItem({name : columnSet.getItemAt(i), value : originalDataSet.getItemAt(i)});
}
myDataGrid.dataProvider = dataSet;//set the data provider of the grid to the transformed data set.
To clarify:
{name : columnSet.getItemAt(i), value : originalDataSet.getItemAt(i)}
This creates a new instance of type Object and assigns the name and value dynamic properties to their respective values. Instead of this you might want to define your own class with bindable properties. Note that the property names are just for this example because I don't know what you're working with actually.
The data grid at that point should have two columns defined by you, with their dataField properties set accordingly. Also, this example assumes columnSet collection contains the "horizontal columns" that you want displayed vertically. If you can obtain these based on the values in the originalDataset, you might not even need columnSet.

Resources