I have the following section of code. Am using XCode. Where should i place the source.txt and target.txt so as to be accessible?
std::ifstream fin("source.txt");
std::ofstream fout("target.txt");
fout<<fin.rdbuf(); //sends the text string to the file "target.txt"
system("pause");
return 0;
Related
I would like to integrate Sphinx documentation functionality to help with my Qt project. However, when including the HTML files for Sphinx, the formatting appears differently and no file links work. For example:
QFile file("/home/user1/project/Sphinx/build/html/intro.html");
if (!file.open(QIODevice::Readonly))
qDebug() << "Didn't open file";
QTextStream in(&file);
ui->textBrowser->setText(in.readAll());
Error: QTextBrowser: No document for _sources/intro.txt
This will cause the textBrowser to open the correct file, but will not end up displaying the page with the correct HTML coding, and will not follow the links even though those HTML files are contained in the same path (as I have copied the entire Sphinx project into the Qt project).
Is there some way to package the entire Sphinx project so that inclusion of multiple files is unnecessary or is the multiple file inclusion the way to go and I'm just handling it incorrectly?
Instead of reading all text and setting it with setText() you must use the setSource() method and pass it to the QUrl using the QUr::fromLocalFile() method.
main.cpp
#include <QtWidgets>
class Widget: public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent=nullptr):
QWidget(parent),
m_text_browser(new QTextBrowser)
{
m_lineedit = new QLineEdit;
auto button = new QPushButton("Load");
auto lay = new QVBoxLayout{this};
auto hlay = new QHBoxLayout;
lay->addLayout(hlay);
hlay->addWidget(m_lineedit);
hlay->addWidget(button);
lay->addWidget(m_text_browser);
connect(button, &QPushButton::clicked, this, &Widget::on_clicked);
}
private slots:
void on_clicked(){
QString fileName = QFileDialog::getOpenFileName(this,
tr("Open Image"),
QDir::homePath(),
tr("HTML Files (*.html)"));
m_lineedit->setText(fileName);
m_text_browser->setSource(QUrl::fromLocalFile(fileName));
}
private:
QTextBrowser *m_text_browser;
QLineEdit *m_lineedit;
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.showMaximized();
return a.exec();
}
#include "main.moc"
I'm trying to parse a html source code. this is the webpage address I'm trying to parse. I have wrote the code below but it doesn't work at the last step that I wanna pull-out content of meta:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QNetworkAccessManager manager;
QNetworkReply *reply = manager.get(QNetworkRequest(QUrl("https://www.instagram.com/p/BTwnRykl6EM/")));
QEventLoop event;
QObject::connect(reply, SIGNAL(finished()), &event, SLOT(quit()));
event.exec();
QString me = reply->readAll();
QString x;
//-------------------------------------------------------------------------------------------------------
//qDebug()<<me;
//-------------------------------------------------------------------------------------------------------
QXmlStreamReader reader(me);
if(reader.readNextStartElement()){
if(reader.name()=="html"){
while (reader.readNextStartElement()) {
if(reader.name()=="head"){
while (reader.readNextStartElement()) {
if(reader.name()=="meta" && reader.attributes().hasAttribute("property") && reader.attributes().value("property").toString()=="og:image")
x = reader.attributes().value("content").toString();
else{
qDebug()<<"why?";
reader.skipCurrentElement();
}
}
}
else
reader.skipCurrentElement();
}
}
else
reader.skipCurrentElement();
}
qDebug()<<x;
return 0;
}
and this part doesn't work:
if(reader.name()=="meta" && reader.attributes().hasAttribute("property") && reader.attributes().value("property").toString()=="og:image")
x = reader.attributes().value("content").toString();
else{
qDebug()<<"why?";
reader.skipCurrentElement();
}
and prints
why?
what is wrong with my code?
HTML is not a valid XML, so you can't use XML parsers. Options for HTML you can find on this wiki page.
Shortly, you can use Qt's Scribe framework or QtWebKit for automatic parsing and rendering HTML, or external libraries for manual parsing:
libxml2 (Win, Mac, Linux)
htmlcxx (Win, Linux)
libhtml (Linux)
libxml2 and libhtml are C libraries, htmlcxx is C++ library, that allows build dom-tree and iterate through it.
I have a bunch (over 1000) HTML files with just simple text. It's just a combination of text within a <table>. It's an internal batch of documents, not for web production.
The job we have is to convert them into JPEG files using Photoshop and the old copy paste method. It's tedious.
Is there a way you would do this process to make it more efficient/easier/simple?
I thought about trying to convert the HTML into Excel and then mail merging it into Word to print as JGEG. But I can't find (and rightly so) anything to convert HTML to XLSX.
Thoughts? Or is this just a manual job?
Here's a little something I created to convert a single html file to jpeg. It's not pretty (to say the least), but it works fine with a table larger than my screen. Put it inside a windows forms project. You can add more checks and call this program in a loop, or refactor it to work on multiple html files.
Ideas and techniques taken from -
Finding the needed size - http://social.msdn.microsoft.com/Forums/ie/en-US/f6f0c641-43bd-44cc-8be0-12b40fbc4c43/webbrowser-object-use-to-find-the-width-of-a-web-page
Creating the graphics - http://cplus.about.com/od/learnc/a/How-To-Save-Web-Page-Screen-Grab-csharp.htm
A table for example - copy-paste enlarged version of http://www.w3schools.com/html/html_tables.asp
static class Program
{
static WebBrowser webBrowser = new WebBrowser();
private static string m_fileName;
[STAThread]
static void Main(string[] args)
{
if (args.Length != 1)
{
MessageBox.Show("Usage: [fileName]");
return;
}
m_fileName = args[0];
webBrowser.DocumentCompleted += (a, b) => webBrowser_DocumentCompleted();
webBrowser.ScrollBarsEnabled = false; // Don't want them rendered
webBrowser.Navigate(new Uri(m_fileName));
Application.Run();
}
static void webBrowser_DocumentCompleted()
{
// Get the needed size of the control
webBrowser.Width = webBrowser.Document.Body.ScrollRectangle.Width + webBrowser.Margin.Horizontal;
webBrowser.Height = webBrowser.Document.Body.ScrollRectangle.Height + webBrowser.Margin.Vertical;
// Create the graphics and save the image
using (var graphics = webBrowser.CreateGraphics())
{
var bitmap = new Bitmap(webBrowser.Size.Width, webBrowser.Size.Height, graphics);
webBrowser.DrawToBitmap(bitmap, webBrowser.ClientRectangle);
string newFileName = Path.ChangeExtension(m_fileName, ".jpg");
bitmap.Save(newFileName, ImageFormat.Jpeg);
}
// Shamefully exit the application
Application.ExitThread();
}
}
You can load all files in one page and use this lib html2canvas to covert.
You can running in the background use nodejs with node-canvas or make it a desk app with node-webkit
In case anyone was looking for answer that works, I ended up using a program called Prince: https://www.princexml.com
It works amazingly, and just have to target the HTML with CSS or JS to make it match your output!
I'm trying to read a file from
public static void printLines(String doc){
Scanner input = null;
input = new Scanner(doc + ".txt"));
while(input.hasNext()){
String nextLine = input.nextLine();
System.out.println(nextLine);
}
input.close;
}
The code above is from a method that takes in String doc as a parameter, and prints out every line. In my main, I used a try catch block to catch a FileNotFoundException. However, when I try running the program, it hits FileNotFoundException all the time.
I'm using Eclipse and the text file and java file are both in the same "Java Project". Is there any reason why my method can't read the text file? Please help!
Try to print the doc inside it. It may be including the extension of the file too.
For example, if the doc is /home/user/Desktop/mySample.txt the Scanner will try to find /home/user/Desktop/mySample.txt.txt which is not available, because you are concatenating the extension .txt again.
The reason you're getting FileNotFoundException is that you're using the wrong constuctor of Scanner.
Change input = new Scanner(doc + ".txt")); to
input = new Scanner(new File(doc + ".txt"))); should suit your need.
I would like to create screenshots of web pages from a given URL. While it's possible to use tools like Selenium RC, that requires a graphical environment. I am running a headless Gentoo server.
This will be part of a tool chain that works like:
Fetch URL
Render HTML
Export render as image file
Store image file
You can run an application with framebuffer X-Server like xvfb - one simple approach is a Qt based app to render the page in a webkit widget and save as an image. Here's a blog post outlining how this can be done with Python.
Here's a quick command line tool I've used with Qt. It's a while since I used it but it should still work!
#include <QtCore/QCoreApplication>
#include <QtGui>
#include <QtWebKit>
#include <QTextStream>
#include <QSize>
QWebView *view;
QString outfile;
void QWebView::loadFinished(bool ok)
{
QTextStream out(stdout);
if (!ok) {
out << "Page loading failed\n";
return;
}
view->page()->setViewportSize(view->page()->currentFrame()->contentsSize());
QImage *img = new QImage(view->page()->viewportSize(), QImage::Format_ARGB32);
QPainter *paint = new QPainter(img);
view->page()->currentFrame()->render(paint);
paint->end();
if(!img->save(outfile, "png"))
out << "Save failure\n";
QApplication::quit();
return;
}
int main(int argc, char *argv[])
{
QTextStream out(stdout);
if(argc < 3) {
out << "USAGE: " << argv[0] << " <url> <outfile>\n";
return -1;
}
outfile = argv[2];
QApplication app(argc, argv);
view = new QWebView();
view->load(QUrl(argv[1]));
return app.exec();
}