Getting QHelpContentWidget QUrl with Single-Click
-
The linkActivated() signal of QHelpContentWidget only triggers on a double-click but I want to trigger on a single-click.
I am able to successfully trigger a slot on clicked() and selectionChanged(int index) but I don't know how to get the QUrl from the QHelpContentWidget with the index.
I also tried helpEngine->contentWidget()->setEditTriggers(QAbstractItemView::CurrentChanged); without success.
Does anyone know how to get a QUrl from QHelpContentWidget using an int index?
-
I learned 2 things:
- I need to access the QUrl directly from QHelpEngine, not its QHelpContentWidget.
- I cannot access QHelpEngine content using a int index, I must use a QModelIndex.
Here's my current solution:
QObject::connect(helpEngine->contentWidget(),QHelpContentWidget::clicked,this,MainWindow::testSlot);
void MainWindow::testSlot(){
QModelIndex testIndex = helpEngine->contentWidget()->currentIndex();
QHelpContentItem *item = helpEngine->contentModel()->contentItemAt(testIndex);
QUrl testUrl = item->url();
textViewer->setSource(testUrl);
return;
} -
Here's a better way that passes the clicked index instead of checking for the currently selected index:
QObject::connect(helpEngine->contentWidget(),QHelpIndexWidget::clicked,this,HelpMenu::onContentClick);
void HelpMenu::onContentClick(QModelIndex testIndex){ QVariant v = helpEngine->indexModel()->data(testIndex, Qt::DisplayRole); QMap<QString,QUrl> item = helpEngine->indexModel()->linksForKeyword(v.toString()); textViewer->setSource(item.constBegin().value()); return; }
-
Thanks for this. It was really helpful.
I couldn't get it to work with your syntax for connecting the signal and slot but the code below worked for me. Not sure if something has changed over the years - I am v new to Qt.
connect(contentWidget, SIGNAL(clicked(QModelIndex)), this, SLOT(testSlot())); void HelpWindow::testSlot() { QModelIndex testIndex = mpHelpEngine->contentWidget()->currentIndex(); QHelpContentItem *item = mpHelpEngine->contentModel()->contentItemAt(testIndex); QUrl testUrl = item->url(); ui->textBrowser->setSource(testUrl); return; }
Of course the testSlot function needs to be added to the Slots in the .h file.
Hope this helps someone out there