Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

[Solved] Problem with anchorClicked() signal in QTextBrowser



  • I tried an example using a QTextBrowser, in which I add a hyperlink. When I click on that link, the text(link) gets deleted from the QTextBrowser. Why it is getting deleted?

    I want to open a new QDialog when i click on that hyperlink. So i tried using anchorClicked(QUrl) signal.

    I found that when this signal is fired the QTextBrowser's text(hyperlink) gets deleted. Please suggest where I am going wrong.....

    MyWidget.h
    @#ifndef MYWIDGET_H
    #define MYWIDGET_H

    #include <QWidget>
    #include <QTextBrowser>
    #include <QVBoxLayout>

    namespace Ui {
    class MyWidget;
    }

    class MyWidget : public QWidget
    {
    Q_OBJECT

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

    private:
    Ui::MyWidget *ui;
    QTextBrowser *textBrowser;

    private slots:
    void MyOwnSlot(const QUrl &url);
    };

    #endif // MYWIDGET_H@

    MyWidget.cpp
    @#include "MyWidget.h"
    #include "ui_MyWidget.h"
    #include <QDebug>
    #include <QDialog>

    MyWidget::MyWidget(QWidget parent) :
    QWidget(parent),
    ui(new Ui::MyWidget)
    {
    textBrowser = new QTextBrowser(this);
    textBrowser->setHtml("<a href='openMyDialog'>myDialog</a>");
    QVBoxLayout
    layout = new QVBoxLayout;
    layout->addWidget(textBrowser);
    setLayout(layout);

    connect(textBrowser, SIGNAL(anchorClicked(QUrl)), this, SLOT(MyOwnSlot(QUrl)));
    

    }

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

    void MyWidget::MyOwnSlot(const QUrl &url)
    {
    QDialog *dlg = new QDialog(this);
    dlg->exec();
    }
    @

    main.cpp
    @#include "MyWidget.h"
    #include <QApplication>

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    MyWidget w;
    w.show();
    return a.exec();
    }@


  • Moderators

    It has nothing to do with the dialog. Your browser just automatically navigates to the clicked url. If you want to prevent this behavior add
    @
    textBrowser->setOpenLinks(false);
    @

    Unrelated comment:
    I know that it's just an example but please don't do that:
    @
    void MyWidget::MyOwnSlot(const QUrl &url)
    {
    QDialog *dlg = new QDialog(this);
    dlg->exec();
    }
    @
    If you click the link 100 times you've got 100 instances of dialog residing in memory until the main window is destroyed. That's bad. If it's a disposable dialog just make a local variable of it, not a dynamic instance:
    @
    void MyWidget::MyOwnSlot(const QUrl &url)
    {
    QDialog dlg(this);
    dlg.exec();
    }
    @
    or manually delete the instance if you need (usually you don't) to allocate dynamically.



  • Thanks for your reply Chris Kawa.. I was trying with

    @textBrowser->setOpenExternalLinks(false);@

    but it was not working.

    @textBrowser->setOpenLinks(false);@

    this works fine.


Log in to reply