在 Python 和 QT6 中显示图像旁边的表格时出现问题

问题描述 投票:0回答:1

我在窗口大小的图像(QLabel / QPixmax)旁边显示 QTableView 时遇到问题。

表格和图像(原始尺寸 600 x 400 像素)显示得非常小,请参阅附图。

from PyQt6 import QtCore
from PyQt6.QtWidgets import QApplication, QMainWindow, QTableView, QVBoxLayout, QHBoxLayout, QSplitter, QLabel, QTableWidgetItem, QTableWidget
from PyQt6.QtGui import QPixmap
from PyQt6.QtCore import Qt

class TableModel(QtCore.QAbstractTableModel):
    def __init__(self, data):
        super(TableModel, self).__init__()
        self._data = data

    def data(self, index, role):
        if role == Qt.DisplayRole:
            # See below for the nested-list data structure.
            # .row() indexes into the outer list,
            # .column() indexes into the sub-list
            return self._data[index.row()][index.column()]

    def rowCount(self, index):
        # The length of the outer list.
        return len(self._data)

    def columnCount(self, index):
        # The following takes the first sub-list, and returns
        # the length (only works if all rows are an equal length)
        return len(self._data[0])
    
    
class Window(QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent=parent)

        self.resize(1200, 700)


        # tabel
        self.table = QTableView()

        data = [
          [4, 9, 2],
          [1, 0, 0],
          [3, 5, 0],
          [3, 3, 2],
          [7, 8, 9],
        ]

        self.model =  TableModel(data)
        self.table.setModel(self.model)
        self.table.setColumnWidth(0,50)
        self.table.setColumnWidth(1,50)
        self.table.setColumnWidth(2,50)

        # picture
        self.picture = QLabel(self)
        #self.picture.setScaledContents(True)
        #self.picture.setMaximumWidth(600)
        self.pixmap = QPixmap('Bilder/no_image_available.jpg')
        # self.pixmap.scaled(self.picture.size(), Qt.AspectRatioMode.KeepAspectRatio)
        self.picture.setPixmap(self.pixmap)


        # layout
        self.Layout = QHBoxLayout(self)

        self.Layout = QSplitter(self)
        self.Layout.addWidget(self.table)
        self.Layout.addWidget(self.picture)

        #self.splitter = QSplitter(self)
        #self.splitter.addWidget(self.table)
        #self.splitter.addWidget(self.picture)

if __name__ == "__main__":
    import sys
    app = QApplication(sys.argv)
    win = Window()
    win.show()
    sys.exit(app.exec())

示例代码的输出:  table as well as the image (original size 600 x 400 pixels) are displayed very small

下一步是使用 QSplitter,在移动时将图像的大小重新调整为剩余宽度。

python qtableview pyqt6 qlabel qpixmap
1个回答
0
投票

通过@musicamante 的评论和进一步的研究,我能够解决我的问题。在下面的代码中,我添加了一个按钮,在激活时交替加载两个不同的图像。

from PyQt6 import QtCore
from PyQt6.QtWidgets import QApplication, QMainWindow, QTableView, QVBoxLayout, QHBoxLayout, QSplitter, QLabel, QPushButton, QWidget
from PyQt6.QtGui import QPixmap
from PyQt6.QtCore import Qt

class TableModel(QtCore.QAbstractTableModel):
    def __init__(self, data):
        super(TableModel, self).__init__()
        self._data = data

    def data(self, index, role):
        if role == Qt.ItemDataRole.DisplayRole:
            # See below for the nested-list data structure.
            # .row() indexes into the outer list,
            # .column() indexes into the sub-list
            return self._data[index.row()][index.column()]

    def rowCount(self, index):
        # The length of the outer list.
        return len(self._data)

    def columnCount(self, index):
        # The following takes the first sub-list, and returns
        # the length (only works if all rows are an equal length)
        return len(self._data[0])
    
    
class Window(QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent=parent)

        self.resize(800, 400)

        # Button
        self.button = QPushButton("toggle Picture")
        self.button.clicked.connect(self.load_pictrue)
         
        # tabel
        self.table = QTableView()

        data = [
          [4, 9, 2],
          [1, 0, 0],
          [3, 5, 0],
          [3, 3, 2],
          [7, 8, 9],
        ]

        self.model =  TableModel(data)
        self.table.setModel(self.model)
        self.table.setColumnWidth(0,50)
        self.table.setColumnWidth(1,50)
        self.table.setColumnWidth(2,50)

        # picture
        self.x = 1
        self.picture = QLabel(self)
        self.load_pictrue()

        # layout with splitter
        self.splitter = QSplitter(self)
        self.splitter.addWidget(self.table)
        self.splitter.addWidget(self.picture)

        self.Layout = QHBoxLayout(self)
        self.Layout.addWidget(self.splitter)

        self.VLayout = QVBoxLayout(self)
        self.VLayout.addWidget(self.button)
        self.VLayout.addLayout(self.Layout)

        self.w = QWidget()
        self.w.setLayout(self.VLayout)
        self.setCentralWidget(self.w)

    def load_pictrue(self):
        if self.x % 2:
            pixmap = QPixmap('Bilder/minion.png')
        else:
            pixmap = QPixmap('Bilder/75.08011.595.jpg')

        pixmap.scaled(200, 200, Qt.AspectRatioMode.KeepAspectRatio)
        self.picture.setPixmap(pixmap)     
        self.x += 1

if __name__ == "__main__":
    import sys
    app = QApplication(sys.argv)
    win = Window()
    win.show()
    sys.exit(app.exec())
© www.soinside.com 2019 - 2024. All rights reserved.