PDA

View Full Version : Can't connect a signal to a slot



cejohnsonsr
26th August 2010, 15:28
I'm using Qt 4.6.3 SDK on the 64 bit version Fedora 13. I created a project via Creator & the form via Designer. It's a trivial program intended only to experiment with Signals & Slots to get me started with Qt. I've read everything I can find & I've done just what the docs say is right, but it doesn't work. I'm including the code from the header & cpp as well as the output from the build. Any help in understanding why this isn't working will be greatly appreciated.

Note: The app does run & the initial string is displayed.

Thanx,

Ed
This is the header:


#ifndef SANDSDEMO_H
#define SANDSDEMO_H

#include <QWidget>
#include <QObject>
#include <QLabel>

namespace Ui {
class SandSDemo;
}

class SandSDemo : public QWidget
{
Q_OBJECT

public:
explicit SandSDemo(QWidget *parent = 0);
~SandSDemo();

public slots:
void cancelText();

private:
Ui::SandSDemo *ui;
void initialText();
};

#endif // SANDSDEMO_H



This is the source:


#include "sandsdemo.h"
#include "ui_sandsdemo.h"

SandSDemo::SandSDemo(QWidget *parent) :
QWidget(parent),
ui(new Ui::SandSDemo)
{
ui->setupUi(this);
connect(ui->cancelButton,SIGNAL(clicked()),ui->msgArea,SLOT(cancelText()));
initialText();
}

SandSDemo::~SandSDemo()
{
delete ui;
}

void SandSDemo::initialText()
{
QString * initialMsg = new QString( "This is the string that will be displayed upon initialization.");

ui->msgArea->setText(*initialMsg);
delete initialMsg;
}

void SandSDemo::cancelText()
{
QString * cancelMsg = new QString("This is the string that will be displayed when the cancel button is clicked.");
ui->msgArea->setText(*cancelMsg);
delete cancelMsg;
}


This is the error from the build output:

Starting /home/ed/projects/SandSDemo-build-desktop/SandSDemo...
Object::connect: No such slot QLabel::cancelText() in ../SandSDemo/sandsdemo.cpp:9
Object::connect: (sender name: 'cancelButton')
Object::connect: (receiver name: 'msgArea')
/home/ed/projects/SandSDemo-build-desktop/SandSDemo exited with code 0

cejohnsonsr
26th August 2010, 15:32
Additional infor: I originally tried to connect via designer, but my slot isn't available in the signals & slots editor.

Thanx,

Ed

hobbyist
26th August 2010, 15:45
Try


connect(ui->cancelButton,SIGNAL(clicked()), this, SLOT(cancelText()));

Moreover, in your initialText() and cancelText() functions, you can set the text as


ui->msgArea->setText("blaa");

No need to introduce a new QString in heap for temporary use.

cejohnsonsr
26th August 2010, 16:23
Thank you so much. That did the trick. It's been several years since I tried to write anything in any language. This app is just a trivial attempt to refresh my memory in several areas as well as trying to become familiar with Qt. Thus the pointers & memory usage.

I am curious to know exactly which object "this" points to in this case. And I'm confused as to why Designer wouldn't let me make the connection.

Ed

norobro
26th August 2010, 19:46
Welcome Ed.

Regarding the this pointer (from here (http://en.wikipedia.org/wiki/This_%28computer_science%29)):
this is usually an immutable reference or pointer which refers to the current object. So in your case it is a pointer to the current instance of SandSDemo.

In Designer try connecting your slot like this:
Press F4 // puts you in signals/slots editing mode
Left click and hold on your pushbutton, drag to your main widget and release. // you should see something like an electrical ground symbol
Click on a signal in the left panel of the dialog that pops upClick edit under the right panel and add your slot.
Close the dialog and press F3 to return to widget edit mode.

Another way to make connections is to let the Meta-Object Compiler, or moc, automatically connect your custom slot. You have to follow the naming convention described here (http://doc.trolltech.com/4.6/qmetaobject.html#connectSlotsByName). If you name your slot on_cancelButton_clicked() you won't need the connect statement.

HTH

cejohnsonsr
26th August 2010, 20:42
Thank you very much. This is just the kind of answer I hoped for. I'll read naming convention info you provided & try the Designer Slot Editor again. I hadn't tried connecting to the Main Widget so the edit buttons were grayed out. This has given me plenty to play with. I'm sure I'll like one way be able to get comfortable making the connections now.

If you're watching this forum, maybe you'll be able to provide some insight into a couple of other issues I'll be posting shortly. As I mentioned before, this is all trivial. I'm just getting my feet wet again & playing around with code is the best way to improve my skills.

Ed