【问题标题】:Adding widgets to qtablewidget pyqt将小部件添加到 qtablewidget pyqt
【发布时间】:2012-08-14 02:23:11
【问题描述】:

在 qtablewidget 中有没有像按钮一样添加?但是单元格内的日期仍然必须显示,例如,如果用户双击一个单元格,我可以像按钮一样发送信号吗?谢谢!

edititem():

def editItem(self,clicked):
    if clicked.row() == 0:
        #go to tab1
    if clicked.row() == 1:
        #go to tab1
    if clicked.row() == 2:
        #go to tab1
    if clicked.row() == 3:
        #go to tab1

表触发器:

self.table1.itemDoubleClicked.connect(self.editItem)

【问题讨论】:

    标签: python pyqt qtablewidget


    【解决方案1】:

    您有几个问题合二为一...简短的回答,是的,您可以向 QTableWidget 添加一个按钮 - 您可以通过调用 setCellWidget 将任何小部件添加到表格小部件:

    # initialize a table somehow
    table = QTableWidget(parent)
    table.setRowCount(1)
    table.setColumnCount(1)
    
    # create an cell widget
    btn = QPushButton(table)
    btn.setText('12/1/12')
    table.setCellWidget(0, 0, btn)
    

    但这听起来不像你真正想要的。

    听起来你想对用户双击你的一个单元格做出反应,就好像他们点击了一个按钮,大概是为了打开一个对话框或编辑器或其他东西。

    如果是这种情况,您真正需要做的就是从 QTableWidget 连接到 itemDoubleClicked 信号,如下所示:

    def editItem(item):
        print 'editing', item.text()    
    
    # initialize a table widget somehow
    table = QTableWidget(parent)
    table.setRowCount(1)
    table.setColumnCount(1)
    
    # create an item
    item = QTableWidgetItem('12/1/12')
    table.setItem(0, 0, item)
    
    # if you don't want to allow in-table editing, either disable the table like:
    table.setEditTriggers( QTableWidget.NoEditTriggers )
    
    # or specifically for this item
    item.setFlags( item.flags() ^ Qt.ItemIsEditable)
    
    # create a connection to the double click event
    table.itemDoubleClicked.connect(editItem)
    

    【讨论】:

    • 如何使该行在用户单击时突出显示该行而不是单个单元格。
    • 做:table.setSelectionBehavior(QTableWidget.SelectRows)
    • 另外,当点击某一行时,我是否可以重定向到不同的选项卡?我的问题是我基本上可以重定向到不同的标签,有点像 html 网页上的超链接。
    • 单击按钮时重定向或移动到选项卡?任何想法。
    • 您将其设置在错误的小部件上 - QWidget 没有该属性。我假设如果您尝试设置选项卡,那么在某个地方您有一个选项卡小部件 - 您必须在此处设置索引值。 qt-project.org/doc/qt-4.8/qtabwidget.html 无意冒犯,但我已经回答了您的问题,您现在提出了一个新问题 - 如果您仍然无法处理更多代码示例并且不继续将这个问题推向新主题,我会发布该问题。
    【解决方案2】:

    在 PyQt4 中添加按钮到 qtablewidget :

    btn= QtGui.QPushButton('Hello')
    qtable_name.setCellWidget(0,0, btn) # qtable_name is your qtablewidget name
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-16
      • 1970-01-01
      • 2011-07-30
      • 1970-01-01
      • 2017-10-31
      • 1970-01-01
      • 1970-01-01
      • 2012-02-05
      相关资源
      最近更新 更多