Qt - Creating PDF file - qt

I want to create a PDF-file in Qt console application. But file size of created file is 0B and I can't open it.
The same code in GUI application works. Where is the difference between code in console and gui application? What should I do to make the code working in console application?
Thanks for help in advance!
#include <QCoreApplication>
#include <QPrinter>
#include <QTextDocument>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QString html = "<h1>Hi!</h1>";
QTextDocument document;
document.setHtml(html);
QPrinter printer(QPrinter::PrinterResolution);
printer.setOutputFormat(QPrinter::PdfFormat);
printer.setOutputFileName("test.pdf");
document.print(&printer);
return a.exec();
}

Here it's working.
#include <QApplication>
#include <QPrinter>
#include <QTextDocument>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QString html = "<h1>Hi!</h1>";
QTextDocument document;
document.setHtml(html);
QPrinter printer(QPrinter::PrinterResolution);
printer.setOutputFormat(QPrinter::PdfFormat);
printer.setOutputFileName("test.pdf");
document.print(&printer);
return a.exec();
}
so it looks like you have just to change the QCoreApplication by QApplication.

Related

FFmpeg on Qt Creator

I have got FFmpeg compiled using MSYS2. I have a strange issue that appeared only recently.the app crashes.
This is my main file:
#include "mainwindow.h"
#include <QApplication>
#include <QDebug>
extern "C" {
#include <libavdevice/avdevice.h>
#include <libavformat/avformat.h>
#include <libavutil/avutil.h>
#include <libavcodec/avcodec.h>
}
int main(int argc, char *argv[])
{
//qDebug() << av_version_info();
// avdevice_register_all();
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
The program runs but crashes if I call 'avdevice_register_all()' and others FFmpeg functions,but only call 'av_version_info()' is runs, not crashes.
I don't really know how to solve this?
I have already tried to use google solving the problem,but no result.

How to show every qDebug with new window?

I make a QT console program.
I want to show every command by new window.
How could I do that ??
main.cpp
I use system is win10 with QT.
#include <QCoreApplication>
#include <QDebug> //在文字視窗輸出文字功能函式
#include <QDir> //搜尋資料夾功能函式
#include <QFileInfo> //顯示檔案資訊
#include <QString> //字串函式
#include <QStringList> //字串陣列函式
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QDir mDir; //設定資料夾位置
qDebug() << mDir.exists(); //輸出確定是否有mDir資料夾
QString command = "frtomem -p WnaYooAQ ";
foreach(QFileInfo mitm, mDir.entryInfoList(Qfilter)) //列出所有資料夾中篩選的檔案內容
{
qDebug() << command << mitm.fileName();
}
return a.exec();
}
I just can show it by one console window.
can't separete it.

Having trouble drawing an image in Qt using QGraphicsScene

The code below loads an image using QLabel. The code uses: myLabel.setMask(pixmap.mask()); and it works as it should. The problem comes when I try and load an image using QGraphicsScene.
#include <QApplication>
#include <QLabel>
#include <QtGui>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QLabel myLabel;
QPixmap pixmap("/path/tomy/image.jpg");
myLabel.setPixmap(pixmap);
myLabel.setMask(pixmap.mask());
myLabel.show();
return app.exec();
}
In this code I am attempting to the same as above but using QGraphicsScene. The pixmap is loaded properly, after that I am not sure why the program is not working properly. Is it because there is no setMask() operation? Or is there an operation missing that is needed to make the image visible?
#include <QtGlobal>
#if QT_VERSION >= 0x050000
#include <QtWidgets>
#else
#include <QtGui>
#endif
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QPixmap pixmap("/path/tomy/image.jpg");
QGraphicsPixmapItem item( pixmap);
QGraphicsScene* scene = new QGraphicsScene;
scene->addItem(&item);
QGraphicsView view(scene);
view.show();
return a.exec();
}

Error Occured while designing Dialog in Qt

The program name is GoToCell in the form I created a label and two push buttons and I wrote the code in main.cpp as follows:
#include <QtGui/QApplication>
#include<QDialog>
#include "ui_GoToCell.h"
#include "GoToCell.h"
int main(int argc, char *argv[]) {
QApplication a(argc, argv);
Ui::GoToCell ui;
QDialog *dialog=new QDialog;
ui.setupUi(dialog);
dialog->show();
return a.exec();
}
While running it I'm getting the following errors:
GoToCell is not a member of ui
What should I do?
I think you have misspelled object name in objectName property in the GoToCell.ui form.
Change it to GoToCell. It will then execute.

QTextBrowser content to File

Is there a way to dump content of QTextBrowser object to a file?
Below is a short, working example of how to do it.
#include <QApplication>
#include <QFile>
#include <QTextBrowser>
#include <QTextStream>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QTextBrowser browser;
browser.setHtml("<html><body>Some text...</body></html>");
QFile file("out.txt");
if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
return -1;
QTextStream out(&file);
out << browser.document()->toHtml();
file.close();
app.exec();
}
#include "main.moc"

Resources