conversion from 'char**' to 'QChar' is ambiguous - c++

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

Related

Preserve QbyteArray to Qstring

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

QWebEnginePage does not know its contents size

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

error: conversion from ‘void’ to non-scalar type ‘QString’ requested

#include "mainwindow.h"
#include <QApplication>
#include <QString>
#include <QDebug>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
QString h;
h = "fsdfsdfsf";
QString j = h.chop(3);
qDebug() << "j: " << j;
return a.exec();
}
Output:
error: conversion from ‘void’ to non-scalar type ‘QString’ requested
Where am I going wrong here?
The reason of the error is that QString::chop() removes characters from the string for which it is called and does not return anything (returns void).
You have two options:
set j = h and then call j.chop (3);
use QString::chopped(): j = h.chopped (3).

error Using QTest Macro QCOMPARE

I want use QTest Macro QCOMPARE in my code,but I receive errors.
QTestString.h
#ifndef QTESTSTRING_H
#define QTESTSTRING_H
#include <QtCore/QString>
#include <QtTest/QtTest>
class TestqstringTest : public QObject
{
Q_OBJECT
public:
TestqstringTest();
private slots:
void testCase1();
};
#endif // QTESTSTRING_H
QTestString.cpp
#include "QTestString.h"
TestqstringTest::TestqstringTest()
{
testCase1();
}
void TestqstringTest::testCase1()
{
QString str = "Hello";
QCOMPARE(str.toUpper(),(QString)"hELLO");
}
main.cpp
#include "QTestString.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
TestqstringTest *test = new TestqstringTest();
return a.exec();
}
However, I receive the following errors:
ASSERT: "QTest::testLogger" in file qtestlog.cpp, line 266 The program
has unexpectedly finished.
I found the answer,you must use int QTest::qExec ( QObject * testObject, int argc = 0, char ** argv = 0 ) to excute it ,then the testlog output correctly.

Converting HTML to PDF with Qt

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!