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();
}
Related
That's strange, w have the member function installEventFilter()
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char* argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.SerialPortInit();
//w.installNativeEventFilter(&w);
w.installNativeEventFilter(&w);
w.show();
qDebug() << "hello Wolrd";
return a.exec();
}
enter image description here
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 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.
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!