QPrinter, QPrintDialog giving errors not encountered in code examples
In the imageviewer example, QPainter and QPrintDialog objects are defined and used as following:
#ifndef QT_NO_PRINTER
QPrinter printer;
#endif
and
QPrintDialog dialog(&printer, this);
A QPainter object is then initialized with QPrinter (printer).
When I tried to use the same code in my function, it looks like:
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
}
The errors are:
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
What I am not able to understand is why are these errors arising when I am using them in my code, but not in the example.
As a friend pointed out, I made sure that I used the right #include files and made sure that 'printer' and 'dialog' were not touched anywhere else in the example.
You are using #ifdef QT_NO_PRINTER
in your code but the example uses #ifndef QT_NO_PRINTER
Pay attention to difference between if not defined and if defined
If your code compiles then it means that you have QT_NO_PRINTER in your project. Without a printer you can not print !
QPrinter printer(this);
This is declaring a function (see https://en.wikipedia.org/wiki/Most_vexing_parse).
You need to write:
QPrinter printer = QPrinter(this);
or:
QPrinter printer((this));
精彩评论