April 25, 2012

eikeR eikeR
Lab Rat
27 posts

[Solved] Advice needed: Reload page on “F5”

 

[ It should be easy to solve, but I found no good starting point. ]

A reload in my application (i.e. a QWebView that loads a html page) already works because I can right-click on a page and select ‘Reload’. This seems to be enabled by default, I did not have to do anything to enable this.

What do I have to do, to achieve a reload by just pressing F5 as we know it from standard browsers? A hint to lead me into the right direction should be enough.

Thanks in advance!

11 replies

April 25, 2012

miroslav miroslav
Ant Farmer
228 posts

If you are reimplementing the web view, you can handle keyPressEvents, and catch F5. If not, you could use an event filter.

 Signature 

Mirko Boehm | .(JavaScript must be enabled to view this email address) | KDE e.V.
FSFE Fellow
Qt Certified Specialist

April 25, 2012

Volker Volker
Robot Herder
5428 posts

I would try to add a QAction together with a shortcut to the webview. You don’t need to subclass for this approach, just set the webview as the parent of the action.

April 26, 2012

AcerExtensa AcerExtensa
Robot Herder
569 posts

  1. QKeySequence keys_refresh(Qt::Key_F5);
  2. this->action_refresh = new QAction(this->view);
  3. this->action_refresh->setShortcut(keys_refresh);
  4. connect(this->action_refresh, SIGNAL(triggered()),this->view,SLOT(reload()));

 Signature 

God is Real unless explicitly declared as Integer.

April 26, 2012

Volker Volker
Robot Herder
5428 posts

Better would be to use

  1. QKeySequence keys_refresh(QKeySequence::Refresh);

This way you get the key sequence the user expects on his platform.

April 26, 2012

AcerExtensa AcerExtensa
Robot Herder
569 posts

Topic subject is : Reload page on “F5” ;)

 Signature 

God is Real unless explicitly declared as Integer.

April 26, 2012

AcerExtensa AcerExtensa
Robot Herder
569 posts

But I think QKeySequence::Refresh & Qt::Key_F5 are synonyms, at least for X11 & WIN platforms. isn’t they?

 Signature 

God is Real unless explicitly declared as Integer.

April 26, 2012

Volker Volker
Robot Herder
5428 posts

Yes, they are. And if you’re only on platforms that use F5, it makes no difference for you. But why use something hardcoded, when there’s a general solution.

And if someone is not aware of the standard key sequences, how should that be reflected in the topic title?

April 26, 2012

eikeR eikeR
Lab Rat
27 posts

Thanks so far!
Unfortunately it’s not working. Can you see what’s wrong? For ease of use, I put the code in the main.cpp, which I will not do in the end, but right now it makes it easier to trace my mistake…

  1. //extract from main.cpp:
  2. QApplication app(argc, argv);
  3. QWebView window;
  4. //[ ... some connects on window->page() and window->page()->mainFrame() ...]
  5. QKeySequence keys_refresh(QKeySequence::Refresh);  //or (Qt::Key_F5), doesn't matter
  6. QAction* actionReload = new QAction(&window);
  7. actionReload->setShortcut(keys_refresh);
  8. QObject::connect(actionReload, SIGNAL(triggered()), &window, SLOT(reload()));
  9.  
  10. window.load(QUrl("file:///C:/start.html"));
  11. window.show();
  12. app.exec();

If I make right-click -> reload on my html page, the page gets reloaded, but not if I press F5.
[Edit: Even if I make s subclass and override the reload() slot, I see that the slot is not called. So either the action is not triggered, or the signal-slot connection is wrong]

April 26, 2012

AcerExtensa AcerExtensa
Robot Herder
569 posts

you have forget to add action to WebView:

  1. window.addAction(actionReload);

 Signature 

God is Real unless explicitly declared as Integer.

April 26, 2012

eikeR eikeR
Lab Rat
27 posts

AcerExtensa wrote:
you have forget to add action to WebView:

@window.addAction(actionReload);@

Great, I was just wondering what kind of black magic binds the action to the window :-) Okay, that’s it. Thank you all (also thanks for the first answer, maybe I will also have to use the keyPressEvent() override for some other stuff, but for the reload it’s easier to use QActions.)

April 26, 2012

sierdzio sierdzio
Area 51 Engineer
2333 posts

If nothing changed during my lengthy absence from this forum, you should now change the topic’s title/ subject to include “[Solved]” at the beginning. I’ve also tagged it for you.

 Signature 

(Z(:^

 
  ‹‹ [Solved] ’webivew’ does not name a type      How to update QtWebKit ››

You must log in to post a reply. Not a member yet? Register here!