Results 1 to 18 of 18

Thread: Print a MainWindow

  1. #1
    Join Date
    Apr 2015
    Location
    Sydney
    Posts
    9
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Smile Print a MainWindow

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Print a MainWindow

    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Apr 2015
    Location
    Sydney
    Posts
    9
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Print a MainWindow

    I have created the MainWindow with this code
    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. // MainWindow Constructor
    6.  
    7. ui->setupUi(this);
    8. loadFile();
    9.  
    10. }
    To copy to clipboard, switch view to plain text mode 
    then used the print method as a slot
    Qt Code:
    1. 264 void MainWindow::Print()
    2. 265 {
    3. 266 NotImplemented();
    4. 267
    5. 268 QPrinter printer;
    6. 269 printer.setOutputFileName("Attenuation_Calculator.pdf");
    7. 270 QPrintDialog(&printer).exec();
    8. 271 MainWindow.render(&printer);
    9. 272
    10. 273 }
    To copy to clipboard, switch view to plain text mode 
    then error
    C2143: syntax error : missing ';' before '.'

    why...when the auto fill allows me to select .render as a method of MainWindow?
    Last edited by anda_skoa; 12th April 2015 at 08:36. Reason: missing [code] tags

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Print a MainWindow

    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).

  5. #5
    Join Date
    Apr 2015
    Location
    Sydney
    Posts
    9
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Print a MainWindow

    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 ?

  6. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Print a MainWindow

    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:
    Qt Code:
    1. render(&printer);
    To copy to clipboard, switch view to plain text mode 
    "We can't solve problems by using the same kind of thinking we used when we created them." -- Einstein
    If you are posting code then please use [code] [/code] tags around it - makes addressing the problem easier.

  7. #7
    Join Date
    Apr 2015
    Location
    Sydney
    Posts
    9
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Print a MainWindow

    ChrisW67,

    When I use render(&printer);

    this causes a LNK2019 error
    Print method.jpg

    as per the attachment.

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

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Print a MainWindow

    You are probably not linking with the printersupport library.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Apr 2015
    Location
    Sydney
    Posts
    9
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Print a MainWindow

    How do I overcome this problem ?
    and allow the linking to the printersupport library?

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Print a MainWindow

    You add QT+=libraryname (e.g. printsupport) to your project file.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #11
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Print a MainWindow

    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.

  12. #12
    Join Date
    Apr 2015
    Location
    Sydney
    Posts
    9
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Print a MainWindow

    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?

  13. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Print a MainWindow

    Did you add the QT+=... line?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  14. #14
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Print a MainWindow

    Quote Originally Posted by psb2519 View Post
    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

    Quote Originally Posted by psb2519 View Post
    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,
    _

  15. #15
    Join Date
    Apr 2015
    Location
    Sydney
    Posts
    9
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Print a MainWindow

    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.

  16. #16
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Print a MainWindow

    Yeah. Don't use QPrintDialog
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  17. #17
    Join Date
    Apr 2015
    Location
    Sydney
    Posts
    9
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Smile Re: Print a MainWindow

    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.

  18. #18
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Print a MainWindow

    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".

Similar Threads

  1. Replies: 0
    Last Post: 6th November 2011, 09:22
  2. Replies: 1
    Last Post: 12th April 2011, 09:53
  3. how to print a value in qt
    By qt_user in forum Qt Programming
    Replies: 5
    Last Post: 6th August 2010, 21:17
  4. qwt print
    By manmohan in forum Qwt
    Replies: 0
    Last Post: 5th May 2010, 07:16
  5. Cancelling Long-running Print/Print Preview
    By ChrisW67 in forum Newbie
    Replies: 4
    Last Post: 16th June 2009, 23:05

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.