如何在特定单元格中添加列表pyqt4
How to add a list in a particular cell pyqt4
我有一个场景,我有一个 table 并且我想在特定单元格下显示一个列表小部件,我 click.i 正在点击第一列中的单元格并想要仅在第一个单元格上单击时使用。(单元格单击在此处实现)。唯一的问题是现在如何在单元格下显示列表。?!?!?!
初始视图
决赛
我的 Table 代码是:-
from untitled import *
from PyQt4 import QtGui # Import the PyQt4 module we'll need
import sys # We need sys so that we can pass argv to QApplication
import os
from PyQt4.QtGui import *
from PyQt4.QtCore import *
from PyQt4 import QtGui, QtCore
class MainWindow(QMainWindow,Ui_MainWindow):
def __init__(self, parent=None):
QMainWindow.__init__(self, parent)
self.setupUi(self)
self.tableWidget.clicked.connect(self.hello)
def hello(self,item):
#print(item.column())
if item.column()==0 :
print("success")
#I want to add this List at this cell of table
itemN = QtGui.QListWidget()
#hlayout = QtGui.QHBoxLayout()
#hlayout.addWidget(itemN)
#self.setCentralWidget(itemN)
if __name__ == '__main__':
app = QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
如有任何帮助,我们将不胜感激。
谢谢
我的 untitled.py 代码在这里,它在 window
中生成 table
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'untitled.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(800, 600)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.gridLayout = QtGui.QGridLayout(self.centralwidget)
self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
self.tableWidget = QtGui.QTableWidget(self.centralwidget)
self.tableWidget.setObjectName(_fromUtf8("tableWidget"))
self.tableWidget.setColumnCount(4)
self.tableWidget.setRowCount(13)
item = QtGui.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(0, item)
item = QtGui.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(1, item)
item = QtGui.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(2, item)
item = QtGui.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(3, item)
item = QtGui.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(4, item)
item = QtGui.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(5, item)
item = QtGui.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(6, item)
item = QtGui.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(7, item)
item = QtGui.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(8, item)
item = QtGui.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(9, item)
item = QtGui.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(10, item)
item = QtGui.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(11, item)
item = QtGui.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(12, item)
item = QtGui.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(0, item)
item = QtGui.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(1, item)
item = QtGui.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(2, item)
item = QtGui.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(3, item)
self.gridLayout.addWidget(self.tableWidget, 0, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 31))
self.menubar.setObjectName(_fromUtf8("menubar"))
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
item = self.tableWidget.verticalHeaderItem(0)
item.setText(_translate("MainWindow", "New Row", None))
item = self.tableWidget.verticalHeaderItem(1)
item.setText(_translate("MainWindow", "New Row", None))
item = self.tableWidget.verticalHeaderItem(2)
item.setText(_translate("MainWindow", "New Row", None))
item = self.tableWidget.verticalHeaderItem(3)
item.setText(_translate("MainWindow", "New Row", None))
item = self.tableWidget.verticalHeaderItem(4)
item.setText(_translate("MainWindow", "New Row", None))
item = self.tableWidget.verticalHeaderItem(5)
item.setText(_translate("MainWindow", "New Row", None))
item = self.tableWidget.verticalHeaderItem(6)
item.setText(_translate("MainWindow", "New Row", None))
item = self.tableWidget.verticalHeaderItem(7)
item.setText(_translate("MainWindow", "New Row", None))
item = self.tableWidget.verticalHeaderItem(8)
item.setText(_translate("MainWindow", "New Row", None))
item = self.tableWidget.verticalHeaderItem(9)
item.setText(_translate("MainWindow", "New Row", None))
item = self.tableWidget.verticalHeaderItem(10)
item.setText(_translate("MainWindow", "New Row", None))
item = self.tableWidget.verticalHeaderItem(11)
item.setText(_translate("MainWindow", "New Row", None))
item = self.tableWidget.verticalHeaderItem(12)
item.setText(_translate("MainWindow", "New Row", None))
item = self.tableWidget.horizontalHeaderItem(0)
item.setText(_translate("MainWindow", "a", None))
item = self.tableWidget.horizontalHeaderItem(1)
item.setText(_translate("MainWindow", "b", None))
item = self.tableWidget.horizontalHeaderItem(2)
item.setText(_translate("MainWindow", "New Column", None))
item = self.tableWidget.horizontalHeaderItem(3)
item.setText(_translate("MainWindow", "d", None))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
我想你要找的功能是:
self.tableWidget.setCellWidget(rowNum, colNum, itemN)
其中rowNum和colNum是位置,所以0,0会放在左上角的方框内。 itemN 是你制作的QListWidget。
这是一个参考:
http://pyqt.sourceforge.net/Docs/PyQt4/qtablewidget.html#setCellWidget
这是一个相关问题:Adding widgets to qtablewidget pyqt
看了你的截图后,我看到你想要的对象是一个QComboBox,而不是一个QlistWidget。
Link: http://doc.qt.io/qt-4.8/qcombobox.html
您需要使用我提到的原始函数来用组合框而不是 listWidget 填充 table 的元素。然后使用 addItems 用选项填充组合框。然后使用currentText查看用户选择了什么选项。
如果这听起来不像您想要的,您将必须制作具有所需功能的 tableWidget 的自定义版本。那更复杂,我无法在一个答案中涵盖所有内容。但如果您有更多问题,我可以尝试为您指明正确的方向。
为了获取当前单元格的位置,我们使用columnViewportPosition()
和rowViewportPosition()
方法,return行和列的位置x,y分别相对于QTableWidget viewport()
,然后我们使用mapToGlobal()
方法获取全局位置,我们将添加单元格的高度以使其垂直移动。
它创建了一个弹出窗口,因为它将使用 QDialog 来放置 QListWidget,这个小部件 return 是选定的文本。
class PopUp(QDialog):
def __init__(self, labels):
QDialog.__init__(self, None, Qt.Popup | Qt.FramelessWindowHint)
self.itemSelected = ""
self.setLayout(QVBoxLayout())
lWidget = QListWidget(self)
self.layout().addWidget(lWidget)
lWidget.addItems(labels)
lWidget.itemClicked.connect(self.onItemClicked)
self.layout().setContentsMargins(0, 0, 0, 0)
def onItemClicked(self, item):
self.itemSelected = item.text()
self.accept()
def text(self):
return self.itemSelected
class MainWindow(QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
QMainWindow.__init__(self, parent)
self.setupUi(self)
self.tableWidget.clicked.connect(self.onClicked)
def onClicked(self, index):
row = index.row()
column = index.column()
x = self.tableWidget.columnViewportPosition(column)
y = self.tableWidget.rowViewportPosition(row) + self.tableWidget.rowHeight(row)
pos = self.tableWidget.viewport().mapToGlobal(QPoint(x, y))
p = PopUp(["1", "2", "3", "4", "5"])
p.move(pos)
if p.exec_() == QDialog.Accepted:
t_item = QTableWidgetItem(p.text())
self.tableWidget.setItem(row, column, t_item)
截图:
我有一个场景,我有一个 table 并且我想在特定单元格下显示一个列表小部件,我 click.i 正在点击第一列中的单元格并想要仅在第一个单元格上单击时使用。(单元格单击在此处实现)。唯一的问题是现在如何在单元格下显示列表。?!?!?!
初始视图
决赛
from untitled import *
from PyQt4 import QtGui # Import the PyQt4 module we'll need
import sys # We need sys so that we can pass argv to QApplication
import os
from PyQt4.QtGui import *
from PyQt4.QtCore import *
from PyQt4 import QtGui, QtCore
class MainWindow(QMainWindow,Ui_MainWindow):
def __init__(self, parent=None):
QMainWindow.__init__(self, parent)
self.setupUi(self)
self.tableWidget.clicked.connect(self.hello)
def hello(self,item):
#print(item.column())
if item.column()==0 :
print("success")
#I want to add this List at this cell of table
itemN = QtGui.QListWidget()
#hlayout = QtGui.QHBoxLayout()
#hlayout.addWidget(itemN)
#self.setCentralWidget(itemN)
if __name__ == '__main__':
app = QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
如有任何帮助,我们将不胜感激。 谢谢
我的 untitled.py 代码在这里,它在 window
中生成 table# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'untitled.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(800, 600)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.gridLayout = QtGui.QGridLayout(self.centralwidget)
self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
self.tableWidget = QtGui.QTableWidget(self.centralwidget)
self.tableWidget.setObjectName(_fromUtf8("tableWidget"))
self.tableWidget.setColumnCount(4)
self.tableWidget.setRowCount(13)
item = QtGui.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(0, item)
item = QtGui.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(1, item)
item = QtGui.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(2, item)
item = QtGui.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(3, item)
item = QtGui.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(4, item)
item = QtGui.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(5, item)
item = QtGui.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(6, item)
item = QtGui.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(7, item)
item = QtGui.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(8, item)
item = QtGui.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(9, item)
item = QtGui.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(10, item)
item = QtGui.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(11, item)
item = QtGui.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(12, item)
item = QtGui.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(0, item)
item = QtGui.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(1, item)
item = QtGui.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(2, item)
item = QtGui.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(3, item)
self.gridLayout.addWidget(self.tableWidget, 0, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 31))
self.menubar.setObjectName(_fromUtf8("menubar"))
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
item = self.tableWidget.verticalHeaderItem(0)
item.setText(_translate("MainWindow", "New Row", None))
item = self.tableWidget.verticalHeaderItem(1)
item.setText(_translate("MainWindow", "New Row", None))
item = self.tableWidget.verticalHeaderItem(2)
item.setText(_translate("MainWindow", "New Row", None))
item = self.tableWidget.verticalHeaderItem(3)
item.setText(_translate("MainWindow", "New Row", None))
item = self.tableWidget.verticalHeaderItem(4)
item.setText(_translate("MainWindow", "New Row", None))
item = self.tableWidget.verticalHeaderItem(5)
item.setText(_translate("MainWindow", "New Row", None))
item = self.tableWidget.verticalHeaderItem(6)
item.setText(_translate("MainWindow", "New Row", None))
item = self.tableWidget.verticalHeaderItem(7)
item.setText(_translate("MainWindow", "New Row", None))
item = self.tableWidget.verticalHeaderItem(8)
item.setText(_translate("MainWindow", "New Row", None))
item = self.tableWidget.verticalHeaderItem(9)
item.setText(_translate("MainWindow", "New Row", None))
item = self.tableWidget.verticalHeaderItem(10)
item.setText(_translate("MainWindow", "New Row", None))
item = self.tableWidget.verticalHeaderItem(11)
item.setText(_translate("MainWindow", "New Row", None))
item = self.tableWidget.verticalHeaderItem(12)
item.setText(_translate("MainWindow", "New Row", None))
item = self.tableWidget.horizontalHeaderItem(0)
item.setText(_translate("MainWindow", "a", None))
item = self.tableWidget.horizontalHeaderItem(1)
item.setText(_translate("MainWindow", "b", None))
item = self.tableWidget.horizontalHeaderItem(2)
item.setText(_translate("MainWindow", "New Column", None))
item = self.tableWidget.horizontalHeaderItem(3)
item.setText(_translate("MainWindow", "d", None))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
我想你要找的功能是:
self.tableWidget.setCellWidget(rowNum, colNum, itemN)
其中rowNum和colNum是位置,所以0,0会放在左上角的方框内。 itemN 是你制作的QListWidget。
这是一个参考: http://pyqt.sourceforge.net/Docs/PyQt4/qtablewidget.html#setCellWidget
这是一个相关问题:Adding widgets to qtablewidget pyqt
看了你的截图后,我看到你想要的对象是一个QComboBox,而不是一个QlistWidget。 Link: http://doc.qt.io/qt-4.8/qcombobox.html
您需要使用我提到的原始函数来用组合框而不是 listWidget 填充 table 的元素。然后使用 addItems 用选项填充组合框。然后使用currentText查看用户选择了什么选项。
如果这听起来不像您想要的,您将必须制作具有所需功能的 tableWidget 的自定义版本。那更复杂,我无法在一个答案中涵盖所有内容。但如果您有更多问题,我可以尝试为您指明正确的方向。
为了获取当前单元格的位置,我们使用columnViewportPosition()
和rowViewportPosition()
方法,return行和列的位置x,y分别相对于QTableWidget viewport()
,然后我们使用mapToGlobal()
方法获取全局位置,我们将添加单元格的高度以使其垂直移动。
它创建了一个弹出窗口,因为它将使用 QDialog 来放置 QListWidget,这个小部件 return 是选定的文本。
class PopUp(QDialog):
def __init__(self, labels):
QDialog.__init__(self, None, Qt.Popup | Qt.FramelessWindowHint)
self.itemSelected = ""
self.setLayout(QVBoxLayout())
lWidget = QListWidget(self)
self.layout().addWidget(lWidget)
lWidget.addItems(labels)
lWidget.itemClicked.connect(self.onItemClicked)
self.layout().setContentsMargins(0, 0, 0, 0)
def onItemClicked(self, item):
self.itemSelected = item.text()
self.accept()
def text(self):
return self.itemSelected
class MainWindow(QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
QMainWindow.__init__(self, parent)
self.setupUi(self)
self.tableWidget.clicked.connect(self.onClicked)
def onClicked(self, index):
row = index.row()
column = index.column()
x = self.tableWidget.columnViewportPosition(column)
y = self.tableWidget.rowViewportPosition(row) + self.tableWidget.rowHeight(row)
pos = self.tableWidget.viewport().mapToGlobal(QPoint(x, y))
p = PopUp(["1", "2", "3", "4", "5"])
p.move(pos)
if p.exec_() == QDialog.Accepted:
t_item = QTableWidgetItem(p.text())
self.tableWidget.setItem(row, column, t_item)
截图: