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
Contributor
fazlanzone7
Posts: 26
Registered: ‎11-10-2013
My Device: Z10
My Carrier: Dialog

A slot function to save database record

[ Edited ]

I have a textfield and a button in my UI, and when I click the button I am supposed to save the text in textfield to my sql database.

 

I have a function to save the record(in a separate class). It takes the text data in the text field as an argument.

 

I used the "clicked()" signal of the button and connected it to a custom slot "saveRecord()". since the arguments of the signal and slot should be matched, i am unable to pass the textfield data to the slot. The alternative I can think of is to have a global variable to save the text data, and then access it from the slot.

 

Is there any other way to implement this scenario?

Please use plain text.
Developer
greenmr
Posts: 875
Registered: ‎03-20-2013
My Device: Red LE Developer Z10
My Carrier: Fido

Re: A slot function to save database record

From your post I surmise that you are doing this from C++, not QML. If so I would suggest you inherit a new class from Button and add a custom signal to it that takes your parameter. Something like this:

 

#include <bb/cascades/Button>
#include <bb/cascades/TextField> class MyButton : public bb::cascades::Button { Q_OBJECT Q_DISABLE_COPY(MyButton) Q_PROPERTY(bb:cascades::TextField textField READ textField WRITE setTextField) bb::cascades::TextField* textField(); void setTextField(bb::cascades::TextField textField); bb::cascades::TextField _textField; private slots: void clickedHandler(); signals: void myClicked(const QString& textFieldText); } MyButton::MyButton() { bool success; Q_UNUSED(success); this->_textField = NULL; success = QObject::connect( this, SIGNAL(clicked()), SLOT(clickedHandler())); Q_ASSERT(success); } bb::cascades::TextField MyButton::textField() { return this->_textField; } void MyButton::setTextField(bb::cascades::TextField textField) { this->_textField = textField; } void MyButton::clickedHandler() { if (this->_textField) { emit myClicked(this->textField->text(); } }

Then you need to register the new class with QML. Just in case you aren't familiar with this, you can put this code in the constructor of the main UI class:

 

MyApp::MyApp() {
       qmlRegisterType<MyButton>( "com.yourcompany.custom", 1, 0, "MyButton" );
}

And finally in QML do something like this:

 

import com.yourcompany.custom 1.0

Page {
   Container {
      TextField {
         id: myTextField
      }
      MyButton {
         text: qsTr("Button Label")
         textField: myTextField
         onMyClicked: {
            var tst = textFieldText;
         }
      }
   }
}

Of course you don't need to use the custom button in QML, you can do it all in C++ instead and do the signal/slot connection manually.



Developer of Built for BlackBerry certified multiFEED RSS/Atom feed reader and aggregator.
Please use plain text.
Developer
BBSJdev
Posts: 6,089
Registered: ‎07-05-2012
My Device: Playbook, Z10 LE, Dev Alpha C
My Carrier: Orange

Re: A slot function to save database record

I think he is already doing that if I read the OP correctly. Although you've missed out whether the UI side is QML or C++.

 

There is no problem with just passing in/attaching your sql object or declaring a property then all you would need is...

 

Button {
    onClicked: {
        _db.saveRecord(textfield.text);  // function or signal
    }
}

 


If you've been helped click on Like Button, if you've been saved buy the app. :smileyhappy:

Developer of stokLocker, Sympatico and Super Sentences.
Please use plain text.
Developer
greenmr
Posts: 875
Registered: ‎03-20-2013
My Device: Red LE Developer Z10
My Carrier: Fido

Re: A slot function to save database record

@BBSJdev I interpreted this:

 

I used the "clicked()" signal of the button and connected it to a custom slot "saveRecord()". since the arguments of the signal and slot should be matched, i am unable to pass the textfield data to the slot.

 

...to imply that the OP was doing this in C++, and also to suggest that the intent was to pass the contents of the TextField to the Button clicked() handler as a parameter rather than have to retrieve the value from somewhere else in the body of the slot. Matching signal/slot arguments is not something devs generally consider when using QML slot syntax. Of course, you can certainly make the connection programatically with connect() even in QML code, but that's not the way I interpreted the OP was doing it. Sometimes it is very tricky figuring out what a poster is actually asking for.

 

If everything is in QML it would be easy to do as you suggest, but my imperfect reading of the question leads me to think it isn't. Hopefully the OP will elaborate.



Developer of Built for BlackBerry certified multiFEED RSS/Atom feed reader and aggregator.
Please use plain text.
Developer
BBSJdev
Posts: 6,089
Registered: ‎07-05-2012
My Device: Playbook, Z10 LE, Dev Alpha C
My Carrier: Orange

Re: A slot function to save database record

Yep, code would be useful.

 


If you've been helped click on Like Button, if you've been saved buy the app. :smileyhappy:

Developer of stokLocker, Sympatico and Super Sentences.
Please use plain text.