【问题标题】:QPrinter, QPrintDialog giving errors not encountered in code examplesQPrinter、QPrintDialog 给出代码示例中未遇到的错误
【发布时间】:2011-06-21 08:18:11
【问题描述】:

在 imageviewer 示例中,QPainter 和 QPrintDialog 对象的定义和使用如下:

#ifndef QT_NO_PRINTER
QPrinter printer;
#endif

QPrintDialog dialog(&printer, this);

然后用 QPrinter(打印机)初始化 QPainter 对象。

当我尝试在我的函数中使用相同的代码时,它看起来像:

void imageviewer::print()
{
...
#ifdef QT_NO_PRINTER

QPrinter printer(this);             //ERROR 1
QPrintDialog dialog(&printer, this);//ERROR 2 and 3

if (dialog.exec())                  //ERROR 4
{
    //do the painting
}

#endif
}

错误是:

1. variable 'QPrinter printer' has initializer but incomplete type
2. 'QPrintDialog' was not declared in this scope
3. Expected ';' before 'dialog'
4. 'dialog' was not declared in this scope

我无法理解的是,为什么在我的代码中使用它们时会出现这些错误,而不是在示例中。

正如一位朋友指出的那样,我确保我使用了正确的#include 文件,并确保在示例中的其他任何地方都没有触及“打印机”和“对话框”。

【问题讨论】:

    标签: qt


    【解决方案1】:

    您在代码中使用#ifdef QT_NO_PRINTER,但示例使用#ifndef QT_NO_PRINTER

    注意if not definedif defined的区别

    如果您的代码可以编译,则意味着您的项目中有 QT_NO_PRINTER。没有打印机就无法打印!

    【讨论】:

    • 两种方式,即使用#ifdef 和#ifndef.. 在后一种情况下,控制不会进入#if-else
    • QT_NO_PRINTER 是一个预处理器指令。您需要将其从项目中删除。如果您的 QT SDK 已使用打印机支持进行编译,您应该没问题。否则,您将收到“QPrinter 无法解析的外部符号”错误。如果你想打印,首先从你的项目中删除这个指令!
    【解决方案2】:
    QPrinter printer(this); 
    

    这是声明一个函数(参见https://en.wikipedia.org/wiki/Most_vexing_parse)。

    你需要写:

    QPrinter printer = QPrinter(this);
    

    或:

    QPrinter printer((this));
    

    【讨论】:

    • 感谢@Alan Birtles 的回答。自从我问这个问题以来已经四年多了,我不再使用 Qt,或者作为一个专业的程序员。但感谢您回答并提醒我这一点。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多