Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    [Solved] Problem with anchorClicked() signal in QTextBrowser

    General and Desktop
    2
    3
    2064
    Loading More Posts
    • 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
      Macro last edited by

      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 Reply Quote 0
      • Chris Kawa
        Chris Kawa Moderators last edited by

        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 Reply Quote 0
        • M
          Macro last edited by

          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 Reply Quote 0
          • First post
            Last post