I have a QTableWidget with some columns and rows and want to add a filter for a particular column.
For that, I've added a QLineEdit in my Window.
I'm already able to filter the rows, when I add only one number in the QLineEdit:
for(int i=0; i<tableWidget->rowCount(); i++)
{
if(!tableWidget->item(i, column)->text().contains(lineEdit->text()))
{
tableWidget->hideRow(i);
}
}
(The slot is connected to the textEdited-Signal of the LineEdit)
What I want to do now:
When I write something like this in the QLineEdit: 10-30; Hide all rows, which doesnt have the number between 10 and 30 (>=10; <=30).
Somebody has an idea, how I can solve this?
This is my decision.
Check if lineEdit text contains two numbers.
QString test = ui->lineEdit->text();
QStringList lst = test.split('-');
if (lst.size() == 2)
Transform they in to integers.
int low = QString(lst[0]).toInt(), high = QString(lst[1]).toInt();
Now let's go to tableWidget and drop in less than or equal to these two numbers.
for (int i = 1; i <= 100; i++) {
int row = ui->tableWidget->rowCount();
ui->tableWidget->insertRow(row);
ui->tableWidget->setItem(row, 0, new QTableWidgetItem(QString::number(i)));
}
connect(ui->lineEdit, &QLineEdit::textChanged, this, [=](const QString &test) {
QStringList lst = test.split('-');
if (lst.size() == 2) {
int low = QString(lst[0]).toInt(), high = QString(lst[1]).toInt();
for (int i = 0; i < ui->tableWidget->rowCount(); i++) {
int temp = ui->tableWidget->item(i, 0)->text().toInt();
if (temp < low || temp > high) {
ui->tableWidget->hideRow(i);
} else {
ui->tableWidget->showRow(i);
}
}
}
});
Related
I have a problem when I'm trying to a loop in a DataTable that a dataset contains.
I'm doing a loop like this:
for(int i = 0; i<ds.Tables[0].Rows.Count - 1 ; i++)
The problem is that I can't get the value of the last line with this one, but if I try to get rid of the "-1" and do a loop on the whole table, I'll have an out of range exception.
This out of range exception is because I have to check if the value of a line "i" is equal to the value of a line "i+1", like this:
if (ds.Tables[0].Rows[i]["Release_No"] != ds.Tables[0].Rows[i + 1]["Release_No"])
So if I do it in a loop, when the index is on the last line it will check if the last line is equal to i+1, and it's out of the table.
So I was trying to check if the index is on the last line, then just get the value of the last line, but it seems like it doesn't work.
if(ds.Tables[0].Rows.IndexOf(ds.Tables[0].Rows[i]) == ds.Tables[0].Rows.Count)
If anyone has an idea, let me know, and of course if it is not clear enough let me know, I'll give more information and more code.
Thanks for your help and your time!
Check if it's the last record, first.
I like to refactor code to read as close to sentence form as possible, explaining what you want it to do using named variables and methods, and that often gets me unlocked.
Try to make each line of code do one thing, and one thing only, like check if it is the last row:
var data = ds.Tables[0].Rows;
var lastRow = data.Count - 1;
for(int i = 0; i < lastRow ; i++)
{
if (i == lastRow){
// This is the last row. Handle the last row here.
}
else
{
// Handle all other rows here
var currentRecord = data[i];
var nextRecord = data[i + 1];
if (currentRecord["Release_No"] != nextRecord["Release_No"])
{
// Handle unique Releases...
}
}
}
Use less than or equal to like this
for(int i = 0; i<=ds.Tables[0].Rows.Count - 1 ; i++)
I hope this may get what you want.
Something like this is better ?
var lastRow = data.Count - 1;
var data = ds.Tables[0].Rows;
for(int i = 0; i< lastRow; i++)
{
testFirstCum = Convert.ToInt32(ds.Tables[0].Rows[i]["EDI_Accum_Quantity"]);
if ( i == lastRow)
{
if (DBNull.Value.Equals(data[i]))
{
quantity = 0;
}
else
{
quantity = Convert.ToInt32(data[i]);
testFirstCum = testFirstCum + quantity;
System.Diagnostics.Debug.WriteLine(quantity);
System.Diagnostics.Debug.WriteLine(testFirstCum);
}
}
else
{
var col = ds.Tables[0].Columns;
var currentRecord = data[i];
var nextRecord = data[i + 1];
if(currentRecord["Release_No"] != nextRecord["Release_No"])
{
for (int j = col[2].Ordinal; j < col.Count; j++)
{
if (DBNull.Value.Equals(data[i][j]))
{
quantity = 0;
}
else
{
quantity = Convert.ToInt32(data[i][j]);
testFirstCum = testFirstCum + quantity;
System.Diagnostics.Debug.WriteLine(quantity);
System.Diagnostics.Debug.WriteLine(testFirstCum);
}
}
}
}
}
In javaFx, treetableView, we can hide or show columns using "+" i.e
setTableMenuButtonVisible(true) symbol
say I have 10 columns in treetableview, but i have shown only 5, How can my program get count of only those columns which are visible (i.e 5 in this case)
U can have something like
ObservableList<TableColumn> visibleColumnList =FXCollections.observableArrayList();
ObservableList<TableColumn > tableColumnList = tableView.getColumns();
for (int j = 0; j < tableColumnList.size(); j++) {
TableColumn tableCol = tableColumnList.get(j);
if (tableCol.isVisible())
visibleColumnList.add(tableCol);
}
Long count = visibleColumnList.size();
Thanks Dev for your answer, though tableCol.isVisible method doesnt works for me, but i got it done other way round.
int count=0;
for (int j = 0; j < ltpSystemViewer.getTable().getColumnCount(); j++) {
TableColumn tableCol = ltpSystemViewer.getTable().getColumn(j);
if (tableCol.getWidth()>0)
count++;
}
return count;
I'm actually working on a personal "Excel" for school.
When the value of my cell is a number (int), I want to add it in my listNumber (QList int). When the value of my cell is a String, I want to add it my listString.
These two lists then allow me to sort.
The problem is here :
QString test = text(i, j);
test.toInt(&ok);
if (ok == true) {
listNumber.append(test.toInt());
qSort(listNumber.begin(), listNumber.end());
}
ERROR ASSERT failure in QList<T>::at: "index out of range" .
I think it's because it wants to "insert" a string in a list of integer.
Here my function "sort"
QList<QString> listString;
QList<int> listNumber;
bool ok;
QTableWidgetSelectionRange range = selectedRange();
for (int j = range.leftColumn(); j <= range.rightColumn(); ++j) {
for (int i = range.topRow(); i <= range.bottomRow(); ++i) {
QString test = text(i, j);
test.toInt(&ok);
if (ok == true) {
listNumber.append(test.toInt());
qSort(listNumber.begin(), listNumber.end());
}
}
}
if (listNumber.count() == 0) {
QMessageBox test;
test.setText("liste vide");
test.exec();
}
else {
int x = 0;
for (int j = range.leftColumn(); j <= range.rightColumn(); ++j) {
for (int i = range.topRow(); i <= range.bottomRow(); ++i) {
Spreadsheet::setFormula(i, j, QString::number(listNumber.at(x)));
x++;
}
}
}
Thank you a lot for your help.
First of all, qSort in Qt is deprecated and it is recommended not to use it:
QT_DEPRECATED_X("Use std::sort") inline void qSort(...
You can use std::sort instead:
#include <algorithm>
//...
std::sort(listNumber.begin(), listNumber.end(), std::less<int>());
//or simply:
std::sort(listNumber.begin(), listNumber.end()); // using default comparison (operator <)
(But also you can simply call deprecated qSort:)
qSort(listNumber);
I have a 2d array of Labels, each label contains a String version of an int. I'm attempting to traverse my array and check to see if each element contains the same int. Am I going about this the wrong way?
public boolean isPattern(Label[][] matirx) {
boolean pattern = false;
for (int rPos = 0; rPos < rows; rPos++) {
for (int cPos = 0; cPos < columns - 3; cPos++) {
if (matrix[cPos][rPos].equals(matrix[cPos + 1][rPos])
&& matrix[cPos][rPos].equals(matrix[cPos + 2][rPos])
&& matrix[cPos][rPos].equals(matrix[cPos + 3][rPos])) {
pattern = true;
}
}
}
When I print my boolean to the output window, I get false no matter what. So I guess my real question is. Am I comparing the contents of each Label or the value at each index, thus causing the variable never to be true?
So I ended up using this solution:
for (int rPos = 0; rPos < rows - 3; rPos++) {
for (int cPos = 0; cPos < columns; cPos++) {
if (matrix[cPos][rPos].getText().equals(matrix[cPos][rPos + 1].getText())
&& matrix[cPos][rPos].getText().equals(matrix[cPos][rPos + 2].getText())
&& matrix[cPos][rPos].getText().equals(matrix[cPos][rPos + 3].getText())) {
pattern = true;
Works as needed. If anyone comes across this and knows a better way, please do share.
I made dynamic vector class..
But the problem show when main function is looping on and on,
my2dArr's row size is increasing when the function is looping
When data is coming on looping, i want to copy new data..
void main()
{
int data[450];
DynamicArray<int> my2dArr(36, 100);
for(int i = 0;i < 36;++i)
{
for(int j = 1;j < 16;++j)
{
my2dArr[i][j-1] = data[i];
}
}
}
// vector class
class DynamicArray
{
public:
DynamicArray(){};
DynamicArray(int rows, int cols): dArray(rows, vector<T>(cols)){}
vector<T> & operator[](int i)
{
return dArray[i];
}
const vector<T> & operator[] (int i) const
{
return dArray[i];
}
void resize(int rows, int cols)//resize the two dimentional array .
{
dArray.resize(rows);
for(int i = 0;i < rows;++i) dArray[i].resize(cols);
}
void clearCOL()
{
for(int i = 0;i < dArray.size();i++)
{
for(int j = 0;j < dArray[i].size();++j)
{
dArray[j].erase();
}
}
}
private:
vector<vector<T> > dArray;
};
The nested for loop should be fine at Initializing your array, but you'd need to put values into the data array to use it in initializing.
If you're only initializing the data once you might consider a third constructor overload that takes in an int[], like so:
DynamicArray( int rows, int cols, T array[] ): dArray( rows, vector< T >( cols ) )
{
for( int i = 0; i < rows; i++ )
{
for( int j = 0; j < cols; j++ )
{
dArray[i][j] = array[i * rows + j];
}
}
}
You'd need to make sure the array was the size you specified. In your example you pass a 450 int array in to initialize a 3,600 int DynamicArray. In you're example you're actually reading illegal data cause you go to the 16th column of each of the 36 rows so you're actually reading 576 elements from a 450 int array. I suppose the array is uninitialized anyway though, so it's all garbage.