将小部件添加到 qtablewidget pyqt [英] Adding widgets to qtablewidget pyqt

查看:46
本文介绍了将小部件添加到 qtablewidget pyqt的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is there anyway to add like a button in qtablewidget? But the date within the cell would stil have to be displaying, for example if an user double clicked a cell, could i send a signal like a button? Thanks!

edititem():

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)

推荐答案

您将几个问题合并为一个...简而言之,是的,您可以向 QTableWidget 添加按钮 - 您可以向 QTableWidget 添加任何小部件通过调用 setCellWidget 的表格小部件:

You have a couple of questions rolled into one...short answer, yes, you can add a button to a QTableWidget - you can add any widget to the table widget by calling 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)

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

But that doesn't sound like what you actually want.

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

It sounds like you want to react to a user double-clicking one of your cells, as though they clicked a button, presumably to bring up a dialog or editor or something.

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

If that is the case, all you really need to do is connect to the itemDoubleClicked signal from the QTableWidget, like so:

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)

这篇关于将小部件添加到 qtablewidget pyqt的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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