I have a qtablewidget with some rows. I need to sort these row randomly.
What is the best way to do this ? I have no idea except remove and add all row randomly ...
"I assume you are looking for some custom sort. Not column header click sort"
I have a way to do it. may be much more efficient way will be there.
get the "QList" of "QTableWidgetItem" using below function.
QList<QTableWidgetItem *> findItems(const QString & text, Qt::MatchFlags flags) const
Then sort your list as shown below with your condiion
bool sortFun(const QTableWidgetItem* v1, const QTableWidgetItem* v2)
{
return //your sor criteria;
}
int doComparison()
{
QList<QTableWidgetItem *> fieldsList;
// Add items to fieldsList.
qSort(fieldsList.begin(), fieldsList.end(), sortFun);
}
Not clear your "QTableWidget".
Then re assign your sorted list of "QTableWidgetItems" to QTableWidget
Finally I have done something different.
What I need to do:
- random the rows of my table
- I have a column with a "Time" and I initialize it with 15:00 for the first row and then add 1 minute for each row.
The solution I used:
- create a qlist with my times (15:00, 15:01, 15:02 ...)
- randomize the qlist (15:01, 15:03, ....)
- assign the qlist's items to the column "Time" of my table
- order my table with the column "Time" with the sort column method.
Related
I have a QTableWidget and an object of class product called p.
I wanna add items to this table.
I tried the code below but no use.
void MainWindow:: add_to_basket (product p){
ui->tableWidget->insertRow(1);
QLineEdit *qle=new QLineEdit();
qle->setText(p.get_name());
ui->tableWidget->setCellWidget(1,1,qle);
QLineEdit *qle1=new QLineEdit();
qle1->setText(QString::number(p.get_price()));
ui->tableWidget->setCellWidget(1,2,qle1);
QSpinBox *qsb=new QSpinBox();
qsb->setValue(p.get_count());
ui->tableWidget->setCellWidget(1,3,qsb);
}
what should I do?
QTableWidget should have defined rowCount and columnCount properties. It can be done either via QTableWidget constructor (https://doc.qt.io/qt-5/qtablewidget.html#QTableWidget-1) or via appropriate methods (setRowCounts and setColumnCounts). If it is done already, that's great.
insertRow inserts an empty row into the table at given position. ui->tableWidget->insertRow(1) would insert a new row at position 1 only if you have previously defined rowCount and columnCount (see point 1).
It depends what is your idea here - if you would like to have at least 4 columns (please note that we are counting from 0 and the QSpinBox is attempted to be put into third column) and insert new product always at first row, your code with point 1 fullfilled will be work fine. But, if your idea is to add a new row each time new product is added, you should rather call ui->tableWidget->insertRow(tableWidget->rowCount()) and use that value to address appropriate row.
You may also want to have a look here for example how to setup QTableWidget: https://wiki.qt.io/How_to_Use_QTableWidget
Is there any simplest way to get cell data of a tableview in javafx using table index values without using cellfactory function.Iam looking simple single line code like this
getValueAt(1,2);//1=column number 2=row number
So i can access the cell data on column=1 and row=2.Thanks in advance
You can get the observable using the TableColumn and use getValue on it:
public static Object getValueAt(TableView table, int column, int row) {
return table.getColumns().get(column).getCellObservableValue(row).getValue();
}
You can also use the following code. when you select a row using mouse
ProcessDetail pd =
(ProcessDetail)tableDefined.getSelectionModel().getSelectedItem();
System.out.println("Value is in this row which is selected"+pd.getStatus());
I have QTreeView and I setSortEnable = true for it, and all of column show sort indicator, but now I want last column do not show sort indicator, just last column.
Have any idea or solution for my issue..:(
please help
Thanks
QHeaderView always draws the sorting indicator on a column when data is sorted by this column (if setSortIndicatorShown is true).
That is why it look like the only way to prohibit drawing of the indicator is to prohibit sorting of the column by left-click.
To prohibit sorting by a certain column you need:
1. create a new class inherited from QHeaderView (or use an event filter)
2. reimplement mouseReleaseEvent and mousePressEvent
3. use logicalIndexAt to indicate that user clicked on the column
4. call setSectionsClickable(false) before calling the base method and setSectionsClickable(true) after it.
Example:
void HeaderView::mouseReleaseEvent(QMouseEvent* e)
{
const int index = logicalIndexAt(e->pos());
if (index == 3) setSectionsClickable(false);
QHeaderView::mouseReleaseEvent(e);
setSectionsClickable(true);
}
Do the same for mousePressEvent.
thus you will prohibit sorting of a certain column
In my case, I've three columns and it was necessary to hide the sort indicator of the second column.
Because of I had custom rules of sorting, just got QHeaderView::sectionClicked event with OnHeaderClicked(int column) slot, and did this:
switch (column)
{
case 0:
// Custom sorting operations
break;
case 1:
tree_widget_->sortByColumn(-1);
break;
case 2:
// Custom sorting operations
break;
}
How can read selected items from QTableWidget?
Thanks
int QTableWidget::currentRow() const
Returns the row of the current item.
int QTableWidget::currentColumn() const
Returns the column of the current item.
Some options (there are also others out there too):
# selectedRanges(), would give you the second cell from each selected row, for example:
indexes = []
for selectionRange in myTable.selectedRanges():
indexes.extend(range(selectionRange.topRow(), selectionRange.bottomRow()+1))
print "indexes", indexes # indexes is a list like [0, 2] of selected rows
for i in indexes:
print "specific item", myTable.item(i, 1).text()
results.append( str(myTable.item(i, 1).text()) )
# selectedItems()
for item in myTable.selectedItems():
print "selectedItems", item.text()
# selectedIndexes()
for item in myTable.selectedIndexes():
print "selectedIndexes", item.row(), item.column()
Use the selectedItems function to retrieve the selected items or the selectedIndexes to get all selected cells including empty ones.
the best way to access the items in a qtablewidget is using the function
QList QTableWidget::selectedRanges () const
I need to access columns of infragistics ultragrid in same sequence in which they are being displayed in grid. If i can get the index of column in same sequence as they are visible on grid, i can fix my issues.
Thanks in advance.
Lalit
UltraGridColumn column = this.ultraGrid1.DisplayLayout.Bands[0].Columns[0];
Debug.WriteLine( "Columns in visible order: ");
// Get the first visible column by passing in VisibleRelation.First.
column = column.GetRelatedVisibleColumn( VisibleRelation.First );
while ( null != column )
{
Debug.WriteLine( " " + column.Key );
// Get the next visible column by passing in VisibleRelation.Next.
column = column.GetRelatedVisibleColumn( VisibleRelation.Next );
}
http://help.infragistics.com/Help/NetAdvantage/NET/2008.2/CLR2.0/html/Infragistics2.Win.UltraWinGrid.v8.2~Infragistics.Win.UltraWinGrid.UltraGridColumn~GetRelatedVisibleColumn.html
I suppose you could try to handle the event that is fired when the order is changed and keep track of all the changes, but this seems like it is asking for subtle bugs to creep in.
I considered looping through all the columns and trying to use some property that would tell me their current position (maybe the TabOrder?) and use that to compile an inorder list of the columns. I guess you might have to loop through each colum using the Column.GetRelatedVisibleColumn() method.
I have not actually implemented it yet as I have other higher priority issues but that may be the road I end up going down.
This is an old question, but I recently had same issue and solved it this way:
var selectedCells = this.Selected.Cells;
List<int> columns = new List<int>();
foreach (var cell in selectedCells)
{
if (!columns.Contains(cell.Column.Index))
columns.Add(cell.Column.Index);
}
columns.Sort((x, y) => this.DisplayLayout.Rows.Band.Columns[x].Header.VisiblePosition.CompareTo(this.DisplayLayout.Rows.Band.Columns[y].Header.VisiblePosition));
You can then use columns to access columns in order they are shown.