Results 1 to 4 of 4

Thread: Qtableview xml

  1. #1
    Join Date
    Jan 2020
    Posts
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Qtableview xml

    im trying to parse data from a qtableview to xml here is my code below:

    Qt Code:
    1. #
    2. table = self.the_material_view.the_model
    3. row = table.item
    4. input_1 = table.item(row,0).text().strip()
    5. input_2 = table.item(row,1).text().strip()
    6. input_3 = table.item(row,2).text().strip()
    7. filename = QFileDialog.getSaveFileName(self, self.tr('Save File'),
    8. os.getcwd(), self.tr("xml files (*.xml);; All files(.txt)"))
    9. file = open(filename[0],"w")
    10. file.write('<name>{name}</name>\n'.format(name=input_1))
    11. file.write('<material_type>{material_type}</material_type>\n'.format(material_type=input_2))
    12. file.write('<density>{density}</density>\n'.format(density=input_3))
    13. for row in range(3):
    14. for column in range(3):
    15. if table.cellWidget(row, column) == True:
    16. file.write('<c{row}{col}>{data}</c{row}{col}>\n'.format(row=row + 1, col=column + 1,
    17. data=table.item(row, column).text().strip()) + "\n")
    To copy to clipboard, switch view to plain text mode 

    i tried using currentIndex function but keep getting an error, the tableview is not callable, the model is based on QtGui.QStandardItemModel()
    Last edited by d_stranz; 2nd March 2021 at 00:04. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,021
    Thanks
    280
    Thanked 818 Times in 807 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Qtableview xml

    In line 2, are you assigning "table" to the QTableView or the QStandardItemModel? If it is the view, then why are you referencing something called "the_model"?

    In line 3, if "table" really is a reference to the QTableView and not the QStandardItemModel, then QTableView does not have any method named "item". This will probably throw an exception.

    But if "table" is really a reference to the QStandardItemModel, then QStandardItemModel::item() returns a reference to a QStandardItem and needs at least a row number argument. item() does not return a row number. You must tell it which row number you want the item from.

    So in lines 4-6, it seems that you are treating "table" as a reference to the QStandardItemModel, because you are calling item() with the correct arguments for the QStandardItemModel::item() method.

    But now in line 15, your confusion over whether "table" is the view or the model gets further confused, because neither QTableView nor QStandardItemModel have a "cellWidget()" method. QTableWidget does, but you started your post by saying you were using QTableView.

    In any case, QTableWidget::cellWidget() returns a QWidget reference, not a Boolean value, and whether it is a valid object reference or not has nothing to do with the text in the cell.

    And in your loop over rows and columns starting in line 13, if "row" in lines 4-6 has the value 0, then in the loop for row == 0, you are retrieving exactly the same items as you did for input_1, _2, and _3.


    You need to get clear exactly what classes you are using.
    Last edited by d_stranz; 2nd March 2021 at 00:39.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Jan 2020
    Posts
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Qtableview xml

    the_material_view is a reference to the qtableview, the_model is the model of that tableview, i need to use qtableview and not qtablewidget

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,021
    Thanks
    280
    Thanked 818 Times in 807 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Qtableview xml

    i need to use qtableview and not qtablewidge
    Then I suggest you rename your variables so you don't get confused between view and model, and read the documentation of QTableView, QStandardItemModel, and QStandardItem to learn the appropriate methods for each class.

    Your code is almost correct and will run if you figure out your confusion over the different classes and how to use them. It will run, but may not produce the results you expect.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Replies: 2
    Last Post: 9th December 2015, 01:07
  2. set XML tag to QTableView
    By mythili in forum Qt Programming
    Replies: 10
    Last Post: 7th May 2013, 12:43
  3. QTableView and QLineEdit OUTSIDE of QTableView, like Excel
    By JoZCaVaLLo in forum Qt Programming
    Replies: 10
    Last Post: 13th May 2011, 13:20
  4. Replies: 2
    Last Post: 26th November 2009, 04:45
  5. QTableView help
    By weaver4 in forum Qt Programming
    Replies: 4
    Last Post: 24th November 2009, 22:57

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.