1

Actualmente estoy trabajando con el widget QTableWidget de la libreia PyQT5 y estoy agregando un widget dentro de cada una de las celdas, sin embargo me surgue el siguiente problema:

Problema: El widget que ingresa a una celda no se muestra completo ya que la celda no logra ajustarce al tamaño del widget

Posible solución: Trate de usar la propiedad resizeRowsToContents en la tabla pero no funciona y el resultado que obtengo es como este

introducir la descripción de la imagen aquí

Como se observa en la imagen la tabla no se ajusta al tamaño del widget que ahora esta en la celda 0,0

Solamente si agrego un tmaño predefinido a la tabla es como logra mostrarse

introducir la descripción de la imagen aquí

Resultado Esperado: Sin embargo lo que deseo obtener es que el tamaño se ajuste en automatico en cuanto ingrese un nuevo elemento a la celda, por ejemplo:

  • 1 elemento QPusButton con medidas 10x10 en la celda 0,0
  • 1 Elemento QLabel con medidas 30,30 en la celda 1,0

y asi sucesivamente que la celda se ajuste a ese tamaño

Código

from PyQt5.QtWidgets import QMainWindow,QApplication,QTableWidget,QPushButton,QWidget,QVBoxLayout

class Main(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)

        self.table = QTableWidget(self)
        self.table.setColumnCount(3)
        self.table.setRowCount(3)
        self.table.resize(400,300)
        
        widget = QWidget()
        layout = QVBoxLayout()
        btn = QPushButton()
        btn.setText("Press Me")
        layout.addWidget(btn)
        widget.setLayout(layout)
        self.table.setCellWidget(0,0,widget)

        self.table.resizeRowsToContents()

app = QApplication([])
m = Main()
m.show()
app.exec_()

1 respuesta 1

1

No sé si te entendí bien, pero prueba esto:

QHeaderView::setSectionResizeMode(QHeaderView::ResizeMode mode)

Sets the constraints on how the header can be resized to those described by the given mode.

enum QHeaderView::ResizeMode

The resize mode specifies the behavior of the header sections. It can be set on the entire header view or on individual sections using setSectionResizeMode().

from PyQt5.QtWidgets import QMainWindow, QApplication, QTableWidget,\
    QPushButton, QWidget, QVBoxLayout, QLabel, QHeaderView
from PyQt5.QtGui import QPixmap

class Main(QMainWindow):
    def __init__(self):
        super().__init__()
        
        self.centralwidget = QWidget()                                                   # +
        self.setCentralWidget(self.centralwidget)                                        # +

        self.table = QTableWidget(3, 3, self)
#        self.table.resize(400, 300)

        self.table.horizontalHeader().setSectionResizeMode(QHeaderView.ResizeToContents) # +
        self.table.verticalHeader().setSectionResizeMode(QHeaderView.ResizeToContents)   # +
        
        self.main_layout = QVBoxLayout(self.centralwidget)                               # + 
        self.main_layout.addWidget(self.table)                                           # +
        
        widget = QWidget()
        btn = QPushButton("x")                
        btn.setFixedSize(25, 25)
        layout = QVBoxLayout(widget)
        layout.setContentsMargins(2, 2, 2, 2)
        layout.addWidget(btn)
        self.table.setCellWidget(0, 0, widget)

        pixmap = QPixmap('lena.jpg').scaled(50, 50)  
        label = QLabel()
        label.setPixmap(pixmap)
        self.table.setCellWidget(1, 1, label)

        self.table.setCellWidget(2, 2, QLabel('Hello World'))

#        self.table.resizeRowsToContents()
        
        
if __name__ == "__main__":
    import sys
    app = QApplication(sys.argv)
    m = Main()
    m.show()
    sys.exit(app.exec_())

introducir la descripción de la imagen aquí

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.