I looked at some examples and decided to implement one of them. It compiles and doesn't crash when ran, however It doesn't create the pdf, it throws some error (which I do not understand). The question is where there error is and how can it be removed?
The project code:
#-------------------------------------------------
#
# Project created by QtCreator 2013-06-08T10:07:11
#
#-------------------------------------------------
QT += core
QT -= gui
QT += printsupport
TARGET = PDFPrintMaybe
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
And the source itself:
#include <QTextDocument>
#include <QPrinter>
#include <QApplication>
int main( int argc, char **argv )
{
QApplication app( argc, argv );
QTextDocument doc;
doc.setHtml( "<p>A QTextDocument can be used to present formatted text "
"in a nice way.</p>"
"<p align=center>It can be <b>formatted</b> "
"<font size=+2>in</font> <i>different</i> ways.</p>"
"<p>The text can be really long and contain many "
"paragraphs. It is properly wrapped and such...</p>" );
QPrinter printer;
printer.setOutputFileName("C:\\Users\\SameTime\\Desktop");
printer.setOutputFormat(QPrinter::PdfFormat);
doc.print(&printer);
printer.newPage();
return 0;
}
And finally, the error itself:
QPainter:: begin(): Returned false
"C:\\Users\\SameTime\\Desktop"
probably refers to the existing folder, not a file name.
You should specify your pdf file name instead, like
"C:\\Users\\SameTime\\Desktop\\1.pdf"
And make sure that path to the file exists and accessible.
Otherwise, sustem would not be able to crate pdf and print (i.e. paint on the pdf canvas)
Related
I have a project with QT in vc++ and I need to locate the string in the UI to different languages. I created a UI through the QTdesigner in the visual studio add in of visual studio 2012 and also I have installed the QT plugin to use the Qt features as well.
I have created a .pro file and added:
SOURCES += main.cpp
TRANSLATIONS += languagefileqt_es.ts
After I generate a linguist file SOURCES emminensmultiportqt_es.ts and it detected correctly all the strings in my IU. After that, I generate the .qm file using the release function of Qtlinguist.
My resources file is:
<RCC>
<qresource prefix="MyAppQT">
<file>languagefileqt_es.qm</file>
</qresource>
</RCC>
Then I have added this to my main.cpp:
QTranslator translator;
bool loaded = translator.load("languagefileqt_es");
qDebug() << "loaded " << loaded;
a.installTranslator(&translator);
And loaded returns true in all the cases. My problem is that the UI is not translated when the application is executed. It is weird because it has no effect.
Any clue about what I am missing or what could I check out?
Thanks a lot
Are you sure, that "languagefileqt_es" is the correct name of your language file? I would expect "languagefileqt_es.qm" instead.
Are you sure that you are loading from the correct directory? Unless you are loading from an internal compiled-in resource (:/languagefileqt_es.qm) you should refer to an absolute path to make sure, that you load the correct thing.
I discovered what was the problem. thanks #Jens for try to help.
I think I commit a mistake of not knowing how the translating mechanism was working. In my main.cpp I had:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindowQT w;
QTranslator translator;
bool loaded = translator.load("languagefileqt_es");
qDebug() << "loaded " << loaded;
a.installTranslator(&translator);
w.show();
return a.exec();
}
But I realized that if I execute
qDebug() << QApplication::translate("MainWindowQTClass", "...BOARDING", 0);
after the loading process it will return the string translated correctly. So I change the definition of my UI after the internationalization and it worked. Apparently, translation is done in a function called retranslateUi() which is called in the constructor.
The correct main.cpp should be:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QTranslator translator;
bool loaded = translator.load("languagefileqt_es");
qDebug() << "loaded " << loaded;
a.installTranslator(&translator);
MainWindowQT w;
w.show();
return a.exec();
}
I tried to use libjpeg-turbo with qt.
I downloaded libjpeg-turbo and installed. I wanted to use it within a project but I got the following fault:
C:\libjpeg-turbo-gcc64\bin\libjpeg-62.dll:-1: Error: file not
recognized: File format not recognized
As soon as I removed the libjpeg-62.dll I received the following fault:
C:\test\main.cpp:8: Error: undefined reference to
`tjInitCompress'
Why is the libjpeg-62 not recognizing the file format?
Thanks for help,
Willy
PS. Here is the code:
test.pro
QT += core
QT -= gui
TARGET = test
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
INCLUDEPATH += C:\libjpeg-turbo-gcc64\include
LIBS += -LC:\libjpeg-turbo-gcc64\bin -llibjpeg-62
main.cpp
#include <QCoreApplication>
#include <turbojpeg.h>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
tjhandle _jpegCompressor = tjInitCompress();
return a.exec();
}
Ok now it works,
it was the wrong version of libjpeg-turbo. Now I use the libjpeg-turbo-gcc and not the libjpeg-turbo-gcc64. Also i change the Libs-Path to
LIBS += "C://libjpeg-turbo-gcc64//bin//libjpeg-62.dll"
MfG Willy
I have a list of textfiles in a treeview (with QFileSystemModel). If a textfile is selected and a print button is pressed. It should show a print dialog and the file should be printed out. I thought (after reading documentations and examples) it should look like this:
void berichtenhistorie::on_printButton_released()
{
QModelIndex index = ui->treeView->currentIndex();
QFileSystemModel *model = (QFileSystemModel*)ui->treeView->model();
QString path = model->filePath(index);
QString name = model->fileName(index);
QString dir = path;
dir.remove(dir.size() - name.size(), name.size());
QFile file(path);
if(file.open(QIODevice::WriteOnly | QIODevice::Text))
{
file.close();
if(file.rename(QString("%1geprint %2").arg(dir, name)))
qDebug() << "renamed";
}
//all above works correctly
QPrinter printer(QPrinter::HighResolution);
printer.setPageSize(QPrinter::A4);
printer.setOrientation(QPrinter::Portrait);
printer.setPageMargins (15,15,15,15,QPrinter::Millimeter);
printer.setFullPage(false);
printer.setOutputFileName(path);
printer.setOutputFormat(QPrinter::NativeFormat);
QPainter painter(&printer);
painter.end();
}
The renaming part (so above all the printing stuff) works as it should, no errors or anything. But I get abunch of errors at the printing error. I thought it was because of the libraries, because im using Qt5.
#include <QDirModel>
#include <QDebug>
#include <QMessageBox>
#include <QtPrintSupport/QPrintDialog>
#include <QtPrintSupport/QPrinter>
#include <QPainter>
#include <QFile>
Here are the errors:
Apparently you are using Qt5, where printing functionality has been placed in separate add on (in Qt4 it is part of QtGui module), see documentation. So you have to add to pro file this line:
QT += printsupport
This will fix your build error, but your code doesn't print yet. You have to use painter.
If you are planing to support Qt4 it should be like this
greaterThan(QT_MAJOR_VERSION, 4) {
QT += printsupport
}
EDIT: I managed to get a compiling and non crashing version. The only thing left is to get the desired output, however this particular question (why it crashes) has been answered so I am closing the question. I will post the working code before the broken one.
Good day! I am trying to create a small example that will simply create a pdf document. Everything compiles, however when the program starts it simply crashes. I am using Qt version 5.0.0
---New working code---
int main( int argc, char **argv )
{
QApplication app( argc, argv );
QTextDocument doc;
doc.setHtml( "<p>A QTextDocument can be used to present formatted text "
"in a nice way.</p>"
"<p align=center>It can be <b>formatted</b> "
"<font size=+2>in</font> <i>different</i> ways.</p>"
"<p>The text can be really long and contain many "
"paragraphs. It is properly wrapped and such...</p>" );
QPrinter printer;
printer.setOutputFileName("C:\\Users\\SameTime\\Desktop\\Cutie\\PDFPrintMaybe");
printer.setOutputFormat(QPrinter::PdfFormat);
doc.print(&printer);
printer.newPage();
return 0;
}
Here is the project code:
#-------------------------------------------------
#
# Project created by QtCreator 2013-06-08T10:07:11
#
#-------------------------------------------------
QT += core
QT -= gui
QT += printsupport
TARGET = PDFPrintMaybe
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
----Old code with error---
And here is the main cpp:
#include <QCoreApplication>
#include <QTextDocument>
#include <QPrinter>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QTextDocument doc;
doc.setHtml("<h1>Testing, testing, is this thing on?!</h1>");
QPrinter printer;
printer.setOutputFileName("C:\\Users\\SameTime\\Desktop\\Cutie\\PDFPrintMaybe");
printer.setOutputFormat(QPrinter::PdfFormat);
doc.print(&printer);
printer.newPage();
return a.exec();
}
I am a bit at a loss since it is compiling but crashing (almost) instantly when ran.
Try to create the objects on the heap otherwise they get automatically deleted when they ran out of scope, then the framework probably tries to delete them again and crashes.
QTextDocument *doc = new QTextDocument();
doc->setHtml("<h1>Testing, testing, is this thing on?!</h1>");
QPrinter* printer = new QPrinter();
printer->setOutputFileName("C:\\Users\\SameTime\\Desktop\\Cutie\\PDFPrintMaybe");
printer->setOutputFormat(QPrinter::PdfFormat);
doc->print(printer);
printer->newPage();
I am trying to do console application to read pixels from image:
#include <QtCore/QCoreApplication>
#include <QtGui/QImage>
#include <iostream>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QImage *img = new QImage("adadad.jpg");
//std::cout << "Type filename:" << std::endl;
img->isNull();
return a.exec();
}
That doesn't work I got: (IT doesn't compile, but anyway file isn't exist yet...)
File not found: tmp/obj/debug_shared/main.o:: In function `main':
What is going on? Is it impossible to use Qimage with console app?!
EDIT:
screen
It is possible to use QImage in a console application, you must make sure that QtGui is configured though. If you chose a console app, your .pro file might contain something like
CONFIG += console
QT -= gui
If that's the case, remove the QT -= gui line.
QImage("adadad.jpg");
Will probably look for a file called adadad.jpg on the current working directory for your application. Check if that file is present. Otherwise, use a fully qualified path.
img->isNull() doesn't do anything on it's own, try this instead:
if(img->isNull())
std::cout << "Image isNull!\n";
else
std::cout << "Image loaded\n";
My guess is that the local directory of the executable is not the same as the location of that image, so Qt can't find the file. Try specifying the complete path.
EDIT: Ahh... didn't realize it was a compilation problem. That looks suspiciously like a moc issue. What build system are you using? and can you confirm that the moc step is executing?
This modification of your code will compile and run as expected if there is a valid image file in the current working directory when you run the app. It will display Image loaded
#include <QtGui/QImage>
#include <iostream>
int main(int argc, char *argv[])
{
QImage *img = new QImage("adadad.jpg");
if(img->isNull())
std::cout << "Image is null";
else
std::cout << "Image loaded";
return 0;
}
You do not need to create an instance of QCoreApplication unless you have subclassed it and put your program code in that subclass.
Update:
Your program does not exit so you are probably getting that compile error because it can't replace the executable because it is still running (and locked). The file locking is more likely to be an issue under Windows.
An important note when you are loading a file using directly "adadad.jpg" in your code. Even if you put the file inside the debug/release folder, QImage will always be null if loaded this way.
I run into this problem yesterday and I fixed it by using the Qt library to get the full path: QCoreApplication::applicationDirPath().
There is two way to achieve that, first one is when you create the img object.
QImage img( QCoreApplication::applicationDirPath() + "adadad.jpg");
if( img.isNull())
{
qDebug() << "Loading Error - file: adadad.jpg.";
return false;
}
or using the load function
QImage img;
if( !img.load(QCoreApplication::applicationDirPath() + "adadad.jpg"))
{
qDebug() << "Loading Error - file: adadad.jpg.";
return false;
}