Getting dynamically created buttons to take me to the correct URL
-
Hey there,
I have an application that dynamically creates QToolButtons whenever a QWebView finishes loading. The text of the toolButtons is set to the URL that the QWebView loaded. By visiting multiple web sites, I will have lots of these buttons with different texts. I want to be able to click them and have them take me to the corresponding site, but they only take me to the previous site regardless of their texts. heres the code:
@void MainWindow::createSiteButtons()
{
QString title = webView->title();
QString url = webView->url().toString();
QIcon icon = webView->icon();appTaskButton = new QToolButton; appTaskButton->setText(url); appTaskButton->setIcon(icon); navBar->addWidget(appTaskButton); connect(appTaskButton, SIGNAL(clicked()), SLOT(goToApp()));
}
void MainWindow::goToApp()
{
//QToolButton *button = qobject_cast<QToolButton *>(sender());
webView->setUrl(QString(appTaskButton->text()));
}
@ -
Hi,
appTaskButton is the last button created thus each time you click any button connected it will retrieve the last created button text.
Using sender like the in the commented code is a good solution:
@
{
QToolButton *button = qobject_cast<QToolButton *>(sender());
if (button) {
webView->setUrl(button->text());
}
}
@ -
yeah the commented code of mine wasn't working, hence I commented it out. Ill try out your example but I have a question: what does the
@if(button)@ statement do? Thanks for your help! -
I have changed the code to this per your example:
@void MainWindow::createSiteButtons()
{
QString title = webView->title();
QString url = webView->url().toString();
QIcon icon = webView->icon();appTaskButton = new QToolButton; appTaskButton->setText(url); appTaskButton->setIcon(icon); navBar->addWidget(appTaskButton); connect(appTaskButton, SIGNAL(clicked()), SLOT(goToApp()));
}
void MainWindow::goToApp()
{
QToolButton *button = qobject_cast<QToolButton *>(sender());
if (button) {
webView->setUrl(QString(button->text()));
}//webView->setUrl(QString(button->text()));
}@
however the program still crashes when I click one of the buttons.
-
if(button) is to ensure that if your slot gets called for whatever reason from something else than a QToolButton, it doesn't crash.
What's the backtrace ?
-
Im not sure what backtrace is
-
When you run in debug mode and your application crashes, you should see the list of calls that lead to where the crash occurred
-
ok so when I click the button and it crashes here is the error given to me in the computers crash report:
Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0 QtWidgets 0x0000000100e52118 QAbstractButton::text() const + 8
1 @BUNDLEIDENTIFIER@ 0x0000000100007839 MainWindow::goToApp() + 89 (mainwindow.cpp:84)
2 @BUNDLEIDENTIFIER@ 0x0000000100007b0d MainWindow::qt_static_metacall(QObject*, QMetaObject::Call, int, void**) + 141 (moc_mainwindow.cpp:76)this is just the first little part, it goes on for quite a while, but these are the only lines relating to something that I made (goToApp()).
What do I do? thanks!
-
Looks strange, can you change your code to something like
@
if (button) {
QString text = button->text();
webView->setUrl(text);
}
@And put a breakpoint there to check that everything is ok
-
ok code changed to this:
@void MainWindow::goToApp()
{
QToolButton *button = qobject_cast<QToolButton *>(sender());
if (button) {
QString text = button->text();
webView->setUrl(text);
}
}@not sure how to put a breakpoint there (I'm terrible at debugging)
-
Right click on the line number and choose: Set breakpoint at Line X