PDA

View Full Version : connect() returning FALSE



virgo_terminus
27th July 2020, 09:44
I'm in Linux 32bit, using Qt4.8.6 and gcc 4.9.1.

I wrote a program having a main window with some buttons which in turn trigger some other small dialogs.
One of these dialogs (CpmUserDialog) has 2 buttons (OK and Cancel) and a spinner for numeric input.
I connected the Cancel button to the standard QDialog::reject() slot.
I connected the OK button to a custom slot I wrote (okUser()).

The problem is the OK button does nothing when clicked, and by digging I found out it's because connect() returns FALSE on this button. "Digging" means a snippet like this: (in ui_CpmUserDialog.h)


const bool connected1 = QObject::connect(pushButton1, SIGNAL(clicked()), CpmUserDialog, SLOT(okUser()));
const bool connected2 = QObject::connect(pushButton2, SIGNAL(clicked()), CpmUserDialog, SLOT(reject()));
printf("OK connected?: %s\n", (connected1==0?"NO":"YES"));
printf("Cancel connected?: %s\n", (connected2==0?"NO":"YES"));

The Cancel button works just fine, connect() returns TRUE for this button.

When this dialog is shown, in the console I get this:


OK connected?: NO
Cancel connected?: YES

There are NO compilation errors and NO runtime errors in the console output.

And I don't know how to figure out the exact cause of the connect() failure.

This dialog has three pieces of code: CpmUserDialog.h, CpmUserDialog.cpp and CpmUserDialog.ui

My approach for custom slots was to define them in a subclass and manually insert their names in the .ui file in place of a dummy slot (like for instance accept()) generated using Qt Designer.

Here are the snippets:

CpmUserDialog.h (full code):
=====================


#ifndef CPMUSERDIALOG_H
#define CPMUSERDIALOG_H

#include <QDialog>

#include "ui_CpmUserDialog.h"

namespace CPM {
extern unsigned char user_new;
}

class CpmUserDialog : public QDialog, public Ui::CpmUserDialog {
Q_OBJECT
private slots:
void okUser();
public:
CpmUserDialog(QWidget *parent = 0);
};

#endif

CpmUserDialog.cpp (full code):
=======================


#include <QtGui>

#include "CpmUserDialog.h"

CpmUserDialog::CpmUserDialog(QWidget *parent) : QDialog(parent, Qt::CustomizeWindowHint | Qt::WindowTitleHint) {
setupUi(this);
}

void CpmUserDialog::okUser() {
CPM::user_new = spinBox1->value();
accept();
}


CpmUserDialog.ui (only the <connections> section):
======================================


<connections>
<connection>
<sender>pushButton1</sender>
<signal>clicked()</signal>
<receiver>CpmUserDialog</receiver>
<slot>okUser()</slot>
<hints>
<hint type="sourcelabel">
<x>20</x>
<y>20</y>
</hint>
<hint type="destinationlabel">
<x>20</x>
<y>20</y>
</hint>
</hints>
</connection>
<connection>
<sender>pushButton2</sender>
<signal>clicked()</signal>
<receiver>CpmUserDialog</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>20</x>
<y>20</y>
</hint>
<hint type="destinationlabel">
<x>20</x>
<y>20</y>
</hint>
</hints>
</connection>
</connections>


The weird thing is that other dialogs don't have this problem. For instance, check out the ZIP attachment with the sources for another dialog (RenameDialog). This one works just fine. In it I used the same approach for implementing the custom slots.