Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Super Contributor
Posts: 440
Registered: ‎01-13-2011
My Device: Playbook
My Carrier: NA
Accepted Solution

Signals and Slots from a class.

Main:
h:
public slots:
	void handleEmit();


cpp:
MyComponent *myComponent = new MyComponent();
	bool res = QObject::connect(myComponent, SIGNAL(myFirstSignal()), this, SLOT(handleEmit()));
	Q_ASSERT(res);
	Q_UNUSED(res);

void Main::handleEmit()
{
	//do something
}


MyComponent:
h:
class MyComponent
{
	signals:
	    void myFirstSignal();

};


cpp:
MyComponent::MyComponent()
{
	//EMIT THE SIGNAL
	emit myFirstSignal();
}

 

I'm trying to emit a signal from a class (myComponent) to my Main app but am not having much luck, I've followed the tutorial on signals and slots but it still doesnt work. Am I missing something?

 

Thanks

 

--------------------------------------------------------------------------------------------------
BlackBerry Apps: Instruments | ARTPAD | Piano | Drums | Xylophone
Contributor
Posts: 34
Registered: ‎10-08-2012
My Device: developer
My Carrier: ??

Re: Signals and Slots from a class.

i don't know if this is the way signals should be called but for me it works fine:

 

in the .h file

signals:
     void myFirstSignal();

 and at the point you want to send the signal:

this->myFirstSignal();
Super Contributor
Posts: 440
Registered: ‎01-13-2011
My Device: Playbook
My Carrier: NA

Re: Signals and Slots from a class.

The correct way is:

 

emit myFirstSignal();

 

This line for me is causing the problems:

 

bool res = QObject::connect(myComponent, SIGNAL(myFirstSignal()), this, SLOT(handleEmit()));
--------------------------------------------------------------------------------------------------
BlackBerry Apps: Instruments | ARTPAD | Piano | Drums | Xylophone
Contributor
Posts: 34
Registered: ‎10-08-2012
My Device: developer
My Carrier: ??

Re: Signals and Slots from a class.

i changed it in my code and it still works fine.

and i changed my connect method to match yours but it still works fine.

 

do you get an error or anything?

Super Contributor
Posts: 440
Registered: ‎01-13-2011
My Device: Playbook
My Carrier: NA

Re: Signals and Slots from a class.

no matching function for call to 'QObject::connect(MyComponent&, const char*, MyApp* const, const char*)'

 

Are you calling your signal from a  seperate class like me?

--------------------------------------------------------------------------------------------------
BlackBerry Apps: Instruments | ARTPAD | Piano | Drums | Xylophone
Contributor
Posts: 34
Registered: ‎10-08-2012
My Device: developer
My Carrier: ??

Re: Signals and Slots from a class.

yes i do.

the only difference I can see at the moment is that your classes don't extend another class and mine do

and connect expects a QObject for sender and receiver

and I don't have enough knollege about Qt to know for sure if a class automaticly extends QObject.

Super Contributor
Posts: 440
Registered: ‎01-13-2011
My Device: Playbook
My Carrier: NA

Re: Signals and Slots from a class.

Could you do me a favour and post your class so I can take a look at it?

 

Thanks!

--------------------------------------------------------------------------------------------------
BlackBerry Apps: Instruments | ARTPAD | Piano | Drums | Xylophone
Developer
Posts: 71
Registered: ‎09-19-2012
My Device: Z10 very soon
My Carrier: Play

Re: Signals and Slots from a class.

Hi.

 

Did you read this already ?

 

http://qt-project.org/doc/qt-4.8/signalsandslots.html

 

 

Contributor
Posts: 34
Registered: ‎10-08-2012
My Device: developer
My Carrier: ??

Re: Signals and Slots from a class.

[ Edited ]

I tried to filter my code for the usefull parts

MainWidget.h:

class MainWidget : public QWidget
{
    Q_OBJECT

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

public slots:
    void handleButton2();
    void setValues(QString s);

private:
    Ui::MainWidget *ui;
};

MainWidget.cpp:

MainWidget::MainWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MainWidget)
{
    //deze methoden creëerd een frame aan de hand van een xml file
    ui->setupUi(this);

    //een actie aan een knop hangen
    connect(ui->pushButton_2, SIGNAL(pressed()), this, SLOT(handleButton2()));
}

void MainWidget::handleButton2(){
    QVector<QString> *waarden = new QVector<QString>();
    waarden->insert(waarden->size(), QString("man"));
    waarden->insert(waarden->size(), QString("vrouw"));
    radioButtonPopup *hoi = new radioButtonPopup();
    hoi->addData(ui->label_3->text(), waarden);
    hoi->exec();
    bool res = QObject::connect(hoi, SIGNAL(done(QString)), this, SLOT(setValues(QString)));
}

void MainWidget::setValues(QString s){
    ui->label_6->setText(s);
}

radioButtonPopup.h:

class radioButtonPopup : public QDialog
{
    Q_OBJECT
public:
    explicit radioButtonPopup(QWidget *parent = 0);
    void addData(QString question, QVector<QString> *possible);
    QString convertInt(int i);

signals:
     void done(QString choise);

public slots:
     void choisesMade();
};

radioButtonPopup.cpp:

radioButtonPopup::radioButtonPopup(QWidget *parent) :
    QDialog(parent)
{
    //layout voor de dialog aanmaken
    layoutGrid = new QVBoxLayout;
    setLayout(layoutGrid);
}

void radioButtonPopup::addData(QString question, QVector<QString> *possible){
    //label om de vraag in te zetten
    QLabel *vraag = new QLabel(question);
    layoutGrid->addWidget(vraag);

    //is nodig om te zorgen dat de lijst weer leeg. anders krijg je bij opnieuw invullen ook de vorige data erbij
    buttons->clear();

    //checkbox vector vullen met de radiobuttons door middel van een loop
    for(int i = 0 ; i < possible->size() ; i++){
        QRadioButton *c = new QRadioButton(possible->at(i));
        buttons->insert(buttons->size(), c);
        layoutGrid->addWidget(c);
    }

    //een knopje op een layout zetten zodat deze tevoorschijn komt
    QPushButton *p = new QPushButton("ok");
    layoutGrid->addWidget(p);
    connect(p, SIGNAL(pressed()), this, SLOT(choisesMade()));
}

void radioButtonPopup::choisesMade(){
    QString s = "";
    for(int i = 0 ; i < buttons->size() ; i++){
        if(buttons->value(i)->isChecked()){
            s = buttons->value(i)->text();
            break;
        }
    }
    emit done(s);
    this->setVisible(false);
    this->close();
}

 

Super Contributor
Posts: 440
Registered: ‎01-13-2011
My Device: Playbook
My Carrier: NA

Re: Signals and Slots from a class.

Thanks, could you let me knwo any includes you have?

--------------------------------------------------------------------------------------------------
BlackBerry Apps: Instruments | ARTPAD | Piano | Drums | Xylophone