2

在 Qt Desktop 应用程序中,如何在应用程序运行之间保持 QPrinter 选择?

考虑以下代码:

printDialog = new QPrintDialog();
if (printDialog->exec() == QDialog::Accepted) {
    qDebug() << printDialog->printer(); // Gives just a pointer value,
                                        // no real use for serializing.
    view->print(printDialog->printer());
}

我希望能够做到这两点:

printDialog = new QPrintDialog("some-previously stored settings") 

并且,甚至不调用 QPrintDialog,只需像这样打印到预先选择的打印机:

view->print(new QPrinter("some previously stored settings"))

有一个处理 QPrinterInfo 的函数,但它只返回打印机名称。还有其他建议吗?

4

2 回答 2

2

你被 Qt 冷落了。您要求的功能不可用。相关 Qt 功能请求(观察请求的年龄和优先级):

QPrinter:添加 << 和 >> 运算符,以便可以保存和加载设置

QPrinter 应该提供一种存储/恢复设置的方法

从当前的 Qt 版本开始,您必须手动将相关的 QPrintDialog 和 QPrinter 值保存/加载到 QSettings(或可序列化为文件)。从两个提到的 QTBUG 中最后一个有用的评论中猜测,Qt 5.0 没有任何改进。

于 2012-09-04T12:23:07.607 回答
2

我编写了以下代码来读取 QPrinter 的所有可用设置并将其写入 QDataStream。不幸的是,到目前为止,这并不能保存/恢复所有可用的打印机选项(即在 Windows 上)。但打印机名称、页面大小等已正确恢复。

随意重复使用。

    ///////////////////////////////////////////////////////////////////////////////
    // Write all available Attributes from QPrinter into stream
    ///////////////////////////////////////////////////////////////////////////////

    template <typename t> void  writeStreamElement(QDataStream &os, t param)
    {
        int i = static_cast<int>(param);
        os << i;
    }
    template <>           void writeStreamElement<QString>(QDataStream &os, QString s)
    {
        os << s;
    }

    QDataStream& operator<<(QDataStream &os, const QPrinter &printer)
    {
        writeStreamElement(os, printer.printerName         ());
        writeStreamElement(os, printer.pageSize            ());
        writeStreamElement(os, printer.collateCopies       ());
        writeStreamElement(os, printer.colorMode           ());
        writeStreamElement(os, printer.copyCount           ());
        writeStreamElement(os, printer.creator             ());
        writeStreamElement(os, printer.docName             ());
        writeStreamElement(os, printer.doubleSidedPrinting ());
        writeStreamElement(os, printer.duplex              ());
        writeStreamElement(os, printer.fontEmbeddingEnabled());
        writeStreamElement(os, printer.fullPage            ());
        writeStreamElement(os, printer.orientation         ());
        writeStreamElement(os, printer.outputFileName      ());
        writeStreamElement(os, printer.outputFormat        ());
        writeStreamElement(os, printer.pageOrder           ());
        writeStreamElement(os, printer.paperSize           ());
        writeStreamElement(os, printer.paperSource         ());
        writeStreamElement(os, printer.printProgram        ());
        writeStreamElement(os, printer.printRange          ());
        writeStreamElement(os, printer.printerName         ());
        writeStreamElement(os, printer.resolution          ());
        writeStreamElement(os, printer.winPageSize         ());

        qreal left, top, right, bottom;
        printer.getPageMargins(&left, &top, &right, &bottom, QPrinter::Millimeter);
        os << left << top << right << bottom;

        Q_ASSERT_X(os.status() == QDataStream::Ok, __FUNCTION__, QString("Stream status = %1").arg(os.status()).toStdString().c_str());
        return os;
    }
    ///////////////////////////////////////////////////////////////////////////////
    // Read all available Attributes from tream into QPrinter
    ///////////////////////////////////////////////////////////////////////////////


    template <typename t> t readStreamElement(QDataStream &is)
    {
        int i;
        is >> i;
        return static_cast<t>(i);
    }
    template <> QString readStreamElement<QString>(QDataStream &is)
    {
        QString s;
        is >> s;
        return s;
    }

    QDataStream& operator>>(QDataStream &is,  QPrinter &printer)
    {

        printer.setPrinterName              (readStreamElement<QString>                (is));
        printer.setPageSize                 (readStreamElement<QPrinter::PaperSize>    (is));
        printer.setCollateCopies            (readStreamElement<bool>                   (is));
        printer.setColorMode                (readStreamElement<QPrinter::ColorMode>    (is));
        printer.setCopyCount                (readStreamElement<int>                    (is));
        printer.setCreator                  (readStreamElement<QString>                (is));
        printer.setDocName                  (readStreamElement<QString>                (is));
        printer.setDoubleSidedPrinting      (readStreamElement<bool>                   (is));
        printer.setDuplex                   (readStreamElement<QPrinter::DuplexMode>   (is));
        printer.setFontEmbeddingEnabled     (readStreamElement<bool>                   (is));
        printer.setFullPage                 (readStreamElement<bool>                   (is));
        printer.setOrientation              (readStreamElement<QPrinter::Orientation>  (is));
        printer.setOutputFileName           (readStreamElement< QString >              (is));
        printer.setOutputFormat             (readStreamElement<QPrinter::OutputFormat> (is));
        printer.setPageOrder                (readStreamElement<QPrinter::PageOrder>    (is));
        printer.setPaperSize                (readStreamElement<QPrinter::PaperSize>    (is));
        printer.setPaperSource              (readStreamElement<QPrinter::PaperSource>  (is));
        printer.setPrintProgram             (readStreamElement<QString>                (is));
        printer.setPrintRange               (readStreamElement<QPrinter::PrintRange>   (is));
        printer.setPrinterName              (readStreamElement<QString>                (is));
        printer.setResolution               (readStreamElement<int>                    (is));
        printer.setWinPageSize              (readStreamElement<int>                    (is));

        qreal left, top, right, bottom;
        is >> left >> top >> right >> bottom;

        printer.setPageMargins(left, top, right, bottom, QPrinter::Millimeter);

        Q_ASSERT_X(is.status() == QDataStream::Ok, __FUNCTION__, QString("Stream status = %1").arg(is.status()).toStdString().c_str());

        return is;
    }


    ///////////////////////////////////////////////////////////////////////////////
    // persist settings
    ///////////////////////////////////////////////////////////////////////////////
    QPrinter *sgPrinter =...;
...
    QByteArray byteArr;
    QDataStream os(&byteArr, QIODevice::WriteOnly);
    os << *sgPrinter;
    QSettings settings("justMe", "myApp"));
    settings.setValue("printerSetup", byteArr.toHex());

    ///////////////////////////////////////////////////////////////////////////////
    // restore settings
    ///////////////////////////////////////////////////////////////////////////////
    QByteArray printSetUp = settings.value("printerSetup").toByteArray();
    printSetUp = QByteArray::fromHex(printSetUp);
    QDataStream is(&printSetUp, QIODevice::ReadOnly);
    is >> *sgPrinter;
于 2013-02-20T12:04:25.687 回答