使用 PyQt 和 QTableWidget 在 Horizo​​ntalHeaderItem 中设置图像

发布于 2024-10-30 17:24:56 字数 812 浏览 0 评论 0原文

我尝试对 QTableWidgetItem 进行子类化,然后使用该新的子类设置我的horizo​​ntalHeaderItem。例如:

class ImageWidget(QtGui.QTableWidgetItem):

def __init__(self, imagePath, parent):
    super(ImageWidget, self).__init__(parent)
    self.picture = QtGui.QPixmap(imagePath)

def paintEvent(self, event):
    painter = QtGui.QPainter(self)
    painter.drawPixmap(0, 0, self.picture)

class showTable(QtGui.QDialog):

def __init__(self, parent=None):
    tableWidget = QtGui.QTableWidget(10, 2)
    imagePath = "C:/Documents and Settings/pwr37669/workspace/Pro_GUI_Py/images/led_green.gif"
    item = ImageWidget(imagePath, QtGui.QTableWidgetItem())
    tableWidget.setHorizontalHeaderItem(0, item)

    tableWidget.show() 

我知道代码不起作用,但是,我正在尝试让类似的东西起作用。

我还需要更改单击单元格或标题的结果。

任何帮助将不胜感激。 谢谢, 斯蒂芬

I tried sub-classing the QTableWidgetItem and then set my horizontalHeaderItem with that new sub-classed class. for instance:

class ImageWidget(QtGui.QTableWidgetItem):

def __init__(self, imagePath, parent):
    super(ImageWidget, self).__init__(parent)
    self.picture = QtGui.QPixmap(imagePath)

def paintEvent(self, event):
    painter = QtGui.QPainter(self)
    painter.drawPixmap(0, 0, self.picture)

class showTable(QtGui.QDialog):

def __init__(self, parent=None):
    tableWidget = QtGui.QTableWidget(10, 2)
    imagePath = "C:/Documents and Settings/pwr37669/workspace/Pro_GUI_Py/images/led_green.gif"
    item = ImageWidget(imagePath, QtGui.QTableWidgetItem())
    tableWidget.setHorizontalHeaderItem(0, item)

    tableWidget.show() 

I know that code won't work, but, I'm trying to get something like that to work.

I also need to change the results of clicking on a cell or the header.

Any help would be greatly appreciated.
Thanks,
Stephen

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

放低过去 2024-11-06 17:25:16

以下似乎可以解决问题:

class ImgWidget1(QtGui.QLabel):

    def __init__(self,imagePath, parent=None):
        super(ImgWidget1, self).__init__(parent)

        pic = QtGui.QPixmap(imagePath)
        self.setAlignment(QtCore.Qt.AlignCenter)
        self.setPixmap(pic)

然后当我想添加图像时,我这样做了:

self.tableWidget.setCellWidget(rows, cells, ui_polling.ImgWidget1(imagePath))

希望这对那里的人有帮助。

The following seems to do the trick:

class ImgWidget1(QtGui.QLabel):

    def __init__(self,imagePath, parent=None):
        super(ImgWidget1, self).__init__(parent)

        pic = QtGui.QPixmap(imagePath)
        self.setAlignment(QtCore.Qt.AlignCenter)
        self.setPixmap(pic)

And then when I wanted to add an image I did this:

self.tableWidget.setCellWidget(rows, cells, ui_polling.ImgWidget1(imagePath))

Hope this helps someone out there.

秋叶绚丽 2024-11-06 17:25:16

请检查下面的示例是否适合您:

import sys
from PyQt4 import QtGui, QtCore

class MainForm(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(MainForm, self).__init__(parent)

        tableWidget = QtGui.QTableWidget()
        tableWidget.setRowCount(3)
        tableWidget.setColumnCount(2)

        for column in range(0, 2):
            for row in range(0, 3):
                print row, column
                item = QtGui.QTableWidgetItem("new item")
                tableWidget.setItem(row, column, item)

        headerItem = QtGui.QTableWidgetItem("Header Test")
        headerItem.setIcon(QtGui.QIcon(QtGui.QPixmap("your_image.png")))
        headerItem.setTextAlignment(QtCore.Qt.AlignVCenter);

        tableWidget.setHorizontalHeaderItem(0, headerItem)

        tableWidget.itemClicked.connect(self.on_tableWidget_itemClicked)
        tableWidget.connect(tableWidget.horizontalHeader(), QtCore.SIGNAL('sectionClicked(int)'), self.on_headersection_clicked)

        self.setCentralWidget(tableWidget)

    def on_tableWidget_itemClicked(self, item):
        print "item clicked " + item.text()

    def on_headersection_clicked(self, item):
        print "header section clicked " + str(item) 

def main():
    app = QtGui.QApplication(sys.argv)
    form = MainForm()
    form.show()
    app.exec_()

if __name__ == '__main__':
    main()

希望这有帮助,问候

Pls, check if an example below would work for you:

import sys
from PyQt4 import QtGui, QtCore

class MainForm(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(MainForm, self).__init__(parent)

        tableWidget = QtGui.QTableWidget()
        tableWidget.setRowCount(3)
        tableWidget.setColumnCount(2)

        for column in range(0, 2):
            for row in range(0, 3):
                print row, column
                item = QtGui.QTableWidgetItem("new item")
                tableWidget.setItem(row, column, item)

        headerItem = QtGui.QTableWidgetItem("Header Test")
        headerItem.setIcon(QtGui.QIcon(QtGui.QPixmap("your_image.png")))
        headerItem.setTextAlignment(QtCore.Qt.AlignVCenter);

        tableWidget.setHorizontalHeaderItem(0, headerItem)

        tableWidget.itemClicked.connect(self.on_tableWidget_itemClicked)
        tableWidget.connect(tableWidget.horizontalHeader(), QtCore.SIGNAL('sectionClicked(int)'), self.on_headersection_clicked)

        self.setCentralWidget(tableWidget)

    def on_tableWidget_itemClicked(self, item):
        print "item clicked " + item.text()

    def on_headersection_clicked(self, item):
        print "header section clicked " + str(item) 

def main():
    app = QtGui.QApplication(sys.argv)
    form = MainForm()
    form.show()
    app.exec_()

if __name__ == '__main__':
    main()

hope this helps, regards

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文