Qt QPrinter,QPrintDialog提供代码示例中未遇到的错误
在imageviewer示例中,QPaint和QPrintDialog对象的定义和使用如下:Qt QPrinter,QPrintDialog提供代码示例中未遇到的错误,qt,Qt,在imageviewer示例中,QPaint和QPrintDialog对象的定义和使用如下: #ifndef QT_NO_PRINTER QPrinter printer; #endif 及 然后使用QPrinter(打印机)初始化QPaint对象 当我尝试在函数中使用相同的代码时,它看起来像: void imageviewer::print() { ... #ifdef QT_NO_PRINTER QPrinter printer(this); //ERROR 1
#ifndef QT_NO_PRINTER
QPrinter printer;
#endif
及
然后使用QPrinter(打印机)初始化QPaint对象
当我尝试在函数中使用相同的代码时,它看起来像:
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
我无法理解的是,为什么在我的代码中使用这些错误时会出现这些错误,而在示例中却没有
正如一位朋友指出的,我确保我使用了正确的“包含文件”,并确保“打印机”和“对话框”在示例中的任何其他地方都没有被触动。您在代码中使用的是\ifdef QT\u NO\u打印机
,但示例使用的是\ifndef QT\u NO\u打印机
注意未定义的与定义的之间的差异
如果您的代码已编译,则意味着您的项目中有QT_NO_打印机。没有打印机就无法打印
QPrinter printer(this);
这是在声明一个函数(请参阅)
你需要写:
QPrinter printer = QPrinter(this);
或:
采用两种方式,即使用#ifdef和#ifndef。。在后一种情况下,控件不输入#if-elseQT_NO_打印机是预处理器指令。您需要将其从项目中删除。如果您的QT-SDK已使用打印机支持进行编译,您应该不会有问题。否则将出现“QPrinter的未解析外部符号”错误。如果要打印,请首先从项目中删除此指令!谢谢@Alan Birtles的回答。我问这个问题已经四年多了,我不再和Qt合作,也不再是专业的程序员了。但是谢谢你的回答和提醒。
QPrinter printer = QPrinter(this);
QPrinter printer((this));