0

I am using Pyside2 to build an application that I populate a TableView with ProgressBar. I am able to put the Progress on each line, therefore, I am not able to vertically center the ProgressBar the same way I center the text on column 1.

I tried the following in the ProgressBar properties:

progressList[i].setAlignment(Qt.AlignVCenter)
progressList[i].setAlignment(Qt.AlignCenter)

But there is no effect.

The image shows the appearance, the ProgressBar remains in the top row

enter image description here

The following is the code that I am using to populate the tableview rows

QMainWindow.__init__(self, parent)

        self.setupUi(self)
        self.setIcon()
        self.showMaximized()

        self.modelo = CustomTableModel(data)

        self.tableView.setModel(self.modelo)

        self.titulos = self.tableView.horizontalHeader()

        self.titulos.setSectionResizeMode(QHeaderView.Interactive)
        self.titulos.setStretchLastSection(True)


        self.tableView.verticalHeader().setVisible(False)

        self.tableView.selectionModel().selectionChanged.connect(self.selectedItem)
        self.tableView.setSelectionBehavior(QTableView.SelectRows)
        self.tableView.setSelectionMode(QTableView.SingleSelection)

        verticalHeader = self.tableView.verticalHeader()
        verticalHeader.setSectionResizeMode(QHeaderView.Fixed);
        verticalHeader.setDefaultSectionSize(60);


        
        progressList = []
        vlayoutList = []
        for i in range(0,len(data)):
            print(data[i][1])
            vlayoutList.append(QVBoxLayout())
            progressList.append(QProgressBar())
            progressList[i].setMinimum(0)
            progressList[i].setMaximum(10)
            if data[i][1] > 7:
                progressList[i].setStyleSheet(" QProgressBar { border: 2px solid gray; border-radius: 0px; text-align: center; } QProgressBar::chunk {background-color: #3add36; width: 1px;}")
            elif data[i][1] > 4 and data[i][1] < 8: 
                progressList[i].setStyleSheet(" QProgressBar { border: 2px solid gray; border-radius: 0px; text-align: center; } QProgressBar::chunk {background-color: #ff9000; width: 1px;}")
            elif data[i][1] < 5:
                progressList[i].setStyleSheet(" QProgressBar { border: 2px solid gray; border-radius: 0px; text-align: center; } QProgressBar::chunk {background-color: #ff0000; width: 1px;}")
            progressList[i].setTextVisible(False)
            progressList[i].setValue(data[i][1])

            progressList[i].setMaximumHeight(30)
            progressList[i].setAlignment(Qt.AlignVCenter)

            self.tableView.setIndexWidget(self.tableView.model().index(i,1), progressList[i])
        
Gto Carlos
  • 37
  • 5

1 Answers1

0

I had solved the problem by using a vertical layout as a progressbar parent and create an Widget from it, the code that solved is shown as follow

progressList[i].setMaximumHeight(30)
progressList[i].setAlignment(Qt.AlignVCenter)
vlayoutList[i].addWidget(progressList[i])
wdgetList[i].setLayout(vlayoutList[i])
self.tableView.setIndexWidget(self.tableView.model().index(i,1), wdgetList[i])

Best regards

Gustavo

Gto Carlos
  • 37
  • 5