如何从我的destop应用程序打印QTableWidget并将单元格调整为文本的长度?该表可由用户食用,并且包含文本,单词,图片和下拉菜单?
我尝试使用下面的代码来执行此操作,但是它以“进程结束,退出代码为-1073740791(0xC0000409)”退出
我正在使用python 3.6和pyqt5。
from PyQt5 import QtWidgets, QtCore, QtPrintSupport, QtGui
from PyQt5.QtWidgets import *
list_a =['word_a 1', 'word_a 2']
list_b =['word_b 1 ', 'word_b 2']
list_c =['word_c 1', 'word_c 2']
combo_box_options = ["Option 1","Option 2","Option 3"]
list_d = ['good','bad']
data = {'Wort A':list_a, 'Wort B':list_b, 'Wort C': list_c, 'Dropdown': [], 'Word D': list_d}
class Window(QTabWidget):
def __init__(self):
QtWidgets.QWidget.__init__(self)
super().__init__()
self.setWindowTitle(self.tr('Document Printer'))
self.table = QtWidgets.QTableWidget()
self.table.setRowCount(5)
self.table.setColumnCount(5)
horHeaders = []
for col, key in enumerate(sorted(data.keys())):
horHeaders.append(key)
for row, item in enumerate(data[key]):
newitem = QTableWidgetItem(item)
newitem.setTextAlignment(QtCore.Qt.AlignCenter)
self.table.setItem(row, col, newitem)
combo_attr = ['bad word', 'good word', 'very nice word', 'delet']
i = 0
for j in horHeaders:
comboBox = QtWidgets.QComboBox()
for t in combo_attr:
comboBox.addItem(t)
self.table.setCellWidget(i,3,comboBox)
i += 1
self.table.setHorizontalHeaderLabels(
'Word A|Word B|Word C|Dropdown|Word D'.split('|'))
self.buttonPrint = QtWidgets.QPushButton('Print', self)
self.buttonPrint.clicked.connect(self.handlePrint)
self.buttonPreview = QtWidgets.QPushButton('Preview', self)
self.buttonPreview.clicked.connect(self.handlePreview)
layout = QtWidgets.QGridLayout(self)
layout.addWidget(self.table, 0, 0, 1, 2)
layout.addWidget(self.buttonPrint, 1, 0)
layout.addWidget(self.buttonPreview, 1, 1)
def handlePrint(self):
dialog = QtPrintSupport.QPrintDialog()
if dialog.exec_() == QtWidgets.QDialog.Accepted:
self.handlePaintRequest(dialog.printer())
def handlePreview(self):
dialog = QtPrintSupport.QPrintPreviewDialog()
dialog.paintRequested.connect(self.handlePaintRequest)
dialog.exec_()
def handlePaintRequest(self, printer):
document = QtGui.QTextDocument()
cursor = QtGui.QTextCursor(document)
table = cursor.insertTable(
self.table.rowCount(), self.table.columnCount())
for row in range(table.rows()):
print(row)
for col in range(table.columns()):
print(col)
cursor.insertText(self.table.newitem(row, col).text())
cursor.movePosition(QtGui.QTextCursor.NextCell)
document.print_(printer)
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
window = Window()
window.resize(640, 480)
window.show()
sys.exit(app.exec_())
答案 0 :(得分:2)
首先,我建议如果程序崩溃而没有给您太多信息,那么您必须在终端或cmd中运行它。如果执行此操作,则会收到以下消息:
AttributeError: 'QTableWidget' object has no attribute 'newitem'
问题很明显:QTableWidget没有此方法,部分解决方案是按项目更改newitem,但这也会带来问题,因为某些项目不存在,因此必须首先进行验证:
def handlePaintRequest(self, printer):
document = QtGui.QTextDocument()
cursor = QtGui.QTextCursor(document)
table = cursor.insertTable(self.table.rowCount(), self.table.columnCount())
for row in range(table.rows()):
for col in range(table.columns()):
it = self.table.item(row, col)
if it is not None:
cursor.insertText(it.text())
cursor.movePosition(QtGui.QTextCursor.NextCell)
document.print_(printer)
打印QTableWidget并没有得到普遍的回应,因为当您指向一个小部件时,您必须根据所使用的小部件来修改代码,例如,在这种情况下,我将创建一个根据以下内容返回文本的函数:嵌入式小部件:
def handlePaintRequest(self, printer):
document = QtGui.QTextDocument()
cursor = QtGui.QTextCursor(document)
table = cursor.insertTable(self.table.rowCount(), self.table.columnCount())
for row in range(table.rows()):
for col in range(table.columns()):
w = self.table.cellWidget(row, col)
it = self.table.item(row, col)
if w is not None:
cursor.insertText(get_text_from_widget(w))
elif it is not None:
cursor.insertText(it.text())
cursor.movePosition(QtGui.QTextCursor.NextCell)
document.print_(printer)
def get_text_from_widget(w):
t = ""
if isinstance(w, QtWidgets.QComboBox):
t = w.currentText()
# if isinstance(w, another_widget):
# t = w.some_method()
return t