Inserting empty rows into table due to 'duplicate items' - qt

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.

Related

Vector out of range for elements of different size

I'm trying to add items from a csv into a vector. Some of the items have 2 elements, some 3, some 4. The code is working fine for the first 3 elements, but when I try to add the fourth I get an out of range error.
int i = 1;
while (i < temp.size()) {
Course course;
course.courseId = temp[i][0];
course.name = temp[i][1];
if (!temp[i][2].empty()) {
course.prereq = temp[i][2];
/*if (!temp[i][3].empty()) {
course.prereq2 = temp[i][3];
}*/
}
courses.push_back(course);
i++;
cout << i;
}
Thank you.

Last line of a datatable asp.net

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);
}
}
}
}
}

Visible columns in Treetableview

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;

dereference pointer always prints 0

I'm trying to figure out why dereferencing my pointer always prints 0. I put in other print statements to make sure random() is working correctly, and it does.
int * first = (int *) malloc(sizeof(int) * N);
while( i < N)
first[i++] = random();
printf("%d", first[i]);
}
I even assigned the values of first to another array and those values matched the ones returned by random(). Why does my print statement in this while loop always print 0?
first[i++] = random();
printf("%d", first[i]);
Assuming i is 0, with these two lines you are assigning a value to the first element of the array:
first[0] = random();
incrementing the index:
i++
then printing the value in the second element of the array:
printf("%d", first[1]);
If you make the incrementing of the index explicit it should be clearer:
while (i < N)
{
first[i] = random();
printf("%d", first[i]);
i++;
}
(You also appeared to have missed the opening bracket ({) but that could be a typo in the question)

Regarding IndexOutOfRange error asp.net c#

Hi
i am creating online quiz. For that i am creating arrays of answers selected by user. i used following code for that, it gives correct array but sometimes gives error "Index was outside the range"
//rsel is session values for selected answer
int rsel = Convert.ToInt32(Session["rblsel"]);
// [Convert.ToInt32(Session["Counter"] indicates size of array of no. of questions
int[] ansarray = new int[Convert.ToInt32(Session["Counter"]) - 1];
int[] temp = (int[])Session["arrofans"];
int j,n;
if (temp == null)
n = 0;
else
n = temp.Length;
for (j = 0; j < n; j++)
{
ansarray[j] = temp[j];
}
ansarray[j] = rsel;
Session["arrofans"] = ansarray;
Help me to find out exact error. Asp.net,c#
Thank you.
Why are you reducing the "counter" by one?
int[] ansarray = new int[Convert.ToInt32(Session["Counter"]) - 1];
It looks like that should probably be a + 1... but to be honest it would be simpler to use the size of ansarray - and use Array.Resize to effectively extend it:
int[] ansarray = (int[])Session["arrofans"];
Array.Resize(ref ansarray, ansarray.Length + 1);
ansarray[ansarray.Length - 1] = rsel;
Session["arrofans"] = ansarray;
That way you don't even need the "Counter" part of the session.
One possible OutOfRange could be triggered when
**arrofans.length >= Counter**
temp.Length should not be bigger than ansarray.Length, or precisely from your code it ansarray.Length must be temp.Length+1 or bigger. To avoid your problem you must change it to for (j = 0; j < n && j < (ansarray.Length-1); j++) but i don't know if it will suite your case

Resources