QTextBrowser content to File - qt

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"

Related

Is there a way to return current viewed page in QPrintPreviewDialog?

I know it is possible with QPrintPreviewWidget via currentPage() function, but is there a way to return current page in QPrintPreviewDialog? Since I like the default QPrintPreviewDialog's interface, and I don't feel confident enough to rebuild it myself, I would like to use QPrintPreviewDialog.
QPrintPreviewDialog is a QDialog that has a QPrintPreviewWidget as internal elements, so using findChild you can obtain that object.
#include <QApplication>
#include <QPrintPreviewDialog>
#include <QPrintPreviewWidget>
#include <QPrinter>
#include <QTimer>
#include <QTextCursor>
#include <QTextDocument>
#include <QDebug>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QPrintPreviewDialog previewDialog;
QObject::connect(&previewDialog, &QPrintPreviewDialog::paintRequested, &previewDialog, [&previewDialog](QPrinter *printer){
QTextDocument document;
QTextCursor cursor(&document);
QTextBlockFormat blockFormat;
for(int i=0; i < 10; i++){
cursor.insertBlock(blockFormat);
cursor.insertHtml(QString("<h1>This is the %1 page</h1>").arg(i+1));
blockFormat.setPageBreakPolicy(QTextFormat::PageBreak_AlwaysBefore);
}
document.print(printer);
if(QPrintPreviewWidget *previewWidget = previewDialog.findChild<QPrintPreviewWidget *>()){
qDebug() << previewWidget->currentPage();
// change page
QTimer::singleShot(100, previewWidget, [previewWidget](){
previewWidget->setCurrentPage(2);
});
}
});
previewDialog.exec();
}

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.

Qt - Creating PDF file

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.

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

Creating an object of QDeclarativeView results in segmentation fault

.h
#include <QObject>
#include <QDebug>
class MyClass : public QObject
{
Q_OBJECT
public:
Q_INVOKABLE void cppMethod (const QString &msg)
{
qDebug() << "Called the C++ method with" << msg;
}
public slots:
void cppSlot (int number)
{
qDebug() << "Called the C++ slot with" << number;
}
};
.cpp
#include <QtCore/QCoreApplication>
#include <QDeclarativeEngine>
#include <QDeclarativeComponent>
#include <QDeclarativeContext>
#include <QDeclarativeView>
#include <QVariant>
#include <QMetaObject>
#include "cppFromQml.h"
int main (int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QDeclarativeView view;
return a.exec();
}
This results in segmentation fault. What's the way out?
Qt: 4.8.1
note that you're not using MyClass, and - just my guess - a declarative view will need a QApplication to properly run.
To better understand, I created a project, dumped almost all away (just kept the .pro, where I added qt += declarative), and changed a bit your code as follow:
#include <QApplication>
#include <QDeclarativeEngine>
#include <QDeclarativeComponent>
#include <QDeclarativeContext>
#include <QDeclarativeView>
#include <QVariant>
#include <QMetaObject>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QDeclarativeView view;
view.show();
return a.exec();
}
now it runs and display an empty view, as expected...

Resources