PyQt4 如何找到 QGridLayout 中小部件的行号?

发布于 2024-10-28 21:45:59 字数 745 浏览 12 评论 0原文

我有一个 PyQt4 应用程序,其中 QGridLayout 作为布局。此布局中有 n 个小部件,每个小部件位于另一行,但不位于另一列。我已经使用构造函数制作了所有小部件。我想知道,如何获取网格布局中小部件的行号,以便当我单击它时,它会获取该数字,并且我可以在代码中进一步使用它。

代码如下所示:

...
class sampleWidget(QWidget):
    def __init__(self):
        QWidget.__init__(self)
        ...
        self.show()

....

class mainClass(QWidget):
    def __init__(self):
        QWidget.__init__(self)
        layout = QGridLayout()
        self.setLayout(layout)
        for i in xrange(10):
            widget = sampleWidget()
            widget.setObjectName("samplewidget" + i)
            layout.addWidget(i, 0)
        self.show()

 ....

我已经进行了所有必要的导入以及运行程序所需的所有内容,不用担心。我唯一担心的是如何获取创建的小部件的行号。

如果有人愿意帮助我,我将非常感激!

祝你有美好的一天。

I have a PyQt4 application with a QGridLayout as Layout. This layout has n widgets in it, each on another row, but not on another column. I have made all the widgets using a constructor. I was wondering, how do I get the row number of a widget in the grid layout, so that when I click on it, it gets that number and I can use it further in my code.

The code looks like this:

...
class sampleWidget(QWidget):
    def __init__(self):
        QWidget.__init__(self)
        ...
        self.show()

....

class mainClass(QWidget):
    def __init__(self):
        QWidget.__init__(self)
        layout = QGridLayout()
        self.setLayout(layout)
        for i in xrange(10):
            widget = sampleWidget()
            widget.setObjectName("samplewidget" + i)
            layout.addWidget(i, 0)
        self.show()

 ....

I have made all the necessary imports and all what is needed to run the program, don't worry. My only worry is how to get the row number of a widget created.

If someone is willing to help me I would be very grateful!

Have a great day.

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

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

发布评论

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

评论(1

情泪▽动烟 2024-11-04 21:45:59

我可能会遗漏一些明显的东西,但这至少是做到这一点的一种方法。
编辑:我对我的第一个建议并不满意。因此改变了它。对于这个问题可能有点过分,但应该展示如何获取您所要求的信息。

from PyQt4 import QtGui, QtCore
import sys, collections

pos = collections.namedtuple("pos", ("row, column"))

class Widget(QtGui.QWidget):

    itemSelected = QtCore.pyqtSignal(QtGui.QWidget, pos)

    def __init__(self):
        super(Widget, self).__init__()
        layout = QtGui.QGridLayout(self)
        for y in range(0, 11):
            layout.addWidget(QtGui.QLabel("Row: %d" % y, self), y, 0)
            for x in range(1,4):
                layout.addWidget(QtGui.QLabel("QLabel"), y, x)
        self.itemSelected.connect(self.onItemSelected)

    def mousePressEvent(self, event):
        widget = self.childAt(event.pos())
        if isinstance(widget, QtGui.QLabel): # Or whatever type you are looking for
            self._handleEvent(widget)
        return QtGui.QWidget.mousePressEvent(self, event)

    def _handleEvent(self, widget):
        layout = self.layout()
        index = layout.indexOf(widget)
        row, column, cols, rows = layout.getItemPosition(index)
        self.itemSelected.emit(widget, pos(row, column))

    def onItemSelected(self, widget, pos):
        print "%s at %s" % (widget, pos)

if __name__ == "__main__":
    app = QtGui.QApplication([])
    wnd = Widget()
    wnd.show()
    sys.exit(app.exec_())

I might be missing something obvious but this is at least one way of doing it.
Edit: I wasn't happy with my first suggestion. Changed it therefore. Might be a bit overboard in regards to the question but should show how to get the information you asked for.

from PyQt4 import QtGui, QtCore
import sys, collections

pos = collections.namedtuple("pos", ("row, column"))

class Widget(QtGui.QWidget):

    itemSelected = QtCore.pyqtSignal(QtGui.QWidget, pos)

    def __init__(self):
        super(Widget, self).__init__()
        layout = QtGui.QGridLayout(self)
        for y in range(0, 11):
            layout.addWidget(QtGui.QLabel("Row: %d" % y, self), y, 0)
            for x in range(1,4):
                layout.addWidget(QtGui.QLabel("QLabel"), y, x)
        self.itemSelected.connect(self.onItemSelected)

    def mousePressEvent(self, event):
        widget = self.childAt(event.pos())
        if isinstance(widget, QtGui.QLabel): # Or whatever type you are looking for
            self._handleEvent(widget)
        return QtGui.QWidget.mousePressEvent(self, event)

    def _handleEvent(self, widget):
        layout = self.layout()
        index = layout.indexOf(widget)
        row, column, cols, rows = layout.getItemPosition(index)
        self.itemSelected.emit(widget, pos(row, column))

    def onItemSelected(self, widget, pos):
        print "%s at %s" % (widget, pos)

if __name__ == "__main__":
    app = QtGui.QApplication([])
    wnd = Widget()
    wnd.show()
    sys.exit(app.exec_())
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文