We already have QWebView implementation and now we want to migrate to QWebEngineView.
Through QWebView we have registered below signal to receive notification for any link is clicked on webview or not and we are getting signal in QWebView.
connect(m_WebView, SIGNAL(linkClicked(const QUrl &)),SLOT(urlLinkClicked(const QUrl &)));
In "urlLinkClicked" slot, we are opening new tab and open that URL into new tab.
We are facing some issue with QWebEngineView. As there is no such signal "linkClicked" exist in QWebEngineView. So we have tried below options but still not able to find the solution.
In main class, we have created WebEngineView class instance and setting WebEnginePage. We are able to render the website in view class but when we click on any link then we are not getting any signal so we are not able to open that new website in new tab.
m_WebEngineView = new QWebEngineView(this);
m_WebEngineView->setPage(new QWebEnginePage());
We have also override "acceptNavigationRequest" method to get the link clicked event in mainWebEngineView but we are not able to get the link clicked event.
Any suggestion i can try ?
Thanks in Advance.
Override QWebEnginePage::acceptNavigationRequest in QWebEnginePage subclass:
bool MyWebPage::acceptNavigationRequest(const QUrl &url, NavigationType type, bool isMainFrame)
{
if (type == QWebEnginePage::NavigationTypeLinkClicked)
{
qDebug() << url;
}
return true;
}
use this in inherited WebEngineView and in inherited webpage we can find hoverdUrl
QWebEngineView *WebEngineView::createWindow(QWebEnginePage::WebWindowType type)
{
if(type==QWebEnginePage::WebBrowserTab)
{
if(!hoverdUrl.isEmpty())
QDesktopServices::openUrl(QUrl(hoverdUrl));
qGlobalDbg("Open external Url requiested in chat, url : " + hoverdUrl, toKIBANA|toLOG);
}
//qDebug()<<"============== link Clicked "<<hoverdUrl;
return NULL;
}
I think you could use the signal "urlChanged".
See the header "Signals" on there official documentation page.
http://doc.qt.io/qt-5/qwebengineview.html
If this does not help I need to know what version of the Qt framework you are using.
Best Regards
/ Rasmus
Unfortunately urlChanged signal that QWebenginePage has emits only when url of current page is changed. Previous linkClicked signal was also emitted when url of current page was not changed. There is a way to handle this, but you would need to have access to page source code.
This functiionality is achieved through QWebChannel class. You would need to create webChannel object and a special callback class, which would handle call backs from webpage in a way you like. Then you would need to set this webChanell on a page you want to and do all the connections like this:
MyCallBackObject* callback= new MyCallBackClass();
mWebChannel = new QWebChannel(this);
mWebChannel->registerObject(QStringLiteral("MyCallBackObject"), callback);
mWebView->page()->setWebChannel(mWebChannel); // mWebView is QWebEngineView
connect(callback, SIGNAL(urlChanged(QUrl)),
this, SLOT(linkClickedSlot(QUrl))); // connect statement, urlChanged is defined in your callback class, linkClickedSlot is a slot where you process clicked signal
For more reference - please use official example from qt or the one from kdab or this video from Qt Developer Days conference
Related
I have two forms one is trainee_view.ui
and other is enter_new_trainee.ui
so for that i have trainee_view.cpp,trainee_view.h to see the list of Trainee in DB
and enter_new_trainee.cpp,enter_new_trainee.h to enter new trainee details
now in trainee_view.ui i have a push button "ADD Trainee"
so if i click this button it will go to "enter_new_trainee.ui"
void trainee_view::on_pushButton_2_clicked()
{
newtrainee=new enter_new_trainee(this);
newtrainee->setWindowFlags(Qt::Window);
newtrainee->show();
// connect(newtrainee, SIGNAL(destroyed()), this, SLOT(refresh_form()));
}
so by using connect() i am trying to refresh the trainee_view after entering the new trainee details. so how can i emmit the signal from
2nd form to 1st form such that i call refresh_form() method in 1st form .
I tried to use destroyed() signal on newtrainee but could not refresh my trainee_view form.
To be MOre simple . i just want to get an object is destroyed or not so if destroyed i can call refresh() method to load back the changes done on widget
for that i opted connect() method so how should i call that. becoz if i call
connect(newtrainee, SIGNAL(destroyed()), this, SLOT(refresh_form()));
there is no effect i.e nothing is loading into the view.
am newbie to qt so pls try to help me.
Thank YOu.
I'm not sure if I correctly understand your app, but I think you misunderstand the concept of Signals and Slots. Look here for some examples. In some simplification you can look at signal and slots this way: connect() command is a place which will not do anything - it just stay and keep listening for a signal. So you should place it in trainee_view.cpp. That's the first part and I see you did it correct, or almost correct. But you need also something that will send the signal, and this is exactly what emit() command do - it should be placed in enter_new_trainee.cpp just after description of generation new entry. For example, let assume user input new entry in LineEdit in UI:
[...]
QString newEntry = ui->LineEdit->text(); //Save entry to variable
emit(newEntry); //Emit it to signal slot
[...]
I have some labels and layouts nested inside a QWidget to build a part of a sidebar. Each QWidget is its own section and one component currently looks like this:
To my understanding, you can only set hyperlinks with QLabel, but I'm trying to get the whole area between the white lines clickable. This is including the icon and the whitespace. Is there any way to achieve this?
This got marked as a duplicate to the opposite of what I was asking, so I'd like to reiterate that I'm trying to implement a hyperlink without QLabel.
You can easily have a widget open a link on click:
class Link : public QWidget {
Q_OBJECT
public:
Link(QUrl url, QWidget p = nullptr) : QWidget(p), _url(url) {}
QUrl _url;
void mouseReleaseEvent(QMouseEvent *) { QDesktopServices::openUrl(_url); }
}
You can avoid any extra signals and connections, and have each link widget store its own link internally, the url can be set on construction and changed at any time. Not using signals and slots makes it easier to change the link too, without having to disconnect previous connections.
IMO going for a signals and slots solution is only justified when you want different arbitrary behavior. In this case you always want the same - to open a particular link, so you might as well hardcode that and go for an easier and more computationally efficient solution.
I would just manually catch the SIGNAL for clicked() and use desktop services to open the url in code.
bool QDesktopServices::openUrl ( const QUrl & url ) [static]
Opens the given url in the appropriate Web browser for the user's desktop environment, and returns true if successful; otherwise returns false.
http://doc.qt.io/qt-4.8/signalsandslots.html
Using this type of syntax, or in the designer, you can also connect a signal to a slot.
connect(widgetThatRepresentsURL, SIGNAL(clicked()),
handlerThatWillOpenTheURL, SLOT(clicked_on_url()));
For widgets that don't have a signal set up for clicked (or whatever event you are interested in), you can subclass the widget in question and reimplement...
void QWidget::mousePressEvent ( QMouseEvent * event ) [virtual protected]
Specifically for creating a signal, there is emit. I've used this in the past like the following
void Cell::focusInEvent(QFocusEvent *e)
{
emit focus(this, true);
QLineEdit::focusInEvent(e);
}
with the following in the header
signals:
void focus(Cell *, bool);
I've followed instructions given on previous questions like this
so now if I put a link to a regular page it opens fine with the default browser. But if I want to open a mailto link from QT QLabel 4.8.6 the link does nothing.
What am I doing wrong?
here is the code:
UpgradeMessageDialog* umd = new UpgradeMessageDialog();
umd->ui->label->setOpenExternalLinks(true);
umd->ui->label->setTextInteractionFlags(Qt::TextBrowserInteraction);
umd->ui->label->setText("<a href='mailto:user#foo.com?subject=Test&body=Just a test'>My link</a>");
umd->exec();
umd->ui->label->connect(umd->ui->label,
SIGNAL(linkActivated(const QString&)), umd,
SLOT(linkOpen(const QString&)));
(this is defined as a public slot in the appropriate h file)
void UpgradeMessageDialog::linkOpen(const QString &link)
{
QDesktopServices::openUrl(QUrl(link));
}
Just to clarify: I have a default mail program set up in my computer, and when I type mailto:a#b.c in the browser that program opens fine.
First, there are two ways to handle link activation in QLabel. You should use one of them, but I see you are trying to use both.
This two ways are:
Call openExternalLinks(true), so that QLabel will automatically open links using QDesktopServices::openUrl() instead of emitting the linkActivated() signal.
Connect to the linkActivated() signal and then manually open link in the connected slot (by calling QDesktopServices::openUrl() for example).
Also you use the exec() function wrong. You should put the exec() call after the connect() call, because exec() is blocking so the signal connection will actually happened after the dialog is closed.
So your code should be like this:
umd->ui->label->setText("<a href='mailto:user#foo.com?subject=Test&body=Just a test'>My link</a>");
connect(umd->ui->label, SIGNAL(linkActivated(QString)), umd, SLOT(linkOpen(QString)));
umd->exec();
or like this:
umd->ui->label->setTextFormat(Qt::RichText);
umd->ui->label->setTextInteractionFlags(Qt::TextBrowserInteraction);
umd->ui->label->setOpenExternalLinks(true);
umd->ui->label->setText("<a href='mailto:user#foo.com?subject=Test&body=Just a test'>My link</a>");
And a little advise: put the label initialization code into the UpgradeMessageDialog constructor.
UpgradeMessageDialog::UpgradeMessageDialog(QDialog* parent) : QDialog(parent)
{
ui->label->setTextFormat(Qt::RichText);
ui->label->setTextInteractionFlags(Qt::TextBrowserInteraction);
ui->label->openExternalLinks(true);
ui->label->setText("<a href='mailto:user#foo.com?subject=Test&body=Just a test'>My link</a>");
}
And then you can use your dialog this way:
QScopedPointer<UpgradeMessageDialog> umd = new UpgradeMessageDialog;
umd->exec();
#include <QUrl>
#include <QDesktopServices>
myLabel = new QLabel(this);
myLabel->setTextFormat(Qt::RichText);
myLabel->setText("Email:href='mailto:serge#essetee.be'>serge#essetee.be</a>");
myLabel->setOpenExternalLinks(true);
Now you just have to click the link and the standard mail client will be launched.
I created a QMessageBox with an html link:
QTMessageBox msgBox(Utility::UI::topLevelWidget());
msgBox.setText("Link");
msgBox.exec();
If I left click the link a new web browser tab opens. The problem is that the url http://www.example.cz/?url=www**%2525**www is opened instead of http://www.example.cz/?url=www**%25**www
How do I prevent such behavior?
UPDATE: If I right click the link, choose "Copy link" and paste it into the browser, the link is ok.
That's because % has the html encoding %25. So %25 -> %2525.
Why does Qt encode the links automatically?
In the QMessageBox, there is a QLabel. The label uses the Qt::TextFormat Qt::AutoText by default. Therefore, it detects in your text, that it is html encoded and generates the link.
The QLabel sends the signal linkActivated(const QString& link) or uses QDesktopServices::openUrl(), depending its the boolean openExternalLinks.
It seems, that the QMessageBox sets openExternalLinks to true.
Since the link will be used as input for a QUrl, it will be parsed. That's the reason for the double encoding.
It is possible, to modify the behavior of QDesktopServices::openUrl() by using its static method void QDesktopServices::setUrlHandler. I implemented and tested it for the desired behavior:
MyUrlHandler urlHandler;
QDesktopServices::setUrlHandler( "http", &urlHandler, "handleUrl" );
QMessageBox msgBox;
msgBox.setText( "Link" );
msgBox.show();
Using the class MyUrlHandler:
class MyUrlHandler : public QObject
{
Q_OBJECT
public:
MyUrlHandler(QObject* parent=0):QObject(parent){}
public slots:
void handleUrl(const QUrl &url)
{
QDesktopServices::openUrl( QUrl::fromEncoded( url.toString().toAscii() ) );
}
};
The trick is simple, I set the link address directly to the QUrl instance as already valid url.
But unfortunately, it modifies the behavior globally.
how can I make a QLabel to behave like a link? What I mean is that I'd like to be able to click on it and then this would invoke some command on it.
QLabel does this already.
Sample code:
myLabel->setText("Click Here!");
myLabel->setTextFormat(Qt::RichText);
myLabel->setTextInteractionFlags(Qt::TextBrowserInteraction);
myLabel->setOpenExternalLinks(true);
The answer from cmannnett85 is fine if you just want to open a URL when the link is clicked, and you are OK with embedding that URL in the text field of the label. If you want to do something slightly custom, do this:
QLabel * myLabel = new QLabel();
myLabel->setName("myLabel");
myLabel->setText("text");
myLabel->setTextInteractionFlags(Qt::TextBrowserInteraction);
Then you can connect the linkActivated signal of the label to a slot, and do whatever you want in that slot. (This answer assumes you have basic familiarity with Qt's signals and slots.)
The slot might look something like this:
void MainWindow::on_myLabel_linkActivated(const QString & link)
{
QDesktopServices::openUrl(QUrl("http://www.example.com/"));
}