PDA

View Full Version : Qwebview Copy Text And Save Image



ShapeShiftme
29th May 2011, 07:49
Good Dall All I have a very basix qwebview, and i woul like to be able to copy text of the page using ctrl -C which does not seem to work. And try save an image


1) Copy text
I have tried the following on the page where my webform loads


QAction *act = ui->webView->pageAction(QWebPage::Copy);
act->setShortcut(QKeySequence::Copy);


When i press Ctrl C And try and paste the text somewhere else nothing happens.


2) Saving An IMage

When i right click on an image on the qwebview i get 4 default options
Open IMage
Save Image
Copy Image
Inspect

Now no matter what option i select nothing happens.
Could someone supply me with some code to get them to work.

Any help will be appreciated

REgards

ShapeShiftme
31st May 2011, 05:38
Good day again.

I have kept trying to solve the problem at hand. Is perhaps the problem a qt bug on webkit. could someone please spend some time trying to help me out.

Regards

BTW im Running Linux kubuntu 11.04 With QT creator And webkit 2.1 apparently

arturo182
31st May 2011, 23:14
I had the same issue with the copy shortcut, I managed to do it by installing an eventFilter on QWebView:

if((keyEvent->key() == Qt::Key_C) && (keyEvent->modifiers().testFlag(Qt::ControlModifier))) {
qApp->clipboard()->setText(m_wvLog->selectedText());
}

And for the image problems, I don't know about Opening and copying but clicking "Save image" emits QWebPage::downloadRequested(QNetworkRequest) signal and you have to do the downloading yourself.

ShapeShiftme
1st June 2011, 06:19
Thanks arturo182.
That worked great. And im greatfull for you putting me in the right direction for the image downloading.

Below is the full code for anyone looking at this thread. I hope it helps someone as this task has wasted me 2 days

Header File


private slots:
void downloadRequested(const QNetworkRequest &); //used for save image
void downloadingIssueFinished(); //used for save image
void keyPressEvent(QKeyEvent *e); //used for crtl-c



cpp file - the below code was taken from http://www.linuxjournal.com/magazine/using-webkit-your-desktop-application

this code is for the same image bit of the fix


void frm_web::downloadRequested(
const QNetworkRequest &request)
{
// First prompted with a file dialog to make sure
// they want the file and to select a download
// location and name.
QString defaultFileName =
QFileInfo(request.url().toString()).fileName();
QString fileName =
QFileDialog::getSaveFileName(this,
tr("Save File"),
defaultFileName);
if (fileName.isEmpty())
return;

// Construct a new request that stores the
// file name that should be used when the
// download is complete
QNetworkRequest newRequest = request;
newRequest.setAttribute(QNetworkRequest::User,
fileName);

// Ask the network manager to download
// the file and connect to the progress
// and finished signals.
QNetworkAccessManager *networkManager =
ui->webView->page()->networkAccessManager();
QNetworkReply *reply =
networkManager->get(newRequest);
//connect(
//reply, SIGNAL(downloadProgress(qint64, qint64)),
//this, SLOT(downloadProgress(qint64, qint64)));
connect(reply, SIGNAL(finished()),
this, SLOT(downloadingIssueFinished()));
}


void frm_web::downloadingIssueFinished()
{
QNetworkReply *reply = ((QNetworkReply*)sender());
QNetworkRequest request = reply->request();
QVariant v =
request.attribute(QNetworkRequest::User);
QString fileName = v.toString();
QFile file(fileName);
if (file.open(QFile::ReadWrite))
file.write(reply->readAll());
}

This is for the Ctrl-C Bit



void frm_web::keyPressEvent(QKeyEvent *e)
{


if((e->key() == Qt::Key_C) && (e->modifiers().testFlag(Qt::ControlModifier))) {
qApp->clipboard()->setText(ui->webView->selectedText());
}
}


And to connect the signals and slots



connect(ui->webView->page(),
SIGNAL(downloadRequested(QNetworkRequest)),
this, SLOT(downloadRequested(QNetworkRequest)));

kulteam
11th August 2016, 06:30
arturo182's Thanks for u !