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

Dificulties with selectionModel and selectedRows in qtablewidget.



  • Greetings. I am attempting to make a small script for QCad (computer aided drafting software) using QtDesigner to build the UI but I am having dificulties in understanding the libraries and examples in Qt sites and applying them.
    One possibility that justifies my difficulty may be the fact that I am using Javascript and the vast majority of examples being given on c++.

    Currently I have a table with data called "Obj_table" and the following code:
    appWin.handleUserMessage("Button entered fine");
    var rowlist0 = widgets["Obj_table"].selectionModel();
    appWin.handleUserMessage("select model has been declared");
    var rowlist1 = rowlist0.selectedRows();
    appWin.handleUserMessage("rowlist has been declared");
    appWin.handleUserMessage(rowlist1.toString());

    appWin.handleUserMessage inserts a message into the command line of QCad, so I can see where I bummed out on the code.
    The code runs successfully, but what is returned in rowlist1 comes in this format:

    QModelIndex(10,0,0x0,QTableModel(0x8606360) ) ,QModelIndex(8,0,0x0,QTableModel(0x8606360) ) ,QModelIndex(5,0,0x0,QTableModel(0x8606360) ) ,QModelIndex(2,0,0x0,QTableModel(0x8606360) )

    Where the 10, 8, 5 and 2 correspond to the rows I had selected on my widget (apparently it even registers them in the order I selected them on the table). So technically, it DOES give me the rows I asked for.

    So I ask: Have I skipped an important step that would have given me the direct result (10, 8, 5, 2) without the added extras or is this what I was supposed to return from the function and additional code to filter and convert the result was expected from the get go?
    Honestly, I think I messed up somewhere or misunderstood the way the class is supposed to operate.

    I would apreciate a correction and explanation, if possible.

    Thank you very much.


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    From what I can see, if you want only the row number you selected, you'll have to build a list looping through rowList1 and ask for the row..

    selectedRows works as expected since it returns the list of index matching the rows you selected.



  • Thank you for the welcoming and for the answer.

    Best regards.


Log in to reply