I'm trying to convert an HTML file to PDF. The whole idea is to create a pdf with many pages, filling the first one with the HTML file contents. Currently I'm trying to do just that, and the code is:
#include "qprinterexample.h"
#include <QtGui/QApplication>
#include <QTextDocument>
#include <QTextStream>
#include <QFile>
#include <QPrinter>
#include <QDir>
int print(){
const int highQualityDPI = 300;
QDir::setCurrent(QCoreApplication::applicationDirPath());
QFile htmlFile ("ejemplo.htm");
if (!htmlFile.open(QIODevice::ReadOnly | QIODevice::Text)){
return -1;
}
QString htmlContent;
QTextStream in(&htmlFile);
in >> htmlContent;
QTextDocument *document = new QTextDocument();
document->setHtml(htmlContent);
QPrinter printer(QPrinter::HighResolution);
printer.setPageSize(QPrinter::A4);
printer.setOutputFormat(QPrinter::PdfFormat);
printer.setOutputFileName("output.pdf");
document->print(&printer);
delete document;
return 0;
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QPrinterExample w;
if(print()<0) return -1;
w.show();
return a.exec();
}
But when I check the output PDF, it's an empty page, just with the page number 1 at the bottom. What am I doing wrong?
Also, I have noticed that when using QTextStream, it only gets "
Use
htmlContent=in.readAll();
Instead of
in >> htmlContent;
This should work!
Related
I want to display the value of QbyteArray like how qDebug() displays it.
qDebug()<<byteArray === Displays -> "\x8E\xA9\xF3\xA5"
how do you grab this QbyteArray into a QString, when i do the convertion found online it gives me "????" as an output .
I would like the content of the QString is the same as the output of the QDebug();
"\x8E\xA9\xF3\xA5"
so that
QString string would contain "\x8E\xA9\xF3\xA5"
Build a QDebug object using the constructor:
QDebug::QDebug(QString *string)
Constructs a debug stream that writes to the given string.
Example:
#include <QApplication>
#include <QDebug>
#include <QLabel>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QLabel label;
QByteArray ba("\x8E\xA9\xF3\xA5");
QString res;
QDebug db(&res);
db << ba;
label.setText(res);
label.show();
return a.exec();
}
Update:
without "\x", use toHex():
#include <QApplication>
#include <QLabel>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QLabel label;
QByteArray ba("\x8E\xA9\xF3\xA5");
label.setText(ba.toHex());
label.show();
return a.exec();
}
I have a very simple application using WebEngineView and I just wanted to resize the widget displaying to the contents of the html file. I'm expecting it to be 30 pixels wide. Instead my program prints QSize(0,0) and even worser the widget is not displayed at all.
What I'm doing wrong here?
#include <QWebEngineView>
#include <QApplication>
#include <QDebug>
#include <QWebEnginePage>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
auto view = new QWebEngineView;
QString html = "<html><body><div width=30px>Text</div></body></html>";
view->setHtml(html);
auto contentsSize=view->page()->contentsSize().toSize();
qDebug() << contentsSize;
view->setFixedSize(contentsSize);
view->show();
return app.exec();
}
Putting my QWebEngineView into a dialog still doesn't work:
#include <QWebEngineView>
#include <QApplication>
#include <QDebug>
#include <QDialog>
#include <QHBoxLayout>
#include <QWebEnginePage>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
auto dialog = new QDialog;
dialog->setLayout(new QHBoxLayout);
auto view = new QWebEngineView;
dialog->layout()->addWidget(view);
QString html = "<html><body><div width=30px>Text</div></body></html>";
view->setHtml(html);
auto contentsSize=view->page()->contentsSize().toSize();
qDebug() << contentsSize;
view->setFixedSize(contentsSize);
dialog->show();
return app.exec();
}
I also tried to connect to the signal loadFinished, but there is no effect.
#include <QWebEngineView>
#include <QApplication>
#include <QDebug>
#include <QDialog>
#include <QHBoxLayout>
#include <QWebEnginePage>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
auto dialog = new QDialog;
dialog->setLayout(new QHBoxLayout);
auto view = new QWebEngineView;
dialog->layout()->addWidget(view);
QString html = "<html><body><div width=30px>Text</div></body></html>";
view->setHtml(html);
QObject::connect(view->page(), &QWebEnginePage::loadFinished, [&view](bool b) {
qDebug() << b;
auto contentsSize = view->page()->contentsSize().toSize();
qDebug() << contentsSize;
view->setFixedSize(contentsSize);
});
dialog->show();
return app.exec();
}
I have the following code:
filter.h
#pragma once
#include <QObject>
#include <QSortFilterProxyModel>
class FilterModel : public QSortFilterProxyModel
{
Q_OBJECT
public:
explicit FilterModel(QObject *parent = 0);
Q_INVOKABLE QString getText (QString text);
};
filter.cpp
#include "filter.h"
#include <QDebug>
FilterModel::FilterModel(QObject *parent) : QSortFilterProxyModel(parent) {}
QString FilterModel::getText(QString text)
{
QString qmltext = text;
qmltext != NULL ? qDebug() << qmltext
: qDebug() << "TEXT = NULL";
return qmltext;
}
main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include "abonentstable.h"
#include "filter.h"
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
AbonentsSqlModel *abonentsSqlModel = new AbonentsSqlModel;
abonentsSqlModel->setQuery("SELECT * FROM abonents");
FilterModel *filterModel = new FilterModel;
filterModel->setSourceModel(abonentsSqlModel);
filterModel->setFilterKeyColumn(0);
filterModel->setFilterWildcard("9");
QQmlContext *context = engine.rootContext();
context->setContextProperty("abonents", filterModel);
context->setContextProperty("filter", filterModel);
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
return app.exec();
}
And the .qml file part:
TextField {
id: textField
...
onTextChanged: {
filter.getText(textField.text)
}
...
}
Method getText() gets text (suddenly!) from QML TextField and prints it into debugger, it works fine. But as you can see, I have code for table sorting.
The following problem is: now sorting mask is "9", it works, but I need to return QString qmltext from getText() in some way and put it into filterModel->setFilterWildcard() in main.cpp like that:
QString qmlText = filterModel.getText(QString);
...
filterModel->setFilterWildcard(qmlText);
Of course, it's just an example, it doesn't works and I don't know how to do this.
I do not fully understand what you want to do, but I think you need something like that (if you really need the return value):
QString FilterModel::getText(const QString& text)
{
setFilterWildcard(text);
return text;
}
By the way:
qmltext != NULL
is not working. Use instead:
qmltext.isEmpty() == false
I am working on a very simple and quick program in qt to convert an entire file to a single line string and prints it out. It is run through the command line. I have one problem. When compiling the program Qt Creator gives me this error conversion from 'char**' to 'QChar' is ambiguous main.cpp 12
Here's my code:
#include <QApplication>
#include <QStringList>
#include <QFile>
#include <QTextStream>
#include <QStringList>
#include <QDebug>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QString b (argv);
QFile sFile(b);
QTextStream in(&sFile);
QString text = in.readAll();
sFile.close();
QStringList doc;
doc<< text;
QString f = doc.join(" ");
QString final = f;
qDebug()<<final;
return a.exec();
}
argv is a vector of char* (c strings). choose the string you are interested in:
QString b(argv[1]);
I need help. I'm trying to open 1 of 2 possible windows on start. Program decide which window will open on screen dimensions.
#include <QApplication>
#include <QDesktopWidget>
#include "mainwindow.h"
#include "vincellform.h"
#include <QDebug>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QDesktopWidget mydesk;
if (mydesk.screenGeometry().width() == 800 && mydesk.screenGeometry().height() == 480)
{
VincellForm vf;
vf.show();
}
else
{
MainWindow w;
w.show();
}
return a.exec();
}
I think that this code is correct, but it isn't. If I'm on different screen (1280*1024 I think) program goes to else part (MainWindow w; w.show();) and then goes to return, but no window is opened. But if I changed a code to:
#include <QApplication>
#include <QDesktopWidget>
#include "mainwindow.h"
#include "vincellform.h"
#include <QDebug>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QDesktopWidget mydesk;
if (mydesk.screenGeometry().width() == 800 && mydesk.screenGeometry().height() == 480)
{
VincellForm vf;
vf.show();
}
MainWindow w;
w.show();
return a.exec();
}
it runs perfectly (MainWindow will open after return). I can't even imagine where the problem can be... Thank you very much
You're defining the window variables locally in the if and else blocks. This means the windows are destroyed immediately after they're shown.
You have two solutions. If you don't mind creating both windows, but only showing one, do this:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QDesktopWidget mydesk;
VincellForm vf;
MainWindow w;
if (mydesk.screenGeometry().width() == 800 && mydesk.screenGeometry().height() == 480)
{
vf.show();
}
else
{
w.show();
}
return a.exec();
}
If you only want one of them created, you'll have to resort to dynamic allocation:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QDesktopWidget mydesk;
std::unique_ptr<VincellForm> vf;
std::unique_ptr<MainWindow> w;
if (mydesk.screenGeometry().width() == 800 && mydesk.screenGeometry().height() == 480)
{
vf.reset(new VincellForm);
vf->show();
}
else
{
w.reset(new MainWindow);
w->show();
}
return a.exec();
}
Note: std::unique_ptr comes from C++11. If you don't have this yet, use raw pointers instead a delete manually at program end.