2012-02-24 46 views
3

我正在寫一個應用程序,可以將地址直接打印到信封上。我正在使用QTextDocument,問題是它的方法print()添加頁碼,這在信封中是不正確的。QTextDocument中用於信封的頁碼

一些代碼,如果你需要它:

void MainWindow::print() 
{ 
    QString addressText = textEdit->document()->toPlainText(); 
    envelopeDocument = new QTextDocument(this); 
    printer.setResolution(QPrinter::HighResolution); 
    printer.setPrinterName("OKI B6200(PCL6)"); 
    printer.setOrientation(QPrinter::Landscape); 
    QFont font("Trebuchet MS"); 
    switch (envelopeComboBox->currentIndex()){ 
    case 0: 
     font.setPointSize(12); 
     envelopeDocument->setDefaultFont(font); 
     envelopeDocument->setPlainText(addressText); 
     printer.setPaperSize(QSizeF(114,225),QPrinter::Millimeter); 
     printer.setPageMargins(120,60,20,15,QPrinter::Millimeter); 
     break; 
    case 1: 
     font.setPointSize(14); 
     envelopeDocument->setDefaultFont(font); 
     envelopeDocument->setPlainText(addressText); 
     printer.setPaperSize(QSizeF(184,262),QPrinter::Millimeter); 
     printer.setPageMargins(140,100,20,20,QPrinter::Millimeter); 
     break; 
    case 2: 
     font.setPointSize(16); 
     envelopeDocument->setDefaultFont(font); 
     envelopeDocument->setPlainText(addressText); 

     printer.setPaperSize(QSizeF(227,324), QPrinter::Millimeter); 
     printer.setPageMargins(170,120,30,40,QPrinter::Millimeter); 
     break; 
    } 

    QPrintPreviewDialog preview (&printer,this); 
    preview.setWindowFlags(Qt::Window); 
    connect(&preview, SIGNAL(paintRequested(QPrinter *)),SLOT(printPreview(QPrinter *))); 
    preview.exec(); 
} 

void MainWindow::printPreview(QPrinter *p) 
{ 
    envelopeDocument->print(p); 
} 

謝謝!

回答

7

你可以試試下面的代碼,它可以幫助你......

QPrinter printer(QPrinter::ScreenResolution); 
printer.setPaperSize(QPrinter::A4); 
printer.setOutputFormat(QPrinter::PdfFormat); 
printer.setOutputFileName(fileName); 
// printer.setPageMargins(0.925, 0.8, 0.5, 0.8, QPrinter::Inch); 

QSizeF paperSize; 
paperSize.setWidth(printer.width()); 
paperSize.setHeight(printer.height()); 
document->setHtml(html); 
document->setPageSize(paperSize); // the document needs a valid PageSize 
document->print(&printer); 

當你指的print()的源代碼,那麼你會認識到,當沒有有效QTextDocument.pageSize()QPointF pageNumberPos只被定義。 如果pageNumberPos不爲空,則printPage()頁碼將被打印。 因此,只需設置有效的QTextDocumtent.pageSize(),並且您的打印文檔上沒有頁碼。

+0

thanx,我會試試這個,回到你身邊... – shofee 2012-02-24 11:53:55

+0

thanx ken它爲我工作... – shofee 2012-02-24 11:56:09