c++qtqt5qtextbrowser

How to add hyperlinks and user clickable actions into qtextbrowser


Currently, I am adding text data into qtextbrowser. In this data, there is a hyperlink(that I am capturing using qregex) and a filepath(for one of my directories). Now, on click of the hyperlink, I want to open the link in one of the browsers of linux. Here I am able to open the hyperlinks in the qtextbrowser but not in the external tools. And on click of filepath, I have to open a terminal at that location.I couldn't how to add actions to the text in the qt.

I couldn't find any appropriate solution.

The code for appending hyperlink: and dirPath

.hpp file

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
    class MainWindow;
}

class MainWindow: public QMainWindow{
       Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

.cpp file

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    QString hyperLink = QString::fromStdString("https://www.google.co.in/");
    hyperLink = QString::fromStdString("<a href = \"") + hyperLink + 
QString::fromStdString("\" >") + hyperLink + QString::fromStdString("</a>");
    hyperLink = QString::fromStdString("HyperLink: ") + hyperLink;
    ui->textBrowser->append(hyperLink);

    QString dirLocation = QString::fromStdString("/home/user/dir");
    dirLocation = QString::fromStdString("<a href = \"") + dirLocation +
    QString::fromStdString("\" >") + dirLocation + QString::fromStdString("</a>");
    dirLocation = QString::fromStdString("Working Directory: ") + dirLocation;
    ui->textBrowser->append(dirLocation);
}

MainWindow::~MainWindow()
{
    delete ui;
}

On the click of the hyperLink, I want to open the link in the default browser of the system.

On the click of the dirPath(that I am currently appending as hyperLink), I want to open the terminal at the dirPath.

ui->textbrowser is QWidget of QTextBrowser.

I am working in a Linux OS

Thank you for help in advance.


Solution

  • As we are going to select the type of action the correct thing is to deactivate the actions that generate the link within the browser to do so the following is used:

    ui->textBrowser->setOpenLinks(false);
    ui->textBrowser->setOpenExternalLinks(false);
    

    Then connect the signal anchorClicked to some slot where we will work the desired logic:

    *.h

    private slots:
        void onAnchorClicked(const QUrl &link);
    

    *.cpp

    connect(ui->textBrowser, &QTextBrowser::anchorClicked, this, &MainWindow::onAnchorClicked);
    // old style
    // connect(ui->textBrowser, SIGNAL(anchorClicked(QUrl)), this, SLOT(onAnchorClicked(QUrl)));
    

    Then we check if the link is a directory, otherwise it will try to open it through QDesktopServices::openUrl(), if it is try to open it but for this there is a caveat, the command to open a terminal depends on the terminal in Linux there are many applications that emulate the terminal so there is no exact answer, in this case assume that you have as desktop manager to Gnome and then use your terminal).

    void MainWindow::onAnchorClicked(const QUrl &link)
    {
        if(!QFileInfo(link.toString()).isDir()){
            QDesktopServices::openUrl(link);
        }
        else{
            QProcess::startDetached(QString("gnome-terminal --working-directory=%1").arg(link.toString()));
        }
    }
    

    The complete example can be found in the following link