Print a PDF file using Qt - c++

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"

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());

cmd.exe to open pdf and print it

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;
}

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);
}
}

Parsing terminal arguments Qt Mac OS X

I have a simple text editor in which I would like to open a file when it's double-clicked from the system file manager.
I managed to do that without any problems under Ubuntu Linux (13.04), but in Mac OS X my code did not work.
After researching a bit, I found out that you need to add the --args argument in terminal in order to parse the arguments to main().
I fixed my code and now my application bundle can open files from the terminal, but when I double click a file in Finder (and select my app), my application launches as if did not receive any terminal arguments (creates a new file).
Here is the code of the main() function:
int main(int argc, char *argv[])
{
QApplication MyApp(argc, argv);
Textpad.setApplicationName("MyApp");
Textpad.setApplicationVersion("0.7.2");
Textpad.setWindowIcon(QIcon(":/app-icon/48x48/icon.png"));
MainWindow *Window = new MainWindow();
QString Arguments;
QString FileLocation;
if (argc != 1) {
int i;
for (i = 0; i < argc; i++)
Arguments = argv[i];
// Check if the OS is Mac OS X (Mac OS X is 3)
if (Window->CheckOS() == 3)
// Remove the "--args" so that we don't confuse it with the file location
Arguments.replace("--args", "");
if (Arguments == "--help") {
// Show help
}
// Create a new file when Textpad is launched normally (under Linux)
if (Arguments == "%U") {
FileLocation.clear();
// Load settings and create UI
Window->Initialize();
// Open the requested file
Window->LoadFile(FileLocation);
}
else {
FileLocation = Arguments;
// Load settings and create UI
Window->Initialize();
// Open the requested file
Window->LoadFile(FileLocation);
}
}
else {
// Create new file
FileLocation.clear();
// Load settings and create UI
Window->Initialize();
// Open the requested file
Window->LoadFile(FileLocation);
}
return MyApp.exec();
}
As I said before my application opens files without probles from the terminal when I write the following:
open MyApp.app --args <location of my file>
But fails when I try to open a file from Finder.
What I am missing?
Thank you in advance.
First of all, you will have to link against the OX-X Framework. OSX works with Events similar to signal slots. The filename will also be given by an apple event. I`ve had this quite some time ago with another language, but i still found a reference:
Edit doc now in Qt archive:
https://doc.qt.io/archives/qq/qq12-mac-events.html