PDA

View Full Version : Print text on the LineEdit



redfox_02
31st December 2016, 11:37
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::on_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.

Nizars
31st December 2016, 14:05
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.




#include <QDebug>
void MainWindow::on_actionPrint_triggered()
{
// actionPrint triggered
qDebug() << "actionPrint triggered."

// instanciate new printer
QPrinter printer;
qDebug() << "Printer instanciated."

// Set name
printer.setPrinterName("Desired printer name");
qDebug() << "Printer name set: " + printer.name;

// Instanciate new QPrintDialog
QPrintDialog dialog(&printer,this);

// Dialog
if(dialog.exec() == QDialog::Rejected)
{
qDebug() << "QDialog rejected!";
return;
}

// get text from LineEdit
QString toPrint = ui->lineEdit->text().toString();
qDebug() << "LineEdit text retreived:\n" + toPrint;

// Print text from LineEdit
toPrint->print(&printer);
qDebug() << "Text print function called.";



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.

redfox_02
31st December 2016, 18:50
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::on_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);

}

anda_skoa
2nd January 2017, 09:07
QPrinter is a QPaintDevice derived class, you can use QPainter to draw on it.
E.g. QPainter::drawText() to draw text.

Cheers,
_