0

I'm writing a program in C++ and qt4 that is supposed to generate a various number (hundreds )of clickable links in a QTextBrowser depending on data from input files.

The idea is that when the user clicks one of these links a value will be passed to a function named 'on_QTextBrowser_anchorClicked(QUrl)'.

I have created a QTextBrowser which displays HTML code and I manage to create different links to every element added. The problem lies within passing the Url defined in href="URL" to the QUrl.

When setOpenLinks for the QTextBrowser is "true", and I print the URL I get the correct result. But I can't pass this URL (which is a value and not a real URL) to a function. When setOpenLinks is "false", the anchorClicked(Url) function passes "" as the Url and here I would like to have the URL printed when setOpenLinks=true.

How can I achieve this? Is there a better way (probably is) to connect a various number (approx. between 50-1000) of generated links to a function using QTextBrowser.

My code:

Compassindex.cpp

void CompassIndex::on_seqBrowser_anchorClicked(QUrl input)
{
    QString Input = input.toString();
    QByteArray nByte = Input.toUtf8();
    std::cerr<<Input.data();         //Print Url on the screen to ensure value is passed
}

void CompassIndex::readfile()
{
    QString Line;
    Int number_out=0;
    ...                //Imports data that will be printed in the QTextBrowser
    ...                //loop for creating links for n number of elements
    Line="<a href=\"";
    Line+=number_out;  //the value (fake Url) that I want to pass to anchorClicked()
    Line+="\">";
    Line+=nameArr[n];  //inserts the name for the link
    Line+="</a>";
    number_out++;
    ...
    ui->seqBrowser->insertHtml(Line);
    ...                //end of loop

}

Many thanks for your reply!

1 Answers1

0

I'm using QTextBrowser to provide an actionable user interface for pqConsole. You can read more about in this answer.

I pass around Prolog invocations, and all seems to be working. My anchorClicked slot is as simple as

void ConsoleEdit::anchorClicked(const QUrl &url) {
    query_run(url.toString());
}

Note I also have defined (as dummy)

void ConsoleEdit::setSource(const QUrl &name) {
    qDebug() << "setSource" << name;
}

I don't touch setOpenLinks(), that defaults to true according to the docs.

Community
  • 1
  • 1
CapelliC
  • 59,646
  • 5
  • 47
  • 90