PDA

View Full Version : Application crashes on click of a QLabel that is set to open URL in browser



rawfool
16th June 2013, 22:03
I have a pop-up widget that shows on entering a specific cell of QTableWidget. And I'm closing the pop-up window on leave event.
The pop-up widget has got some QLabels, which are set as URLs. When I click on a hyperlink(QLabel), the link is opened in my browser. And when I click on another url Qlabel, the link is opened, but my application crashes immediately.

One observation is if the mouse is taken out of the pop-up window, before it takes time to open the link in the browser, the application doesn't crash.


//cpopupwindow.h
// this closes the window when the mouse is out of widget
protected:
bool eventFilter(QObject * object, QEvent * event)
{
if(event->type() == QEvent::Leave)
{
if(isVisible())
{
while(!cveReferences.isEmpty())
{
delete cveReferences.takeFirst();
}
this->close();
}
}

return QWidget::eventFilter(object, event);
}




// in cpopupwindow.cpp
this->installEventFilter(this); //in ctor

// this is a public function which displays the content and is called in tablewidget class on cellEnter()
void CPopupWindow::someFunction()
{
// This is how I add the text to QLabels
if("CVE" == repoType)
{
cceReference_table->setVisible(false);
for(int i = 0; i < lst.count(); i++)
{
cveReferences.append(new QLabel());
cveReferences.at(i)->setText("<a href=\"" + references.value(lst.at(i)) + "\">" +
lst.at(i) + "</a>");
cveReferences.at(i)->setTextFormat(Qt::RichText);
cveReferences.at(i)->setTextInteractionFlags(Qt::TextBrowserInteraction );
cveReferences.at(i)->setOpenExternalLinks(true);
vLyt->addWidget(cveReferences.at(i));
}
}
}


Everything is fine until second click of the URL. On second click, if the mouse is not taken out of pop-up widget, then the whole application crashes.
Kindly help me with this. Thank you.

ChrisW67
17th June 2013, 00:44
Run your program in your debugger and cause it to crash. When it crashes look through the stack back trace to find the line in your code that is causing the crash. We cannot do this for you.

rawfool
17th June 2013, 06:15
I'm not emitting any signals on click of the QLabel, just setting the QLabel as hyperlink and setting flags to Qt::TextBrowserInteraction as shown in someFunction() in earlier post. Thing is, this crash happens only if the mouse is not oved out of this window, the browser shows on top of it, this point crash happens. But if I quickly remove my mouse out of widget, event closes the window.

So I'm unable to figure out where I put the break-point. As the hyperlinked labels are already set on the window & there is no problem there.
Thank you.

EDIT: I put the break-point on show of this pop-up window, it's crashing and stack message is


Level Function File Line
0 QApplicationPrivate::dispatchEnterLeave Qt5Widgets

Santosh Reddy
17th June 2013, 06:38
while(!cveReferences.isEmpty())
{
delete cveReferences.takeFirst();
}
Why are you deleting cveReferences?



for(int i = 0; i < lst.count(); i++)
{
cveReferences.append(new QLabel());
cveReferences.at(i)->setText("<a href=\"" + references.value(lst.at(i)) + "\">" +
lst.at(i) + "</a>");
cveReferences.at(i)->setTextFormat(Qt::RichText);
cveReferences.at(i)->setTextInteractionFlags(Qt::TextBrowserInteraction );
cveReferences.at(i)->setOpenExternalLinks(true);
vLyt->addWidget(cveReferences.at(i));
}
It is not garanteed that you are setting the QLabel you just created. It is better to set the QLabel settings and then append it to the list. Just think what if the list was not empty, and you keep addeing new QLabels, then you will end up setting the old QLabels

rawfool
17th June 2013, 07:15
This, pop-up window is basically shown on cellEntered() of a tablewidget. I'm deleting that cveReferences because there is one else if more condition in someFunction() where some other widget comes in place of Qlabels. I'm adding & deleting the Qlabels dynamically, based on a reply from QNetworkReply.
So the adding & deleting of these QLabels needs to be dynamic in order to hide these labels to give place for another widget on other event.

ChrisW67
17th June 2013, 12:08
So I'm unable to figure out where I put the break-point.
You don't need to set a breakpoint. Let the program crash and the debugger will stop your program and display a back trace. Follow the back trace down until you find the line of your code that triggered the crash. In all likelihood at that point in your code you are accessing a null or invalid pointer.