Results 1 to 5 of 5

Thread: Qwebview Copy Text And Save Image

  1. #1
    Join Date
    Dec 2010
    Location
    South Africa
    Posts
    56
    Thanks
    8
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Qwebview Copy Text And Save Image

    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

    Qt Code:
    1. QAction *act = ui->webView->pageAction(QWebPage::Copy);
    2. act->setShortcut(QKeySequence::Copy);
    To copy to clipboard, switch view to plain text mode 


    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

  2. The following user says thank you to ShapeShiftme for this useful post:

    Mohammad_Aqajani (6th January 2021)

  3. #2
    Join Date
    Dec 2010
    Location
    South Africa
    Posts
    56
    Thanks
    8
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: Qwebview Copy Text And Save Image

    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
    Last edited by ShapeShiftme; 31st May 2011 at 06:42. Reason: Added system os

  4. #3
    Join Date
    Mar 2009
    Location
    Belchatow, Poland
    Posts
    38
    Thanks
    11
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows Symbian S60

    Default Re: Qwebview Copy Text And Save Image

    I had the same issue with the copy shortcut, I managed to do it by installing an eventFilter on QWebView:
    Qt Code:
    1. if((keyEvent->key() == Qt::Key_C) && (keyEvent->modifiers().testFlag(Qt::ControlModifier))) {
    2. qApp->clipboard()->setText(m_wvLog->selectedText());
    3. }
    To copy to clipboard, switch view to plain text mode 

    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.

  5. The following user says thank you to arturo182 for this useful post:

    ShapeShiftme (1st June 2011)

  6. #4
    Join Date
    Dec 2010
    Location
    South Africa
    Posts
    56
    Thanks
    8
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: Qwebview Copy Text And Save Image

    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
    Qt Code:
    1. private slots:
    2. void downloadRequested(const QNetworkRequest &); //used for save image
    3. void downloadingIssueFinished(); //used for save image
    4. void keyPressEvent(QKeyEvent *e); //used for crtl-c
    To copy to clipboard, switch view to plain text mode 


    cpp file - the below code was taken from http://www.linuxjournal.com/magazine...op-application

    this code is for the same image bit of the fix

    Qt Code:
    1. void frm_web::downloadRequested(
    2. const QNetworkRequest &request)
    3. {
    4. // First prompted with a file dialog to make sure
    5. // they want the file and to select a download
    6. // location and name.
    7. QString defaultFileName =
    8. QFileInfo(request.url().toString()).fileName();
    9. QString fileName =
    10. QFileDialog::getSaveFileName(this,
    11. tr("Save File"),
    12. defaultFileName);
    13. if (fileName.isEmpty())
    14. return;
    15.  
    16. // Construct a new request that stores the
    17. // file name that should be used when the
    18. // download is complete
    19. QNetworkRequest newRequest = request;
    20. newRequest.setAttribute(QNetworkRequest::User,
    21. fileName);
    22.  
    23. // Ask the network manager to download
    24. // the file and connect to the progress
    25. // and finished signals.
    26. QNetworkAccessManager *networkManager =
    27. ui->webView->page()->networkAccessManager();
    28. QNetworkReply *reply =
    29. networkManager->get(newRequest);
    30. //connect(
    31. //reply, SIGNAL(downloadProgress(qint64, qint64)),
    32. //this, SLOT(downloadProgress(qint64, qint64)));
    33. connect(reply, SIGNAL(finished()),
    34. this, SLOT(downloadingIssueFinished()));
    35. }
    36.  
    37.  
    38. void frm_web::downloadingIssueFinished()
    39. {
    40. QNetworkReply *reply = ((QNetworkReply*)sender());
    41. QNetworkRequest request = reply->request();
    42. request.attribute(QNetworkRequest::User);
    43. QString fileName = v.toString();
    44. QFile file(fileName);
    45. if (file.open(QFile::ReadWrite))
    46. file.write(reply->readAll());
    47. }
    To copy to clipboard, switch view to plain text mode 

    This is for the Ctrl-C Bit

    Qt Code:
    1. void frm_web::keyPressEvent(QKeyEvent *e)
    2. {
    3.  
    4.  
    5. if((e->key() == Qt::Key_C) && (e->modifiers().testFlag(Qt::ControlModifier))) {
    6. qApp->clipboard()->setText(ui->webView->selectedText());
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 

    And to connect the signals and slots

    Qt Code:
    1. connect(ui->webView->page(),
    2. SIGNAL(downloadRequested(QNetworkRequest)),
    3. this, SLOT(downloadRequested(QNetworkRequest)));
    To copy to clipboard, switch view to plain text mode 

  7. #5
    Join Date
    Aug 2016
    Posts
    1
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Qwebview Copy Text And Save Image

    arturo182's Thanks for u !

Similar Threads

  1. QWebView Ctrl+C for copy isn't working, how to fix ?
    By rsilva in forum Qt Programming
    Replies: 4
    Last Post: 13th September 2011, 14:42
  2. Replies: 1
    Last Post: 29th November 2009, 21:33
  3. How to copy sellected ellipse area from image
    By adamsakli in forum Newbie
    Replies: 2
    Last Post: 24th September 2009, 23:11
  4. How to save a Qwebview as an html file
    By richardander in forum Qt Programming
    Replies: 2
    Last Post: 5th June 2009, 01:07
  5. copy string from text box
    By Rekha in forum Newbie
    Replies: 6
    Last Post: 27th July 2006, 20:21

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.