PyQT4:在Qtableview中添加组合框 [英] PyQT4: Adding combobox in Qtableview

查看:3628
本文介绍了PyQT4:在Qtableview中添加组合框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是PyQT的新手。



我有兴趣为tableView的每一行添加一个combobox。可能在PyQT 4?



我知道,这是可能在QT5,但不知道PyQT。



< >

这需要使用 /pyqt.sourceforge.net/Docs/PyQt4/qtableview.htmlrel =nofollow noreferrer> QTableView ,或者您可以使用 QTableWidget



假设您可以使用窗口小部件和视图,轻松地向单元格添加组合框(或任何小部件)。



< hr>

  class MainWindow(QtGui.QMainWindow):
def __init __(self,parent = None):
QtGui。 QMainWindow .__ init __(self,parent)
self.table = QtGui.QTableWidget()
self.table.setColumnCount(3)
self.setCentralWidget(self.table)
data1 = ['row1','row2','row3','row4']
data2 = ['1','2.0','3.00000001','3.9999999']
combo_box_options = 1,选项2,选项3]

self.table.setRowCount(4)

用于范围(4)中的索引:
item1 = QtGui.QTableWidgetItem(data1 [index])
self.table.setItem(index,0,item1)
item2 = QtGui.QTableWidgetItem(data2 [index])
self.table.setItem (index,1,item2)
combo = QtGui.QComboBox()
在combo_box_options中的t:
combo.addItem(t)
self.table.setCellWidget ,combo)






这里的重要部分是: / p>

  combo_box_options = [选项1,选项2,选项3] 

这是要保存的值的列表。在这个例子中,有三个选项。

 用于combo_box_options中的t:
combo.addItem(t)
self.table.setCellWidget(index,2,combo)

此块设置组合框, ,然后将其添加到单元格(本示例中的最后一个)。



上面的代码块生成如下:




I am new to PyQT.

I am interested to add a combobox to the each row of tableView. Is it possible in PyQT 4?

I know, it is possible in QT5, but not sure about PyQT.

Thank you in advance for help.

解决方案

Does this need to be done using a QTableView or can you do it using a QTableWidget?

Making the assumption that you can use the Widget vs the View, you can easily add a combobox (or any widget) to a cell.


class MainWindow(QtGui.QMainWindow):
    def __init__(self, parent=None):
        QtGui.QMainWindow.__init__(self,parent)
        self.table = QtGui.QTableWidget()
        self.table.setColumnCount(3)
        self.setCentralWidget(self.table)
        data1 = ['row1','row2','row3','row4']
        data2 = ['1','2.0','3.00000001','3.9999999']
        combo_box_options = ["Option 1","Option 2","Option 3"]

        self.table.setRowCount(4)

        for index in range(4):
            item1 = QtGui.QTableWidgetItem(data1[index])
            self.table.setItem(index,0,item1)
            item2 = QtGui.QTableWidgetItem(data2[index])
            self.table.setItem(index,1,item2)
            combo = QtGui.QComboBox()
            for t in combo_box_options:
                combo.addItem(t)
            self.table.setCellWidget(index,2,combo)


The important parts here are:

combo_box_options = ["Option 1","Option 2","Option 3"]

This is the list of values you want your combobox to hold. In this example, there are three options.

for t in combo_box_options:
    combo.addItem(t)
self.table.setCellWidget(index,2,combo)

This block sets up the combobox, per row, and then adds it to a cell (the last one in this example).

The code block above produces out put like this:

这篇关于PyQT4:在Qtableview中添加组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆