Get content of cell from QTableView
-
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?
-
@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!
-
@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.
-
@Karoluss96
Sorry but this question does not mean much. If you are, say, generating anINSERT
statement from the column values in a row, and some of these are PythonNone
, and you need to passNULL
for this to SQL inVALUES
clause, then you must write code to do so at the appropriate point. If you use, say,QSqlQuery
to generate the statement you may find thatQSqlQuery::addBindValue()
does this for you if the value is PythonNone
, I don't know. -
@JonB said in Get content of cell from QTableView:
addBindValue()
well first I want to concatenate data from rowData into one 'train' of later sql Values like below:
dn2 = iden + ',\'' + teryt + '\',\'' +....+ DatMod
where iden is rowCount[0], teryt is rowCount[1] etc.
but it take erorr:
TypeError: can only concatenate str (not "NoneType") to str
-
@Karoluss96 said in Get content of cell from QTableView:
TypeError: can only concatenate str (not "NoneType") to str
@JonB told you that you will have to write code for that. You need to check whether it is None, if it is add "NULL" instead of the value into the string.
But it would be way better to use QSqlQuery::addBindValue() anyway...
-
@Karoluss96
Both of what @jsulm said.Since I already wrote
and some of these are Python
None
, and you need to passNULL
for this to SQL inVALUES
clause, then you must write code to do so at the appropriate point.I don't understand why you come back with
TypeError: can only concatenate str (not "NoneType") to str
error message when you haven't done anything about it. Please read the answers and act upon them, else it's pointless asking/responding. -
@jsulm I try inspiring from here: https://cpp.hotexamples.com/examples/-/QSqlQuery/addBindValue/cpp-qsqlquery-addbindvalue-method-examples.html but still I have only one
-
@Karoluss96 said in Get content of cell from QTableView:
but still I have only one
Only one what? Can you please be more precise?
-
This post is deleted!
-
@Karoluss96 How does the SQL query look like? What data does rowData contain? Do you have more than one place holder in your querry to fill?
-
sql = ' INSERT INTO ZARZADZANIE.ZAMOWIENIA (ID,TERYT,ZAM,...) VALUES ( '
after rowData:
query = QSqlQuery(db) dane2 = iden + ',\'' + teryt + '\',\'' +zam + '\',\'' +... + DatMod print (dane2) sqlCale = sql + dane2 + ')' print (sqlCale) query.exec_(sqlCale) while query.next(): self.model4.insertRow(row)
Insert Row isn't finished yet
Only in values data are filling
-
What exactly are expecting this code to do ?
Also, as already written by my fellows, you should rather use bind values to build your final query rather than building the string by hand.
Also, you are not doing any error checks with regard to the query execution and finally, your code seems to do two different counter intuitive things. First you do a database insertion and next you seem to try to retrieve data as a result of that insertion query.So what is you exact goal ?
-
well.. when I put last code in this topic I found that I forgot add ')' between values and word 'values' :-P
The only one thing to do is change default (non)value python's None to sql's NULL