Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to display data returned from Oracle procedure in a different order in QTableWidget?



  • I have some data returned from an Oracle stored proc in this order:

    Col1, Col2, Col3, Col4, Col5

    But I want to display it in my QTableWidget like this:

    Col2, Col5, Col3, Col4

    Col1 is a primary key so I don't want it displayed in the table.

    Now this is a very simple example and I know I can use setColumnHidden and moveSection but in my app there are screens that have 50+ columns, this would obviously take forever,

    Is there a more efficient way to do this? I have many stored procs that return data in a certain order but I don't want to display it in that order to the user in my QTableWidget. Using PYQT5, displaying data in a QTableWidget. I will have some comboBoxes, Date fields, strings, integers....

    Any type of sample code would be great but anything to get me pointed in the right direction would be helpful....

    l_query = cursor.callproc('MYPROC_PD', (l_cur,))
    l_results = l_query[0]
    allSQLRows = l_results.fetchall()


  • Lifetime Qt Champion

    Hi,

    A proxy model comes to mind for that see QAbstractProxyModel and the classes related.

    Hope it helps



  • Currently I'm using:
    Import cx_oracle
    Connection = cx_oracle.connect()
    Cursor = connection.cursor()
    ProcResults =cursor.callproc()

    I'm assuming I can't use this now and need to use something from QsqlQuery? Can I pass that a stored procedure?


  • Lifetime Qt Champion

    Sorry, I've read QTableView rather than Widget.

    If you populate a QTableWidget with the result of your query you don't need any special stuff, just put the data where you want them directly.

    If you take the QTableView + QSqlQueryModel then you should take a look at the SQL driver chapter of Qt's documentation where the support for stored procedure is discussed.



  • I just thought it wouldn't be very clean to just say:

    Query result(3) put into 6th column in table widget and so on.....

    Thought there may be a cleaner way to link result set and column positions.

    Is there an advantage of using qtTableView in your 2nd example over what I'm currently doing?


  • Lifetime Qt Champion

    You avoid having to create and/or update and/or re-create items for your QTableWidget when you call the stored procedure again.


Log in to reply