I have created a QComboBox delegate which I use for a single column of a QTableView (each cell of that column is an individual combobox), the data which I want to display in my combobox returns from the database as a string of id's separated by a ;
My question is, how do I populate my combobox with this data retrieved by the database.
To clarify: the column in question retrieves specific Id's (4 digit strings), sometimes an object may have multiple Id's (separated by ;), I want to create a combobox for the cells with multiple Id's so the user can select which one they want to work with.
Perhaps something like this would work:
QString data = "Id01;Id02;Id03;Id04;Id05";
QStringList list = data.split(";");
QComboBox *comboBox = new QComboBox;
comboBox->insertItems(0, list);
Related
I am very new to javaFX technology. I have created one tableview which is having multiple columns. Now I have a requirement where I need to replace all the values in one of the column with one new value. How to achive this in javaFX. The new value which will replace all the values in that table coloumn is coming from one pop up.
Just set the appropriate property for each item in the table:
table.getItems().forEach(item -> item.someProperty().set(someValue));
where someProperty() is the property accessor method for the property in the column of interest, and someValue is the value you're setting it to.
I'm writing a small program with QT creator (QT 5.2.1) under Windows 7 (32 bit) and I'm having problems reading the informations stored in a TableView. My application has 3 elements, a TableView to store text data, a TextBrowser to show info and a buttom.
I modified the TableView properties: when the user selects with the mouse a cell, the full row is selected and multiple row selection is not allowed.
The user select a row and when the buttom is pressed, I would like to read the content of a specific TableView cell and show it in a TextBrowser. In particular, I would like to know the row index of the selected row and read the content of the cell with that row index and a specific column index (example 2).
The pseudo-code is this:
void my_program::on_pushButton_clicked()
{
ui->textBrowser->append("button pressed");
QItemSelectionModel *select = ui->tableView->selectionModel();
int index_row = select->selectedRows();
int index_column = 2;
char cell_data[30] = ??[index_row][index_column]
ui->textBrowser->append(cell_data);
}
The main problem is that select->selectedRows() returns a QModelIndex that is a collection of indexs and I do not know how to convert it to int (since multiple selection are not allowed, it should have only one element).
I would be glad if someone can suggest me a way to proceed.
Thanks
Francesco
edit:
Hi Bogdan, thanks a lot!! I succeed to read the cell content by using
ui->textBrowser->append(ui->tableView->model()->data(ui->tableView->model()->index(2,5)).toString());
this give me the content of the cell in position 2,5.
not sure if this is the best way or not but it works !!.
Can you be a bit more precise about how to iterate the QModeIndexList ? thanks :)
selectedRows() returns QModelIndexList, thus you need to iterate over it and call QModelIndex::data() to get stored data.
A simplified view of the problem I'm having....
A TableView TV contains two TableColumns (Name, Value).
A data structure Class obj_data contains two data items, SimpleStringProperty location, and double X. Multiple obj_data objects are stored in an observalable list and used with the TableView TV as it's data source
In the rendering of TableColumn Name, I need to get and the interrogate the value the corresponding X in order to change some visual attribute of the location tableCell..
I've played around with...
nameColumn.setCellValueFactory(new Callback, ObservableValue>()...
to return the value of obj_data element but am having difficulty trying to get the index of the table cell so I can correctly render the "location" field.. Hope this makes sense!
PS: This was so much more simple pre-javaFX with cellrenderers since I could get the row & col locations in the callback arguments!
Thanks in advance!
Currently I have a simple setup where I maintain a list of bools corresponding to each item in my ListModel:
http://programmingexamples.net/wiki/Qt/ModelView/StringListModelCheckable
However, now what I want to do is have two such lists bools, say IsHot and IsLarge. Then I want to have a ListView that displays each string with a checkbox for one of the bools (the "Hot" view), and a separate ListView that displays each string with a checkbox for the other bool (the "Large" bool). Any hints on how to go about this?
Make your model a table, return the data in two different columns, the cells in these columns will be checkable.
On your list views, call QListView::setModelColumn to set the column the list view displays.
If you want to synchronise scrolling between the lists, you would then be better using a QTableView, so that rows would match up.
Edit to add more detail on returning text and check state.
To return a text and the state of a checkbox from the model you return different data for different roles from the [data] function on your model.
From the manual for Qt::ItemDataRole:
Qt::DisplayRole The key data to be rendered in the form of text. (QString)
Qt::CheckStateRole This role is used to obtain the checked state of an item. (Qt::CheckState)
Both columns in your model would return the same data (the name) for DisplayRole but different data for CheckStateRole
I'm using PyQt to create a GUI application. In a view inherited from QTableView, need to detect the row the user has selected when they double click a row. The table has sorting, but no editing.
How do I do it?
Note - tried the doubleClicked(int) signal. It is emitted by mouse buttons, not by data cells, so it was never fired. :(
Ian
I dont understand.
The doubleClicked signal of the QTableView has the signature
void doubleClicked ( const QModelIndex & index )
If you connect that signal you should obtain the correct QModelIndex.
No need to use SIGNALs anymore:
self.your_table.doubleClicked.connect(your_function)
"doubleClicked" being inherited from QAbstractItemView.
Once you have the modelIndex, (from Frank's comment above) you can use it to find which cell was double clicked.
def slotDoubleClicked(self, mi):
row = mi.row()
column = mi.column()
You then can use these row and col values to access the table with table.setItem(row, column, newdata) or other table method
Like #regomodo said, you can simply connect your function to the double click via:
self.your_table.doubleClicked.connect(your_function)
Then, if you want to know on which row the user double clicked, you can use the following code:
for idx in self.your_table.selectionModel().selectedIndexes():
row_number = idx.row()
column_number = idx.column()
It will return an integer corresponding to the row or the column number.
There will always only be a single value as the double click remove the previous selection.
If you link your function to a push button or another signal, you can receive a list containing multiple elements selected by the user.
For example, you can easily retrieve a list of all selected rows using this code:
rows = []
for idx in self.your_table.selectionModel().selectedIndexes():
rows.append(idx.row())
rows = list(set(rows))
This will return a list of all selected rows (The set function will also remove any duplicates).
Cheers!