Results 1 to 4 of 4

Thread: Print text on the LineEdit

  1. #1
    Join Date
    Dec 2016
    Posts
    2
    Qt products
    Qt5

    Default Print text on the LineEdit

    Hello everyone I am new to this forum and I enjoy a bit of time with QT 5.7 I've created a first graphical interface, but now I have a problem where I'm stuck printing LineEdit.
    I post the code I was able to print in the textEdit text but if you want to print in a LineEdit simply replacing the previous me error:


    void MainWindow:n_actionPrint_triggered()
    {
    QPrinter printer;
    printer.setPrinterName("desierd printer name");
    QPrintDialog dialog(&printer,this);
    if(dialog.exec() == QDialog::Rejected) return;
    ui->textEdit->print(&printer);
    }


    If as I said now I replace the textEdit the LineEdit me error will be changed because of something that row but do not know how ...

    And also I should print various LineEdit not only so do not know even then how to assemble the various LineEdit because they are printed in the correct position.

    If anyone has a solution thank you in advance.

  2. #2
    Join Date
    Dec 2016
    Posts
    46
    Thanks
    20
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Print text on the LineEdit

    I am not quite sure what you mean. I am a beginner myself but I wonder, can you first get the text from LineEdit and then send it to the printer function? Basically dividing the process into smaller steps to diagnose where the problem occurs.


    Qt Code:
    1. #include <QDebug>
    2. void MainWindow::on_actionPrint_triggered()
    3. {
    4. // actionPrint triggered
    5. qDebug() << "actionPrint triggered."
    6.  
    7. // instanciate new printer
    8. QPrinter printer;
    9. qDebug() << "Printer instanciated."
    10.  
    11. // Set name
    12. printer.setPrinterName("Desired printer name");
    13. qDebug() << "Printer name set: " + printer.name;
    14.  
    15. // Instanciate new QPrintDialog
    16. QPrintDialog dialog(&printer,this);
    17.  
    18. // Dialog
    19. if(dialog.exec() == QDialog::Rejected)
    20. {
    21. qDebug() << "QDialog rejected!";
    22. return;
    23. }
    24.  
    25. // get text from LineEdit
    26. QString toPrint = ui->lineEdit->text().toString();
    27. qDebug() << "LineEdit text retreived:\n" + toPrint;
    28.  
    29. // Print text from LineEdit
    30. toPrint->print(&printer);
    31. qDebug() << "Text print function called.";
    To copy to clipboard, switch view to plain text mode 

    Maybe "QString toPrint" isn't what you should end up using in the end, you might change it to QDocument or something else, I amnot quite sure really since I have never worked with those classes. I just thought that having the qDebug telling you what is going on might help you narrow down your your problem occurs.

  3. #3
    Join Date
    Dec 2016
    Posts
    2
    Qt products
    Qt5

    Default Re: Print text on the LineEdit

    I have tried in many ways but nothing!
    I thought of converting LineEdit in textedit and found a network code, but nn works write it below:

    QString s = lineEdit->text();
    QTextEdit textEdit;
    textEdit.setPlainText(s);

    but maybe something is missing or is that wrong.


    Added after 1 29 minutes:


    I solved by converting LineEdit in textedit and it works, now prints two LineEdit but though in two different pages and always to top.
    How can I print LineEdit the same page and in the right position you are in the interface graphics ??

    If someone politely knows how to do mail me the code. I write the code that found so far:


    void MainWindow:n_actionPrint_triggered()
    {
    QPrinter printer;

    QPrintDialog dialog(&printer, this);
    if(dialog.exec() == QDialog::Rejected) return;

    QString s;
    QString t;

    s = ui->lineEdit->text();
    t = ui->lineEdit_17->text();

    QTextEdit textEdit5;
    QTextEdit textEdit6;

    textEdit5.setPlainText(s);
    textEdit6.setPlainText(t);

    textEdit5.print(&printer);
    textEdit6.print(&printer);

    }
    Last edited by redfox_02; 31st December 2016 at 22:12.

  4. #4
    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 text on the LineEdit

    QPrinter is a QPaintDevice derived class, you can use QPainter to draw on it.
    E.g. QPainter::drawText() to draw text.

    Cheers,
    _

Similar Threads

  1. Replies: 2
    Last Post: 25th September 2014, 01:24
  2. Replies: 7
    Last Post: 5th February 2014, 11:20
  3. how to get the text from a LineEdit using QT??
    By Soumya Somasekhar Ram in forum Qt Programming
    Replies: 11
    Last Post: 11th September 2013, 13:43
  4. send text of lineedit
    By akhilteja in forum Newbie
    Replies: 1
    Last Post: 23rd August 2013, 09:53
  5. print text in the lineEdit control from string
    By rk0747 in forum Qt Programming
    Replies: 2
    Last Post: 6th February 2010, 08:48

Tags for this Thread

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.