Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] Problem with anchorClicked() signal in QTextBrowser
QtWS25 Last Chance

[Solved] Problem with anchorClicked() signal in QTextBrowser

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 2.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Macro
    wrote on last edited by
    #1

    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();
    }@

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • M Offline
        M Offline
        Macro
        wrote on last edited by
        #3

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

        @textBrowser->setOpenExternalLinks(false);@

        but it was not working.

        @textBrowser->setOpenLinks(false);@

        this works fine.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved