How to get number of used columns in a row in QXlsx - c++

I have an excel file that I'm trying to parse in Qt Framework using QXlsx library. I'm Stuck on calculating the number of last Used column in a row, because each row has a different number of used columns. see the picture below as an example.
I have already tried the following code
QXlsx::Document doc("data.xlsx");
int lastColumn = doc.dimention().columnCount();
But this line will return the number of last used column in the excel file
in my example it will be column 'F' which will be 6 for all rows which is incorrect.
Any hint will be appreciated
Thanks in advance.

So after searching and asking for help the way to get the last column in QXlsx library as suggested in this replay is to check for non-empty cell in reverse in each row and break the loop if i found the last non-empty cell. As following
QXlsx::Document doc("data.xlsx");
QVariant cell;
int number;
int lastColumn = doc.dimention().columnCount();
for(int column = lastColumn; 0!=column; column--){
cell = doc.read(row,column);
if(cell.toString()!=""){
number = column;
break;
}
}

Related

QTableWidget::resizeRowsToContents() does not work for rows with column spans

I develop a project that uses QTableWidget. Below is a small test fragment of code reproducing the problem.
auto tw = new QTableWidget(3,3);
tw->setItem(0, 0, new QTableWidgetItem("test123 test123 test123 test123 test123 test123 test123"));
for (int i = 1; i < 3; ++i)
for (int j = 0; j < 3; ++j)
tw->setItem(i, j, new QTableWidgetItem("test123"));
tw->setSpan(0,0,1,3);
tw->show();
tw->resizeRowsToContents();
There is a table with 3 rows and 3 columns. The first cell of the table contains a long text. I create a column span at this cell to fit the text and avoid changing other columns width.
The issue is occured when I try to resize rows to their contents. All rows except the first one is resized without any problems - they take up minimal space. The first one gets much more higher that it was before (I expected it to have the same height as the other rows). It seems like QTableWidget::resizeRowsToContents() does not respect column span at all so it increases the row height to fit cell contents.
Could you please tell me how to fix this problem and force rows with column span be resized correctly?

Why is the time complexity of this problem only consider the previous recursive call and not the entire problem?

Here we have a box that is 4 * 7 and it can be filled with rectangles that are either 1 * 2 or 2 * 1. This depiction is from the book Competitive Programmer's Handbook.
To solve this problem most efficiently, the book mentions using the parts that can be in a particular row:
Since there are 4 things in this set, the maximum unique rows we can have is 4^m, where m is the number of columns. From each constructed row, we construct the next row such that it is valid. Valid means we cannot have vertical fragments out of order. Only if all vertical "caps" in the top row correspond to vertical "cups" in the bottom row and vice versa is the solution valid. (Obviously for the horizontal fragments, their construction is restricted in row creation itself, so it is not possible for there to be inter-row discrepancy here.)
The book then says this:
Since a row consists of m characters and there are four choices for
each character, the number of distinct rows is at most 4^m. Thus, the
time complexity of the solution is O(n4^{2m}) because we can go through
the O(4^m) possible states for each row, and for each state, there are
O(4^m) possible states for the previous row.
Everything is fine until the last phrase, "there are O(4^m) possible states for the previous row." Why do we only consider the previous row? There are more rows, and this time complexity should consider the entire problem, not just the previous row, right?
Here is my ad hoc C++ implementation for 2 by n matrix, which would not work in this case, but I was trying to abstract it:
int ways[251];
int f(int n){
if (ways[n] != 1) return ways[n];
return (ways[n] = f(n-1) + f(n-2));
}
int main(){
ways[0] = 1;
ways[1] = 1;
for (int i = 2; i <= 250; i++){
ways[i] = -1;
cout << f(250) << '\n';
}
}

Accessing specific column / row and compare to another (C++)

I'm attempting to take a text file as an input with, let's say, six columns and twenty rows and make various calculations based on the data in the text file.
Is there a way to be able to access a specific column/row in the code and compare it to another? I'm basically trying to see how many numbers in, let's say, column two are +10 away from each other so if column two was 10 11 16 20 21 25 30 31 34 40 50, the program would give me the solution 10,20,30,40,50 and 11,21,31.
It sounds like you may want to utilize this functionality to do more than just figure out if numbers in a row are a set distance from eachother, so I'll provide a more generalized solution.
First create a 20x6 matrix of character pointers:
char *inputmatrix[20][6];
Then load up the matrix with the values from the file. We first get the whole line from the file with fgets, from there we need to parse the line based on spaces using strtok. From there we'll need to create space for each element using malloc, copy in the value from strtok (because it gets overridden on the next call to strtok), and then store the pointer in our array:
char buffer[256];
char *value;
while(!feof(f)){
if(!fgets(buffer,256,f))
break;
value = strtok(buffer," ");
while(value != NULL){
inputmatrix[currow][curcol] = (char*)malloc(strlen(value+1));
memset(inputmatrix[currow][curcol],0,strlen(value+1));
memcpy(inputmatrix[currow][curcol],value,strlen(value));
curcol++;
value = strtok(NULL," ");
}
currow++;
curcol = 0;
}
Now that we've got a matrix of strings, we can go through and run any algorithm you want. For instance, to find out all the elements in a column that are +10 away from eachother we'll have to first determine if the element can be converted to an int using atoi, then compare it with the next int in the column and so on:
int curelement = -1, nextelement = -1;
for(int i=0;i<3;i++){
for(int j=0;j<6;j++){
if((nextelement = atoi(inputmatrix[i][j])) != 0){
if(nextelement - curelement == 10){
printf("row %i,: %i,%i\n",i,curelement,nextelement);
}
curelement = nextelement;
}
}
The above algorithm only works if the integers in the row are in ascending order; if not you have to take each integer and compare it with the rest of the integers in the row.

How to find the Range of Cell in excel - Qt?

I have cell's value as row and column.
Cells(1,10); //1 is row and 10 is column
Now i want to identify the Range of of that cell location.
J1; //where j is column and 1 is row
Is there any possibility to find out in QT?
Using the object model
Cells(5, 3).address(true,true,xlA1)
would return "$C$5"

Finding how many rows needed given number of buttons per row?

I want to layout X buttons.
At the start, Y items can be in a row.
After the first row is laid out, only Y - 1 items can appear in the next row and so on.
So say I have 13 buttons and the first row can have up to 6 buttons, I would need 3 rows. The first would have 6 buttons the second 5 buttons, and the 3ed 2 buttons.
Thanks
What algorithm could be to do:
int getRowCount(int startCols, int numItems);
I know how to do it with MOD if the number of columns is constant but how would you do it if the maximum number of columns decreases with each row?
In situations like this, I try to translate the english into code.
int getRowCount(int startCols, int numItems) {
int currentCols = startCols;
int numRows = 0;
while (numItems > 0) { // as long as items remain
numRows += 1; // add another row
numItems -= currentCols; // reduce the remaining items by the current number of columns
currentCols--; // reduce the number of columns by one
}
}
It's always best to run through the scenario with some edge cases. Ask yourself questions like:
What answer do I get if numItems is 0?
What answer do I get if startCols is 0?
What answer do I get if numItems == startCols?