QWebEngine: print a page? - c++

The migration from QWebKit to QWebEngine seems to be much more complicated than Qt guys claimed. With QWebKit I could print a webpage easily via
QWebView->print(&printer);
With QWebEngine class QWebEngine view does not provide a method print(). Their browser example uses a class named QWebEngineFrame which offers a method print() - but the whole QWebEngineFrame is not defined anywhere!
So my question: how do I print a page using QWebEngine?

I think the correct way to use QWebEngineView::render method because QWebEngineView is a QWidget. It accepts paint device as a first argument and you may pass QPrinter there for printing.
Update: If you can use the latest version of Qt, in Qt 5.8 a new function for printing page was added:
void QWebEnginePage::print(QPrinter *printer, FunctorOrLambda resultCallback);
Actually it first prints to temp PDF with QPrinter settings.
Here is the link to Qt docs.
You can read about this in our blog also.

I would offer following code (for a while):
QTextEdit *textEdit = new QTextEdit;
ui.myWebView->page()->toHtml([textEdit](const QString &result){ textEdit->setHtml(result); });
textEdit->print(somerinter);
textEdit->deleteLater();

Qt 5.7 includes print support in to pdf files for QWebEngine.
Use printToPdf function to export the current page in a pdf file. Example:
const QString fileName = QFileDialog::getSaveFileName(0,
tr("Save pdf"),
".",
tr("PDF Files (*.pdf)"));
if (fileName.isEmpty()) {
return;
}
ui->webView->page()->printToPdf(fileName);

QWebView->page()->print(&printer, [=](bool){});

Related

Displaying picture that is not in resources of Qt

I am currently writing an application in Qt, which is basically a warehouse. An application reads CSV, enables user to process it and enables to show picture of each good. I tried displaying picture using QLabel and Pixmap, however nothing happens even though the file is in the same folder and the name provided is exactly as it should be. Is it the resources issue or my code fails somehow? Is there any possibility to display the image without adding it to resources in order to avoid adding many photos manually?
void ImageViewer::viewImage(QString imgName)
{
QString pathWithName = imgName;
pathWithName.append(".jpg");
ui->label->setPixmap( QPixmap(pathWithName) );
ui->label->show();
update();
}
Sorry for any mistakes in post creation or code displaying here- it's my first post.
Edit:
I am adding code from MainWindow (called CsvReader in my project) to how I'm invoking the method viewImage:
void CsvReader::on_imgView_clicked()
{
ImageViewer* img = new ImageViewer(this);
img->setModal(true);
img->exec();
QModelIndex List selInd ui->tableView->selectionModel()->selectedIndexes();
QString id = model->item(selInd.first().row(), 0)->text();
img->viewImage(id);
}
Edit 2:
Solved. Had to change path using QDir:
QDir* directory = new QDir("/home/kokos/Magazyn/photos");
QFileInfo checkFile(*directory, pathWithName);
Thanks in advance,
Kokos
Confirm your file's location and existence first. Add this;
QFileInfo checkFile(pathWithName);
if (checkFile.exists() && checkFile.isFile()) {
// your code
}

how to load qt compiled c++ code in maya?

Is there any way to load qt compiled c++ code in maya?
//example code
void MainWindow::on_pushButton_clicked()
{
ui->labell->setText("Hello");
}
actually i was created basic ui with text and push button, what i want is text should change to hello when i push button and i achieved that. so this connections were made with above code, after compiling this all works fine but when i load ui file in maya and i pushes the button text doesn't changes because actually code was written in c++. so, is there any alternative to load that code too along with ui file?
thank you,
Anvesh Chary
To load a .ui file in Maya, I've previously done this in python, I'm not sure about C++ but I don't believe maya interprets C++ directly anyway (I could be wrong there).
import maya.cmds as cmds
ve = cmds.about(version=True)
conv = "%s"%ve
versionOutput = float(conv[0:4])
def mayaVers():
cmds.warning("You're using Maya %s! You need to be using Maya 2011 or greater to be compatible with this script.\n" % conv);
def loadUIWindow():
if versionOutput >= 2011:
if (cmds.dockControl('dockUIWindow', exists=True)):
cmds.deleteUI('dockUIWindow')
scriptsDirectory = cmds.internalVar(usd=True)
UIWindow = cmds.loadUI(uiFile=scriptsDirectory + "/uifilename.ui")
dockSoftMod = cmds.dockControl('dockUIWindow',area="left", content='uiwindowname', label="")
else:
mayaVers()
loadUIWindow()
Here's how I've done it in the past, if you're just looking to source a UI file into the Maya session, this is how it can be done.
Obviously you'll need to either put your ui file in the scripts directory, or change the uiFilePath to your file.
Also, the content flag in the dockControl is important, this needs to be the name of the window or control that you're trying to dock. Let's say you have called your UI file wrapper 'win', the content flag would need to be the same.
EDIT
After you load the UI file, you can edit any element in the window if you know it's name.
cmds.button('ParentBtn', edit=1, command="parentObject()")
Hope this helps.

QFileDialog dynamic translation

This question has unfortunately been asked before but I'm going insane here.
In my Qt Application the user is able to dynamically change the language which works perfect for all my own translations. It does not work for my calls to QFileDialog. The respective code:
void myapp::change_language(std::string& lang_str){
// my own translations works
qApp->removeTranslator(&this->app_lang);
this->app_lang.load(QString::fromStdString(lang_str));
qApp->installTranslator(&this->app_lang);
// system translations, works not for qfiledialog
qApp->removeTranslator(&this->app_lang_qt);
bool test = this->app_lang_qt.load("qt_fr.qm"); // returns true
qApp->installTranslator(&this->app_lang_qt);
}
And
void myapp::changeEvent(QEvent* event){
if(event->type() == QEvent::LanguageChange){
this->ui.retranslateUi(this);
}
QMainWindow::changeEvent(event);
}
With
QTranslator app_lang;
QTranslator app_lang_qt;
The fixed string "qt_fr.qm" is just for testing purpose because french is easily detectable.
What I want is to change the language in static calls to QFileDialog and QMessageBox but the language is only changed in QMessageBox, not in QFileDialog. For both classes I'm only calling the static members to that can't be the issue. I also tried to install this translator in the main.cpp with the same results.
By default, QFileDialog will use the native file browser rather than a custom Qt-based dialog. The native file browser is will be using the OS language, rather than the Qt language, and will not have Qt translations applied to it. You can override this behaviour using the DontUseNativeDialog option for QFileDialog.

store input of line edit to string qt

I'm a beginner.I am making a simple gui program using qt in which you enter a url/website and that program will open that webpage in chrome.I used line edit in which user enters url and i used returnPressed() slot, but the problem is (it might sound stupid) that i don't know how to take the input by user and store it in a string so that i can pass that string as parameter to chrome.Is im asking something wrong.also tell me how can i save input to a txt file, i know how to do that in a console program.Is this process is same with others like text edit etc.
My mainwindow.cpp:
QString exeloc = "F:\\Users\\Amol-2\\AppData\\Local\\Google\\Chrome\\Application\\chrome.exe";
void MainWindow::on_site_returnPressed()
{
QString site;
getwchar(site);
QString space=" ";
QString result = exeloc + space + site;
QProcess::execute(result);
}
What im doing wrong.
thanks
You've got your approach slightly wrong, I can see where you're coming from though. It's actually a lot more simple than you're trying, Qt has a QDesktopServices class that allows you to interact with various system items, including open urls in the browser. There's documentation on it here.
QLineEdit has a text() function that will return a QString. So you can do something like this:
QString site = ui->site->text();
You don't have to use QProcess to open a web site in a browser. You can use QDesktopServices::openUrl static function.
Like this:
QString site = ui->site->text();
QUrl url(site);
QDesktopServices::openUrl(url);
Remember to include QDesktopServices and QUrl headers:
#include <QDesktopServices>
#include <QUrl>

Qt: How can I access the actual widgets on a page in WebKit?

Is there a way to access the widgets generated by INPUT and SELECT on a page in WebKit, using Qt?
On a related note, does WebKit provide these widgets, or does it delegate back to Qt to generate them?
There are no "widgets". Newer browsers render all elements themselves to allow overlays etc.
If you want to manipulate them use the DOM.
Everything inside in QWebView does not use the conventional Qt widget system. It's only HTML, rendered by WebKit. But you can access to html by using the evalJS function. Example of code:
QString Widget::evalJS(const QString &js)
{
QWebFrame *frame = ui->webView->page()->mainFrame();
return frame->evaluateJavaScript(js).toString();
}
evalJS(QString("document.forms[\"f\"].text.value = \"%1\";").arg(fromText));
evalJS(QString("document.forms[\"f\"].langSelect.value = \"%1\";").arg(langText));
evalJS(QString("translate()"));
QString from = evalJS("document.forms[\"f\"].text.value");
QString translation = evalJS("document.forms[\"f\"].translation.value");
ui->textEditTo->setText(translation);