PDA

View Full Version : Print a MainWindow



psb2519
11th April 2015, 13:11
I have Created a MainWindow with and Need to print the MainWindow (including all children).
How do I do this?
Are there any examples available?

Thanks

wysota
11th April 2015, 14:41
QWidget::render()

psb2519
12th April 2015, 00:29
I have created the MainWindow with this code


MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
// MainWindow Constructor

ui->setupUi(this);
loadFile();

}

then used the print method as a slot


264 void MainWindow::Print()
265 {
266 NotImplemented();
267
268 QPrinter printer;
269 printer.setOutputFileName("Attenuation_Calculator.pdf");
270 QPrintDialog(&printer).exec();
271 MainWindow.render(&printer);
272
273 }

then error
C2143: syntax error : missing ';' before '.'

why...when the auto fill allows me to select .render as a method of MainWindow?

d_stranz
12th April 2015, 00:45
why...when the auto fill allows me to select .render as a method of MainWindow?

Do you have a member variable of your MainWindow class named "MainWindow"? If not, do you know C++? If the answers are "No" and "Yes", then you can probably figure out why line 271 won't compile.

And learn to use [code ] & [/code ] tags to wrap the code lines when you post source code. (With no spaces after the word "code" in the square brackets).

psb2519
12th April 2015, 02:41
I am new to C++ and Qt....trying very hard to get up to speed.
but I do not understand what you mean d_stranz..

do you have a reference or example of what you mean ?

ChrisW67
12th April 2015, 06:00
There is no object named "MainWindow" in scope where that statement is. There is, however, a type called "MainWindow" so the compiler is expecting an explicit call to method in the MainWindow class, like "MainWindow::staticFunc();", or a declaration, like "MainWindow foobar;", and does not find one before the dot and generates and error. You want neither of these anyway.

Assuming you are trying to print the window corresponding to the object this code is executing in then you should be calling render() on "this" object, i.e:


render(&printer);

psb2519
12th April 2015, 08:07
ChrisW67,

When I use render(&printer);

this causes a LNK2019 error
11071

as per the attachment.

does it make any difference that I am using a .ui file to create the mainwindow?

wysota
12th April 2015, 08:26
You are probably not linking with the printersupport library.

psb2519
12th April 2015, 23:08
How do I overcome this problem ?
and allow the linking to the printersupport library?

wysota
13th April 2015, 06:56
You add QT+=libraryname (e.g. printsupport) to your project file.

d_stranz
14th April 2015, 02:10
You add QT+=libraryname (e.g. printsupport) to your project file.

And you make sure you run qmake to create an updated Makefile with the new library name.

psb2519
15th April 2015, 01:22
All,

I have added
#include <QtPrintSupport/QPrinter>
#include <QtPrintSupport/QPrintDialog>
#include <QPainter>
and I still get the same unresolved external symbol error.
I believe QPainter is required for the render method..is this correct?

What other libraries do I need to add?

wysota
15th April 2015, 06:09
Did you add the QT+=... line?

anda_skoa
15th April 2015, 07:11
I have added
#include <QtPrintSupport/QPrinter>
#include <QtPrintSupport/QPrintDialog>
#include <QPainter>
and I still get the same unresolved external symbol error.

unresolved symbol is a linker error, which means the compiler was already satisfied with the includes before.

Wysota's suggestion is to add the printing module to the project, i.e. editing the project file



What other libraries do I need to add?

Adding includes is not adding libraries. Adding includes makes the compiler aware of types and declarations.
Adding libraries, or in your case a Qt module, is done in the project file.

Cheers,
_

psb2519
15th April 2015, 07:32
Thanks....this has fixed the link problem..
Now I have got to the program compiling and executing.
but when I use the action Print from the menu bar I get
"QPrintDialog: Cannot be used on non-native printers" error
in the Application Output Window of the IDE

Hope this is an easy fix...lol.

wysota
15th April 2015, 08:06
Yeah. Don't use QPrintDialog :)

psb2519
15th April 2015, 09:21
There is an easy fix...

One of the other threads had a comment that the Windows based machines do not handled pdf well.

So I changed the line
printer.setOutputFileName("Filename.pdf");
to
printer.setOutputFileName("Filename");
and the printdialog worked.
I made sure the print to file was not ticked...and whalla...(ozzie slang for great it worked)
a print of what I wanted worked.

Thankyou for the Help to all..it is very much appreciated.

yeye_olive
15th April 2015, 09:46
Wait, if all you wanted was to send the output to a printer, not a file, then why did you call printer.setOutputFileName() in the first place?
Also, the documentation of QPrinter::setOutputFileName() specifically says what happens when the file name ends with ".pdf".