cmd.exe to open pdf and print it - c++

I am using Qt4.8, What I want is open a pdf and print that pdf automatically through cmd.exe, without clicking on print button in pdf reader by using QProcess:
I have two different code that do two different task:
Opne Pdf
QString scmd= "cmd.exe";
list.push_back("/C");
list.push_back("test.pdf");
Process.start(scmd, list);
Sleep(2000);
Print pdf without open it
QString scmd2 = "C:/Program Files (x86)/Adobe/Reader 11.0/Reader/AcroRd32.exe.exe"
list2.push_back("/t");
list2.push_back("test.pdf");
Process.start(scmd2, list2);
Sleep(2000);
So I want to merge this command, I dont know how I can do that? Please suggest me something

You can fetch all information from HKEY_CLASSES_ROOT of windows registry.
Here is a example how to fetch default path to printing software and how to run it. I tested it on Qt 5.7
#include <QSettings>
#include <QProcess>
#include <QDebug>
int main(int argc, char *argv[])
{
const QString classesRoot = "HKEY_CLASSES_ROOT";
// get ID of .pdf extension
QSettings pdfSettings(classesRoot + "\\.pdf", QSettings::NativeFormat);
QString pdfId = pdfSettings.value("Default").toString();
// get path to default program that associated with PDF files
QString printPath = QSettings(classesRoot + "\\" + pdfId + "\\shell\\print\\command", QSettings::NativeFormat).value("Default").toString();
QString openPath = QSettings(classesRoot + "\\" + pdfId + "\\shell\\open\\command", QSettings::NativeFormat).value("Default").toString();
qDebug() << "print path" << printPath;
qDebug() << "open path" << openPath;
// open .pdf file
QProcess::startDetached(openPath.arg("full path to pdf file.pdf") );
// print .pdf file
QProcess printProcess;
printProcess.start(printPath.arg("full path to pdf file.pdf") );
printProcess.waitForFinished(-1);
return 0;
}

Related

In qt QFileDialog setsuffix is not working in linux, how to solve?

I am working on a Save dialog for my qt app. Everything works, but if no file extension is added behind the filename, it won't automatically be saved with the file extension although the filter is selected.
I know i need to set a defaultsuffix option, but even if i do, then it still won't add the extension automatically if its not given.
I found several other similar questions, where i read it works in windows but it could fail on linux distro's. If so, is there a simple workaround? Because right now, i don't have a working solution...
void MainWindow::on_actionSave_Chart_As_triggered()
{
QFileDialog *fileDialog = new QFileDialog;
fileDialog->setDefaultSuffix("files (*);;AstroQt aqt (*.aqt)");
QString fileName = fileDialog->getSaveFileName(this, "Save Radix", ui->label_2->text() +".aqt", "AstroQT(*.aqt)");
qDebug() << " save file name " << fileName << endl;
QFile file(fileName);
if (!file.open(QFile::WriteOnly | QFile::Text)) {
QMessageBox::warning(this, "Warning", "Cannot save file: " + file.errorString());
return;
}
setWindowTitle(fileName);
QTextStream out(&file);
QString text = "text that will be saved...";
out << text;
file.close();
}
Edit: After trying multiple solutions, none seemed to work. But it should have, i guess. Why else is there a aftersuffix function...? For now i solved it doing it manually. But i'm not happy with it, there should be a better solution/explanation.
// add extension if none is found.
if(!fileName.endsWith(".aqt"))
fileName.append(".aqt");
If you use the static method getSaveFileName things seems to work correctly:
#include <QFileDialog>
#include <QApplication>
#include <QDebug>
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
QString fileName = QFileDialog::getSaveFileName(
nullptr, QObject::tr("Save File"),
"teste.aqt",
QObject::tr("AstroQt (*.aqt)"));
qDebug() << " save file name " << fileName << endl;
return app.exec();
}
I get the correct file name with the extension, if I type something without the extension.
If you take a look at QFileDialog documentation, you will see that getSaveFileName() is an static function. Because of this, there is no way for this method to access a member of the instance of the class that makes use of setDefaultSuffix(). So whatever you set in fileDialog->setDefaultSuffix(...) has nothing to do with what the getSaveFileName() function does.
In ordertTo make it work, you have to run the dialog directly from the instance. You should do something like this:
QFileDialog fileDialog(this, "Choose file to save");
fileDialog.setDefaultSuffix("json");
fileDialog.setNameFilter("json-files (*.json)");
fileDialog.exec();
QFile f(fileDialog.selectedFiles().first());
QFileInfo fileInfo(f);
QString FILE_NAME(fileInfo.fileName());

Qt QProcess sometimes works and sometimes does not work

I want to convert a ps file to pdf by ps2pdf and this codes in Qt:
QPixmap graphPng(filePath + "report/pictures/graph-000.png");
int graphPsWidth=graphPng.width();
int graphPsHeight=graphPng.height();
//convert "ps" file to "pdf" file
QProcess process;
QStringList arg;
arg<<"-dDEVICEWIDTHPOINTS=" + QString::number(graphPsWidth)<<"-dDEVICEHEIGHTPOINTS=" + QString::number(graphPsHeight)<<"export/report/pictures/graph-000.ps"<<"export/report/pictures/graph-000.pdf";
process.start("ps2pdf",arg);
//process.waitForStarted(-1);
process.waitForFinished(-1);
(I use a png file as the same dimensions of the ps file to get the dimensions and use it in creating pdf file)
I don't know why sometimes the pdf file is created and some times without any output message (process::readAllStandardError() and process::readAllStandardOutput()) there is no pdf file!
When the pdf file is not created if I run that immediately in terminal, the pdf file will be created!!!
What is the reason and how can I solve it?
It is always advisable to verify all the steps, so I share a more robust version of your code.
For example it is not necessary to use QPixmap, the correct thing is to use QImage. In addition, the path of the files is verified.
#include <QCoreApplication>
#include <QDir>
#include <QProcess>
#include <QImage>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QDir directory(QString("%1%2%3")
.arg(a.applicationDirPath())
.arg(QDir::separator())
.arg("export/report/pictures/"));
QString name = "graph-000";
QString png_path = directory.filePath(name + ".png");
QString ps_path = directory.filePath(name + ".ps");
// verify path
Q_ASSERT(QFileInfo(png_path).exists());
Q_ASSERT(QFileInfo(ps_path).exists());
QString pdf_path = directory.filePath(name+".pdf");
QImage img(png_path);
int graphPsWidth = img.width();
int graphPsHeight = img.height();
QProcess process;
QStringList arg;
arg << QString("-dDEVICEWIDTHPOINTS=%1").arg(graphPsWidth) <<
QString("-dDEVICEHEIGHTPOINTS=%1").arg(graphPsHeight) <<
ps_path << pdf_path;
process.start("ps2pdf",arg);
process.waitForFinished();
Q_ASSERT(QFileInfo(pdf_path).exists());
return 0;
}
I can solve the problem by handling the RAM, because if the ram exceed the specific limit (almost near the full capacity of the RAM), the QProcess can not be started. So every time it diagnoses the exceeded limit, the PDF file is not created and vice versa.

Print a PDF file using Qt

I want to open and print a PDF file from particular path, My previous code work perfectly open and directly send print command to printer.
Now what I want is multiple printer are there and I have to select one, and after that I want send print command, I don't want want use QPrintDialog, My printer name are stored in a TextBox and retrieve that name and print it through that printer which I set in textbox:
my previous code mention below:
#include <QSettings>
#include <QProcess>
#include <QDebug>
int main(int argc, char *argv[])
{
const QString classesRoot = "HKEY_CLASSES_ROOT";
// get ID of .pdf extension
QSettings pdfSettings(classesRoot + "\\.pdf", QSettings::NativeFormat);
QString pdfId = pdfSettings.value("Default").toString();
// get path to default program that associated with PDF files
QString printPath = QSettings(classesRoot + "\\" + pdfId + "\\shell\\print\\command", QSettings::NativeFormat).value("Default").toString();
QString openPath = QSettings(classesRoot + "\\" + pdfId + "\\shell\\open\\command", QSettings::NativeFormat).value("Default").toString();
qDebug() << "print path" << printPath;
qDebug() << "open path" << openPath;
// open .pdf file
QProcess::startDetached(openPath.arg("full path to pdf file.pdf") );
// print .pdf file
QProcess printProcess;
printProcess.start(printPath.arg("full path to pdf file.pdf") );
printProcess.waitForFinished(-1);
return 0;
}
Or you can change your printer as default printer during printing.
change default printer to your printer
print pdf
restore old default printer
How to retrieve and set the default printer in Windows:
http://support.microsoft.com/default.aspx?scid=kb;EN-US;246772
Since QT has no functions for system administration. For QT,
Change default printer to your printer
How to get default printer name?
QPrinterInfo::defaultPrinterName()
from: http://doc.qt.io/qt-5/qprinterinfo.html#defaultPrinterName
How to set default printer?
By excuting, RUNDLL32 PRINTUI.DLL,PrintUIEntry /y /n "your printer name"
from: http://windowsitpro.com/windows/jsi-tip-8415-how-can-i-set-users-default-printer-batch-script
Print your pdf as you know
Restore old default printer
By executing, RUNDLL32 PRINTUI.DLL,PrintUIEntry /y /n "old default printer name"

Unable to start g++ using QProcess

I want to compile a c++ file from Qt application by using QProcess. But it is not working, I don't see any .o or .exe file generated by the compiler.
Here is what I am doing -
QProcess *process = new QProcess(this);
QString program = "g++";
QStringList arguments;
//fileName is fetched from QFileDialog
arguments << fileName << "-o" << QFileInfo(fileName).path() + QFileInfo(fileName).baseName() + ".exe";
errorFilename = QFileInfo(fileName).baseName() + "_error.txt";
process->setStandardOutputFile(errorFilename);
connect(process, SIGNAL(finished(int)), this, SLOT(compiled()));
process->start(program, arguments);
Pleae tell me what's wrong with this code. I am working on windows 7.
Keep in mind that errors don't go to stdout, they go to stderr. Try using:
process->setStandardErrorFile(errorFilename);
Also QFileInfo::path() won't have a path separator at the end, so you'll need to add one when concatenating the path with the base filename:
QFileInfo finfo(fileName);
arguments << fileName << "-o" << QFileInfo( QDir(finfo.path()), finfo.baseName() + ".exe").filePath();

I would like to open and read a text file at program launch, using Qt

I would like to open a text file at program launch, using Qt. I would like the text to appear in the text field which is called textEdit.
It is a simple notepad program that I am changing into an app I want to do other special things.
How do I input a text file, say "text.txt" into my textEdit widget upon program launch? All of the text file.
Writing with C++.
Thanks.
#include <QFile>
#include <QTextStream>
QString fileName = "myFile.txt";
File* myFile = new QFile(fileName);
if (myFile->open(QIODevice::ReadOnly | QIODevice::Text)
{
QTextStream *myFileStream = new QTextStream(myFile);
while ( !(myFileStream->atEnd()) )
{
QString line = myFileStream->readLine();
textEdit->append(line);
}
}