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

PyQt5 QTreeWidget Multiple Column Count



  • Hello,
    I am using QTreeWidget.
    soru.png
    or https://ibb.co/qjhKWPY (backup for image)
    As i specify in screenshot, i want to expand 1 to 4 columns for see all of text.
    My Code:

                baslik = QTreeWidgetItem([str(veri.Masa), str(veri.CPU), str(veri.GPU), str(veri.RAM)])
                icerik_islemci = QTreeWidgetItem(["İşlemci:", str(veri.CPU)])
                icerik_ekrankarti = QTreeWidgetItem(["Ekran Kartı:", str(veri.GPU)])
                icerik_anakart = QTreeWidgetItem(["Anakart:", str(veri.Motherboard)])
                #icerik_islemci.setExpanded(True)
                #icerik_ekrankarti.setExpanded(True)
                #icerik_anakart.setExpanded(True)
                #baslik.setExpanded(True)
                #icerik_islemci.setExpanded(True)
                #icerik_islemci.setFirstColumnSpanned(True)
                baslik.addChild(icerik_islemci)
                baslik.addChild(icerik_ekrankarti)
                baslik.addChild(icerik_anakart)
    
                self.liste.addTopLevelItem(baslik)
    

    Thanks



  • @JonB said in PyQt5 QTreeWidget Multiple Column Count:

    column spanning

    Thank you!
    I solved with this code

                icerik_islemci = QTreeWidgetItem(["İşlemci:%s"% str(veri.CPU)]) # it was 2 strings, now 1. ( "islemci:", str(veri.CPU) )
                self.liste.addTopLevelItem(baslik) 
                icerik_islemci.setFirstColumnSpanned(True)
    

    But i want to ask, is it possible to span colum n 2 to 4 ? This code spanning from 1 to 4.
    Thank you :)



  • @Emrecp
    Set your column widths explicitly, or look at using QTreeView.resizeColumnToContents().



  • No that's not what i want.
    I want add 4 texts and 2 texts as child.
    For example in this screenshot, Emre, 42.375, 33, 7763 values are 4 texts. That's fine, but when i add "islemci" as child, i want to be column count set to 2. Not 4. So text will be fit.



  • @Emrecp
    I think you mean you want column spanning? If so I only see https://doc.qt.io/qt-5/qtreeview.html#setFirstColumnSpanned as available for QTreeWidget? And I have a feeling the "first column" there means the treeview column, not what you call the second column where the red line starts?

    You could read https://stackoverflow.com/questions/26739541/how-to-span-the-columns-of-a-qtreeview-in-c-qt. The accepted answer there seems to be saying what I suggested about sizing columns to fit, but that's not spanning or what you want, I think. Looks like others suggest that doing it via an item delegate is the only way to go?



  • @JonB said in PyQt5 QTreeWidget Multiple Column Count:

    column spanning

    Thank you!
    I solved with this code

                icerik_islemci = QTreeWidgetItem(["İşlemci:%s"% str(veri.CPU)]) # it was 2 strings, now 1. ( "islemci:", str(veri.CPU) )
                self.liste.addTopLevelItem(baslik) 
                icerik_islemci.setFirstColumnSpanned(True)
    

    But i want to ask, is it possible to span colum n 2 to 4 ? This code spanning from 1 to 4.
    Thank you :)



  • Solved :)
    Added \t\t
    Now everything is fine
    Code:

    icerik_islemci = QTreeWidgetItem(["İşlemci:\t\t%s"% str(veri.CPU)])
    

Log in to reply