我在QML中弹出一个打印按钮,上面的代码将我的矩形打印为QImage:
myqml.qml
RoundButton {
id: roundButton1
Text {
anchors.fill: parent
color: "white"
text: "print"
}
onPressed: {
var stat = rectangle.grabToImage(function(result) {
Serv.print(result.image); //result.image holds the QVariant
});
console.log("Success: ", stat);
}
}
serv.cpp
void Serv::print(QVariant data) {
QImage img = qvariant_cast < QImage > (data);
QPrinter printer(QPrinter::HighResolution);
QPrintDialog dialog( & printer, 0);
if (dialog.exec() != QDialog::Accepted)
return;
QPainter painter( & printer);
painter.drawImage(QPoint(50, 50), img);
painter.end();
}
它打印我的矩形,但是第一次我单击“打印”按钮时,对话框窗口没有显示,而是在后面运行,但是直到再次单击该按钮时我才看到它。这是什么问题? >
我使用Qt 5.9.1