Get content of cell from QTableView
-
Hi, I've tried the same (almost, because in python), but I gain error: name 'index' is not defined
-
Hi, I've tried the same (almost, because in python), but I gain error: name 'index' is not defined
@Karoluss96 Please post the code.
Did you define the "index" member variable? -
No, but It is noe build-in function? like here: https://stackoverflow.com/questions/37035756/how-to-select-multiple-rows-in-qtableview-using-selectionmodel
-
No, but It is noe build-in function? like here: https://stackoverflow.com/questions/37035756/how-to-select-multiple-rows-in-qtableview-using-selectionmodel
@Karoluss96 If you read this thread, especially what @JonB asked you will see that it is unclear what index is in the code posted. Sinse it is not my code I also don't know.
-
I put tableView.currentIndex() here, that take the number of index for data, which work correct, but later .data() says: ' 'int' object has no attribute 'data''
-
I put tableView.currentIndex() here, that take the number of index for data, which work correct, but later .data() says: ' 'int' object has no attribute 'data''
@Karoluss96 Can you please post the code?
-
I put tableView.currentIndex() here, that take the number of index for data, which work correct, but later .data() says: ' 'int' object has no attribute 'data''
@Karoluss96 said in Get content of cell from QTableView:
but later .data() says: ' 'int' object has no attribute 'data''
Do you expect people to say anything useful about this when you show no code?
-
My plan is to get data from whole table and separate it between rows to make sql question (Insert Into those who are new added)
now (after cutting of .toInt() ) looks that:data= self.dlg.tableView_3.model().index(self.dlg.tableView_3.currentIndex().row(),0) dt=data.data() print (dt)
-
It take only the first record from selected row, wheras I need all data from selected row, which later I want to put (separatly one-by-one) to a sql question
-
It take only the first record from selected row, wheras I need all data from selected row, which later I want to put (separatly one-by-one) to a sql question
@Karoluss96 said in Get content of cell from QTableView:
It take only the first record from selected row
Careful with words, you mean column not record. So look at your
0
argument which only fetches the data for column 0, and write code to pick up all the columns in the row if that is what you want.... -
Maybe some loop will be good?
-
Maybe some loop will be good?
@Karoluss96 I would think that would indeed be a good idea if you want to get the content of each column :)
-
Now I have:
data= self.dlg.tableView_3.model().index(self.dlg.tableView_3.selectionModel().currentIndex().row(),0) dane=data.data() for i in data: data=self.model4.selectIndex(i) print (data)
but it get error: 'QModelIndex' object is not iterable
-
Now I have:
data= self.dlg.tableView_3.model().index(self.dlg.tableView_3.selectionModel().currentIndex().row(),0) dane=data.data() for i in data: data=self.model4.selectIndex(i) print (data)
but it get error: 'QModelIndex' object is not iterable
@Karoluss96
This is wrong. Yourdata
variable is a single data index item, you can't gofor i in data
. Please look at the documentation for correct usage of QVariant QAbstractItemModel::data(const QModelIndex &index, int role = Qt::DisplayRole) const or QVariant QModelIndex::data(int role = Qt::DisplayRole) const -
hmm, so maybe the the whole row at begin in loop?
-
hmm, so maybe the the whole row at begin in loop?
@Karoluss96
I really should not have to tell you this! If you want to allow for picking all the columns in a row even if just one cell is selected (so you can't only look at selected cells) you will want something like:row = self.dlg.tableView_3.selectionModel().currentIndex().row() model = self.dlg.tableView_3.model() for col in range(model.columnCount()): print(model.index(row, col).data()) # Or I think you can do this in Python: rowData = [ model.index(row, col).data() for col in range(model.columnCount()) ] print(rowData)
-
@JonB said in Get content of cell from QTableView:
self.dlg.tableView_3.selectionModel().currentIndex().row()
Well, It works, but I need to separate it to put every attribute to INSER INTO sql query.
Thnaks for help!
-
@JonB said in Get content of cell from QTableView:
self.dlg.tableView_3.selectionModel().currentIndex().row()
Well, It works, but I need to separate it to put every attribute to INSER INTO sql query.
Thnaks for help!
@Karoluss96
I don't know why you quote the first line of this code, but anyway.Well, It works, but I need to separate it to put every attribute to INSER INTO sql query.
And what is the problem with that? Assuming
rowData = [ model.index(row, col).data() for col in range(model.columnCount()) ]
works in Python you have
rowData
containing all the values you need for your SQL statement. -
I suppose true. I forgot to change the first word in my previous answer from nagative to positive :-)
-
If can add something for it:
I need to change all empty (if is empty!) records in rowData from 'None' Type to 'Null' (for better recognising it in SQL Question).It's obviosly rather a python than qt question, but I can't find good answer in a differnt forum.