如何修改此代码以自动打印或将默认设置设置为横向?
我是PyQt5的新手。尝试制作具有打印功能的应用程序。我从互联网上复制并粘贴了此代码,但默认设置为纵向。自动横向打印很重要。
from PyQt5 import QtWidgets, QtCore, QtPrintSupport, QtGui
class Window(QtWidgets.QWidget):
def __init__(self):
QtWidgets.QWidget.__init__(self)
self.setWindowTitle(self.tr('Document Printer'))
self.table = QtWidgets.QTableWidget(200, 5, self)
for row in range(self.table.rowCount()):
for col in range(self.table.columnCount()):
item = QtWidgets.QTableWidgetItem('(%d, %d)' % (row, col))
item.setTextAlignment(QtCore.Qt.AlignCenter)
self.table.setItem(row, col, item)
self.table.setHorizontalHeaderLabels(
'SKU #|NAME|DESCRIPTION|QUANTITY|PRICE'.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 = self.makeTableDocument()
document.print_(printer)
def makeTableDocument(self):
document = QtGui.QTextDocument()
cursor = QtGui.QTextCursor(document)
rows = self.table.rowCount()
columns = self.table.columnCount()
table = cursor.insertTable(rows + 1, columns)
format = table.format()
format.setHeaderRowCount(1)
table.setFormat(format)
format = cursor.blockCharFormat()
format.setFontWeight(QtGui.QFont.Bold)
for column in range(columns):
cursor.setCharFormat(format)
cursor.insertText(
self.table.horizontalHeaderItem(column).text())
cursor.movePosition(QtGui.QTextCursor.NextCell)
for row in range(rows):
for column in range(columns):
cursor.insertText(
self.table.item(row, column).text())
cursor.movePosition(QtGui.QTextCursor.NextCell)
return document
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
window = Window()
window.resize(640, 480)
window.show()
sys.exit(app.exec_())
有人知道吗?还是有一段代码可以在pyqt5中进行横向打印?谢谢!
答案 0 :(得分:0)
QPrinter :: Orientation
此枚举类型(不要与Orientation混淆)用于指定每个页面的方向。
var url = 'https://example.com/profile';
var data = {username: 'example'};
fetch(url, {
method: 'POST', // or 'PUT'
body: JSON.stringify(data), // data can be `string` or {object}!
headers:{
'Content-Type': 'application/json'
}
}).then(res => res.json())
.then(response => console.log('Success:', JSON.stringify(response)))
.catch(error => console.error('Error:', error));