In c++ wxwidgets, I am trying to make a move functions for a wxgrid, so it gets the old…
- User
In c++ wxwidgets, I am trying to make a move functions for a wxgrid, so it gets the old position and the new position and move the row data to the new position. So far it works perfectly for one item but when I try to add a new item the order gets wrong, help me solve this issue.
This is how it works right now: A - B - C - D, I move D to 0, the output is: D - A - B - C, but when I move B to 1 the output is wrong: C - B - D - A, so the first position is wrong now, but the newly added is correct.
This is my code:
void MoveRow(wxGrid* grid, int rowPos, int newRowPos) { grid->InsertRows(newRowPos);
for (int col = 0; col < grid->GetNumberCols(); col++) { grid->SetCellValue(newRowPos, col, grid->GetCellValue(rowPos + 1, col)); } grid->DeleteRows(rowPos + 1);}
std::vector pinnedRows;
void FrameMain::OnPinRows(wxCommandEvent& event) { wxArrayString rowValues = Utils::GetSelectedRowValues(gridGames, 0);
if (rowValues.Count() >= 1) { for (size_t i = 0; i < rowValues.Count(); i++) { int row = games->GetGameRow(rowValues[i]); std::vector<int>::iterator it = std::find(pinnedRows.begin(), pinnedRows.end(), row); if (it == pinnedRows.end()) { pinnedRows.push_back(row); } else { pinnedRows.erase(it); } } SetPinnedRows(); }}
void FrameMain::SetPinnedRows() { for (size_t i = 0; i < pinnedRows.size(); i++) { Utils::MoveRow(gridGames, pinnedRows[i], i); }
SetPinnedRowsColor();}
- Assistant
The issue in your code is that the order of the pinnedRows vector is not being updated correctly when you move a row. As a result, the order of the pinned rows in the grid becomes incorrect.
To solve this issue, you need to update the pinnedRows vector whenever a row is moved. Here's an updated version of your code that maintains the correct order:
- User
can you update the pinnedrows vector in SetPinnedRows instead, so the moverows can be re-used later without the vector?
- Assistant
Certainly! Here's an updated version of the code that updates the pinnedRows vector in the SetPinnedRows function:
void MoveRow(wxGrid* grid, int rowPos, int newRowPos) { grid->InsertRows(newRowPos); if (newRowPos < rowPos) { rowPos++; } for (int col = 0; col < grid->GetNumberCols(); col++) { grid->SetCellValue(newRowPos, col, grid->GetCellValue(rowPos, col)); } grid->DeleteRows(rowPos); } void FrameMain::OnPinRows(wxCommandEvent& event) { wxArrayString rowValues = Utils::GetSelectedRowValues(gridGames, 0); if (rowValues.Count() >= 1) { for (size_t i = 0; i < rowValues.Count(); i++) { int row = games->GetGameRow(rowValues[i]); std::vector<int>::iterator it = std::find(pinnedRows.begin(), pinnedRows.end(), row); if (it == pinnedRows.end()) { pinnedRows.push_back(row); } else { pinnedRows.erase(it); } } SetPinnedRows(); } } void FrameMain::SetPinnedRows() { std::sort(pinnedRows.begin(), pinnedRows.end()); std::vector<int> newPinnedRows; for (size_t i = 0; i < pinnedRows.size(); i++) { int rowPos = pinnedRows[i]; Utils::MoveRow(gridGames, rowPos, i); newPinnedRows.push_back(i); } pinnedRows = newPinnedRows; SetPinnedRowsColor(); } - User
this is wrong, pinnedrows vector contains the rows not the position, the position is set by the index, so pinnedrows[0] means the position 0 of the row.
- Assistant
Apologies for the confusion. If the
pinnedRowsvector contains the rows and not the positions, we can update theMoveRowfunction to also update thepinnedRowsvector accordingly. Here's the updated code:void MoveRow(wxGrid* grid, int rowPos, int newRowPos, std::vector<int>& pinnedRows) { grid->InsertRows(newRowPos); if (newRowPos < rowPos) { rowPos++; } for (int col = 0; col < grid->GetNumberCols(); col++) { grid->SetCellValue(newRowPos, col, grid->GetCellValue(rowPos, col)); } grid->DeleteRows(rowPos); // Update pinnedRows vector int row = pinnedRows[rowPos]; pinnedRows.erase(pinnedRows.begin() + rowPos); pinnedRows.insert(pinnedRows.begin() + newRowPos, row); } void FrameMain::OnPinRows(wxCommandEvent& event) { wxArrayString rowValues = Utils::GetSelectedRowValues(gridGames, 0); if (rowValues.Count() >= 1) { for (size_t i = 0; i < rowValues.Count(); i++) { int row = games->GetGameRow(rowValues[i]); std::vector<int>::iterator it = std::find(pinnedRows.begin(), pinnedRows.end(), row); if (it == pinnedRows.end()) { pinnedRows.push_back(row); } else { pinnedRows.erase(it); } } SetPinnedRows(); } } void FrameMain::SetPinnedRows() { std::sort(pinnedRows.begin(), pinnedRows.end()); for (size_t i = 0; i < pinnedRows.size(); i++) { int rowPos = games->GetGameRow(pinnedRows[i]); Utils::MoveRow(gridGames, rowPos, i, pinnedRows); } SetPinnedRowsColor(); } - User
no it is still wrong, the problem is this: The grid has a row order that never changes, so the pinnedRows vector contains these rows index of which rows are pinned. So when I add a new pinned row, I get the row index and store it in pinnedrows JUST to move around because the pinned rows should be at the top of the grid, but the grid rows doesn't change the numbers are still the same, take this into account when solving my problem, and if you have to modify everything do it so I can pin and get the pinned rows on the top
- Assistant
I apologize for the previous misunderstandings. Based on your clarification, it seems like the
pinnedRowsvector contains the row indices of the pinned rows, and the objective is to move the pinned rows to the top of the grid while preserving the order of the remaining rows. Here's an updated solution that addresses this requirement:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.