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
Developer
Posts: 671
Registered: ‎03-21-2012
My Device: BlackBerry PlayBook 16GB
My Carrier: Vip
Accepted Solution

How to connect slot to cascades Button

I'm new to Cascades and QML, and yesterday I tried to port native QtWidget app to Cascades, but it doesn't work. The problem is, I add few buttons to container in main QML file and coded my custom slot function that should be executed when user click some button. But I don't know how to connect clicked() signal to my custom slot function.

 

When I use  

connect(closeButton,SIGNAL(Button::clicked()),this,SLOT(on_closeButton_clicked())); or connect(exitButton,SIGNAL(clicked()),this,SLOT(on_exitButton_clicked())); I get "Can't connect SIGNAL(null) to SLOT(on_closeButton_clicked())" errors on all buttons.

 

Here is my code in app.cpp

 

App::App()
{
    QmlDocument *qml = QmlDocument::create("main.qml");
    qml->setContextProperty("cs", this);

    AbstractPane *root = qml->createRootNode<AbstractPane>();
    Application::setScene(root);
    textHolder = root->findChild<TextArea*>("textHolder");
    openButton = root->findChild<Button*>("openButton");
    saveButton = root->findChild<Button*>("saveButton");
    saveAsButton = root->findChild<Button*>("saveAsButton");
    closeButton = root->findChild<Button*>("closeButton");
    exitButton = root->findChild<Button*>("exitButton");
    
    connect(openButton,SIGNAL(Button::clicked()),this,SLOT(on_openButton_clicked()));
    connect(saveButton,SIGNAL(Button::clicked()),this,SLOT(on_saveButton_clicked()));
    connect(saveAsButton,SIGNAL(Button::clicked()),this,SLOT(on_saveAsButton_clicked()));
    connect(closeButton,SIGNAL(Button::clicked()),this,SLOT(on_closeButton_clicked()));
    connect(exitButton,SIGNAL(clicked()),this,SLOT(on_exitButton_clicked()));
}

//one slot function

void App::on_closeButton_clicked()
{
//ui->textEdit->clear();
	textHolder->resetText();
//if(file.open(QIODevice::ReadOnly | QIODevice::Text)){
    file.close();
    fileName.clear();
//}
}

 

And qml

import bb.cascades 1.0


Page {
    id: mainPage
    content: 
    Container {
    id: main
    Container {
    id: buttonHolder
    scrollMode: ScrollMode.None
            layout: StackLayout {
                layoutDirection: LayoutDirection.LeftToRight
                leftPadding: 5.0
                rightPadding: 5.0
                topPadding: 5.0
                bottomPadding: 5.0
            }
            touchPropagationMode: TouchPropagationMode.None
            objectName: "buttonHolder"
            Button {
                id: openButton
                text: "Open"
            }
            Button {
                id: closeButton
                text: "Close"
                onClicked: {
                }
            }
            Button {
                id: saveButton
                text: "Save"
                onClicked: {
                }
            }
            Button {
                id: saveAsButton
                text: "Save As"
                onClicked: {
                }
            }
            Button {
                id: exitButton
                text: "Exit"
            }
        }
    
    
    
    //********************************
        Container {
            id: textHolderArea
            layout: AbsoluteLayout {
            }
            topMargin: 5.0
            leftMargin: 1.0
            rightMargin: 1.0
            bottomMargin: 2.0
            TextArea {
                id: textHolder
                textStyle.fontStyleHint: FontStyleHint.Default
                layoutProperties: AbsoluteLayoutProperties {
                }
                leftMargin: 5.0
                rightMargin: 5.0
                preferredHeight: 645.0
                textStyle.justification: TextJustification.Enabled
                objectName: "textHolder"
                text: ""
            }
        }
    //********************************
    }
}

 

 

And I have no idea how to call my C++ slot from app.cpp in my main QML file

 

 

Retired
Posts: 2,559
Registered: ‎10-16-2009
My Device: BlackBerry Z10
My Carrier: Bell

Re: How to connect slot to cascades Button

Your buttons will need an objectName defined to be visible to C++, the id is only visible from within the QML.

Garett
@garettBeuk
--
Goodbye everybody!