asked    Reginald     2018-10-13       c++       106 view        1 Answer

[SOLVED] How to insert programmatically a value in a new row of a QtableView

I'm using a QtableView to display and edit data from a QsqlTableModel. Everything's fine : data from a postgreSQL table is displayed, and user can edit it and save modifications.

I want to add a row when uses click on a button. I use the insertRecord method from my QslTableModel. The row is correctly added in the QTableView.

My problem is : I want to insert a value from a query in the first cell of this new row. (to automatically populate an unique identifier).

This is my code :

def ajoutlgn(self):
    query_idNewLine = QtSql.QSqlQuery(self.db)
    if query_idNewLine.exec_('SELECT sp_idsuivi+1 FROM bdsuivis.t_suivprev_tablo ORDER BY sp_idsuivi DESC LIMIT 1'):
        while query_idNewLine.next():
            identifiant = query_idNewLine.value(0)
            #print identifiant

    record = QtSql.QSqlRecord()
    record.setValue(1,identifiant)
    self.model.insertRecord(self.model.rowCount(), record)

The value is not inserted in the new row (but if I enter a value by hand, it works perfectly). Nevertheless, the query is OK (as I can see with the « print identifiant » line, which returns the expected integer).

Do you see what I'm doing wrong ?

Are there other methods to insert programmatically a value in a QTableView cell?

Or do I have to use a QitemDelegate ?

Thanks for advance.

  1 Answer  

        answered    Neil     2018-10-13      

Finally I found the solution :

This line creates a record, but not a record for my QsqlTableModel

    record = QtSql.QSqlRecord()

I replaced it with this one, and it works perfectly :

    record = self.model.record()




Your Answer





 2018-10-13         Fay

Custom Layout in QListView

I'm looking for a way to create a custom layout in the QListView. The problem is the following: I have a number of images, with different widths (height is the same). In code, these images are stored in QAbstractItemModel. I want to lay them out similar to "justified" layout in text editors, i.e. in each row there's an image in the start and in the end of the row, images in between are placed uniformly. Spaces between images on the same row should be equal. I didn't find this customization functionality in Qt docs nor in google.How can I achieve custom layout like this usin...
 qt                     1 answers                     106 view
 2018-10-13         Cornelia

Retrieving the row number of a QTableView

I'm using a QTableView to display some data inside a table. Because no vertical header is given Qt automatically assigns a row-id to a row. The example below displays the following table:id|data-------1 | B2 | A3 | D4 | CAfter sorting the table based on the "data"-column:id|data-------2 | A1 | B4 | C3 | DWhen a user double-clicks an entry, I want to be able to identify the clicked row by its id (i.e. A=2, B=1, C=4, D=3). Unfortunately, the methods used in "onDoubleClicked" only return the "new" row-id (i.e. A=1, B=2, C=3, D=4).So how do I retrieve the correct row-id, when a...
 c++                     3 answers                     109 view
 2018-10-13         Ken

jQuery image preload order

I'm having troubles adapting a jQuery image preload script in just one aspect: I can't figure out how to ensure, that my images are loaded/displayed in the order of my image array.At the moment every time I reload the page the images are displayed in a different order. (Everything except the image order works fine alreday.)Can anyone help me fixing this?The relevant code: (function($) { var imgList = []; $.extend({ preload: function(imgArr, option) { var setting = $.extend({ init: function(loaded, total) {}, ...
 javascript                     3 answers                     110 view