PDA

View Full Version : Bouncing button problems



WinchellChung
25th June 2007, 20:50
I'm sure this is some kind of rank newbie mistake, but I cannot seem to discover what I am doing wrong.

I've got several buttons, each with their "clicked" signal hooked to a slot to do various functions. However, it seems that when the app is running, when the user clicks on one of the buttons, two clicked signals are received by the app.

For instance, one of the buttons uses QDesktopServices::openUrl to invoke the system's web browser and send it to a certain web page. When the button is clicked once, Firefox appears with two tabs, both set to the same web page.

The same thing happens when the keyboard shortcut for the button is typed.

Any ideas of what I am doing wrong?

marcel
25th June 2007, 21:18
Can you post the code where you connect the button signals and where you create the actions. Or at least describe the problem a bit?

The most common reason is that you connected somehow the clicked signal twice to the same slot.
So, when it is emitted, the slot gets called twice.

Regards

WinchellChung
25th June 2007, 22:59
Qt 4.2.3, compiled statically
MS Visual Studio 2005

In myMainWindow.h


class myMainWindow : public QMainWindow
{
Q_OBJECT

public:
myMainWindow(QWidget *parent = 0, Qt::WFlags flags = 0);
~myMainWindow();
...

private slots:
void on_aboutUs_clicked(bool checked);
...
};



In myMainWindow.cpp


myMainWindow::myMainWindow(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
ui.setupUi(this);
connect(ui.aboutUs, SIGNAL(clicked(bool)), this, SLOT(on_aboutUs_clicked(bool)));
...
}

myMainWindow::on_aboutUs_clicked(bool checked)
{
QDesktopServices::openUrl(QUrl(tr("http://www.whatever.com/about.html")));
}



In ui_myMainWindow.h


class Ui_myMainWindowClass
{
public:
QPushButton *aboutUs;
...
aboutUs= new QPushButton(page_2);
aboutUs->setObjectName(QString::fromUtf8("aboutUs"));
aboutUs->setGeometry(QRect(11, 0, 195, 27));
aboutZeiss->setIcon(QIcon(QString::fromUtf8(":/myMainWindow/Resources/AboutUs.png")));

As you can see there is not much to it. When the button is clicked, two instances of the URL are opened. I've set breakpoints in the slot and yes indeed it gets called twice. This happens with all ten buttons in the app.

As a work-around, I'm experimenting with using the "released" signal instead of the "clicked" signal.

jpn
25th June 2007, 23:37
"on_aboutUs_clicked" follows the naming convention of automatic connections (http://doc.trolltech.com/4.3/designer-using-a-component.html#automatic-connections). Therefore the connection is established twice; 1) automatically 2) by hand.

WinchellChung
26th June 2007, 00:02
Automatic connections! That explains it.
I had not gotten that far in the book so I had never heard of them. I used that naming convention because the book used it.

Thanks!