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: 105
Registered: ‎04-24-2012
My Device: blackberry developer
My Carrier: relience
Accepted Solution

How to create own slot method in QT for blackberry Playbook

Hello all,

 

After two days of work around I am successfully able to develope project of QT in Qnx IDE for blackberry playbook 2.0.

But Now I am  having problem to create slot method to call on the specific button click event,

 

Here is my code.

 

#include <qapplication.h>

#include <qpushbutton.h>
#include <qmessagebox.h>
#include <qmainwindow.h>
#include <qboxlayout.h>

 

slots
void clickedSlots();

int main(int argc, char *argv[]) {

    qputenv("QT_QPA_FONTDIR", "/usr/fonts/font_repository/monotype");
    QCoreApplication::addLibraryPath("app/native/lib");
    QApplication app(argc, argv);

    QWidget window;
    window.resize(1024, 600);
    window.setWindowTitle(QString::fromUtf8("QPushbutton Background"));
click = new QPushButton("Click Me");
    click->setToolTip("Thanks");
    click->setMaximumHeight(100);
    click->setGeometry(400, 100, 100, 40);
    QObject::connect(click, SIGNAL(clicked()), &app, SLOT(clickedSlots()));

    QPalette* palette1 = new QPalette();
    palette1->setColor(QPalette::Button, Qt::blue);
    click->setPalette(*palette1);

    QPushButton *quit = new QPushButton("Quit");
    QObject::connect(quit, SIGNAL(clicked()), &app, SLOT(quit()));

    QPalette* palette2 = new QPalette();
    palette2->setColor(QPalette::Background, Qt::green);
    quit->setPalette(*palette2);
    quit->setGeometry(200, 100, 100, 40);
    QVBoxLayout* layout = new QVBoxLayout(&window);
    layout->setGeometry(QRect(0, 0, 1024, 600));
    layout->addWidget(click);
    layout->addWidget(quit);

window.show();
    return app.exec();
}

void clickedSlots() {
    QMessageBox msgBox;
    msgBox.setWindowTitle("Hello");
    msgBox.setText("You Clicked ");
    msgBox.show();
    msgBox.exec();
}

 

Please help me with this problem. I had started developing in QT before 2 days only. I am not familier with QT development.

 

Thanks In advance.

bskania.

Retired
Posts: 499
Registered: ‎05-07-2012
My Device: developer
My Carrier: developer

Re: How to create own slot method in QT for blackberry Playbook

You don't show your include file: your slots need to be declared in your include file (not in the .cpp file), and the class also needs a Q_OBJECT.   Any time you have a connect, use the debugger to step over the connect and check the console for Qt messages telling you if the connect succeeds, and if not why.

 

The Qt documentation online is excellent.  You should be able to find some basic examples you can walk through.

 

Stuart

Developer
Posts: 105
Registered: ‎04-24-2012
My Device: blackberry developer
My Carrier: relience

Re: How to create own slot method in QT for blackberry Playbook

As you suggested I converted my code and set the slots in the header file.

 

but its giving me compile time error.

 

this is my header file named "myslots.h"

 

#include <qobject.h>
#include <qmessagebox.h>
#include <qpushbutton.h>
class myslots: public QObject {

Q_OBJECT

public:
    myslots() {
    }
    ;
    ~ myslots() {
    }
    ;

public slots:
    void clickedSlot() {
        QMessageBox* msgBox = new QMessageBox();
        msgBox->setWindowTitle("Hello");
        msgBox->setText("You Clicked " + ((QPushButton*) sender())->text());
        msgBox->exec();

    }
    ;

};

as soon as I tried to add Q_OBJECT its giving me error of


src\main.o: In function `myslots':
D:\ndk-2.0.0-workspace\QTHelloWorld\Simulator-Debug/..\src\/myslots.h:9: undefined reference to `vtable for myslots'
src\main.o: In function `~myslots':
D:\ndk-2.0.0-workspace\QTHelloWorld\Simulator-Debug/..\src\/myslots.h:12: undefined reference to `vtable for myslots'
cc: D:/bbNDK2.0/host/win32/x86/usr/bin/ntox86-ld caught signal 1

 

and if I remove Q_OBJECT. I  am able to build the project and deploy it on playbook simulator.

 

But at the time of debug I am getting error message in console ike

 

Object::connect: No such slot QApplication::_slots.clickedSlot() in ..\src\main.cpp:98
Object::connect:  (receiver name: 'com.example.QTHelloWorld.testDev_THelloWorldbdb718b4')

 

here _slots is object of the myslots class.

 

Waiting for your response.

Thanks in advance.

bskania

 

 

Developer
Posts: 133
Registered: ‎03-28-2011
My Device: BlackBerry 9900 & PlayBook
My Carrier: Bell

Re: How to create own slot method in QT for blackberry Playbook

You're lucky I'm feeling extra generous today.

 

I fixed your code. You should really read the Qt documentation, it's fantastic. Here's a good start for Signals and Slots.

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

 

Mark this post as the solution, and give me a like please.

 

main.cpp

#include <qapplication.h>

#include <qpushbutton.h>
#include <qmessagebox.h>
#include <qmainwindow.h>
#include <qboxlayout.h>
#include "myslots.h"

int main(int argc, char *argv[]) {
    qputenv("QT_QPA_FONTDIR", "/usr/fonts/font_repository/monotype");
    QCoreApplication::addLibraryPath("app/native/lib");
    QApplication app(argc, argv);

    QWidget window;
    window.resize(1024, 600);
    window.setWindowTitle(QString::fromUtf8("QPushbutton Background"));
    QPushButton *click = new QPushButton("Click Me");
    click->setToolTip("Thanks");
    click->setMaximumHeight(100);
    click->setGeometry(400, 100, 100, 40);
    myslots a;
    QObject::connect(click, SIGNAL(clicked()), &a, SLOT(clickedSlot()));

    QPalette* palette1 = new QPalette();
    palette1->setColor(QPalette::Button, Qt::blue);
    click->setPalette(*palette1);

    QPushButton *quit = new QPushButton("Quit");
    QObject::connect(quit, SIGNAL(clicked()), &app, SLOT(quit()));

    QPalette* palette2 = new QPalette();
    palette2->setColor(QPalette::Background, Qt::green);
    quit->setPalette(*palette2);
    quit->setGeometry(200, 100, 100, 40);
    QVBoxLayout* layout = new QVBoxLayout(&window);
    layout->setGeometry(QRect(0, 0, 1024, 600));
    layout->addWidget(click);
    layout->addWidget(quit);
    window.show();
    return app.exec();
}

 

myslots.h

#include <qobject.h>
#include <qmessagebox.h>
#include <qpushbutton.h>

class myslots : public QObject {
    Q_OBJECT

    public:
        myslots() { };

    public slots:
        void clickedSlot() {
            QMessageBox* msgBox = new QMessageBox();
            msgBox->setWindowTitle("Hello");
            msgBox->setText("You Clicked " + ((QPushButton*) sender())->text());
            msgBox->exec();
        };
};

 

myslots.pro

TARGET = myslots
DEPENDPATH += .
INCLUDEPATH += .
HEADERS += myslots.h
SOURCES += main.cpp

 

Put all of those files in the same folder, run qmake in there, it will produce a Makefile. Run make, and it will output an executable binary called myslots. Run that, and your code works.

 

I didn't make many changes, I'll let you walk through the code yourself to figure out what I did. Look at the documentation to help. 

 

I have not tested this in Momentics, nor the PlayBook/Dev Alpha. But I'm 100% certain the code works, since I ran it myself.

 


Founder of Pulsecode Inc. and taab
Authomator - Two-factor authentication codes on BlackBerry 10 - http://www.xitijpatel.com/ - Follow @xitijpatel
Is there a helpful or useful post in this thread? Click the thumbs up on it so that other people can find it more easily!
Developer
Posts: 105
Registered: ‎04-24-2012
My Device: blackberry developer
My Carrier: relience

Re: How to create own slot method in QT for blackberry Playbook

[ Edited ]

Hello,

 

I already review that link. and done same as you had given in the file.

But one thing that I am working on the QNX Momentics IDE with playbook sdk 2.0

 

So as soon as I am trying to add the line Q_OBJECT , Its giving me error.

 

In that link did you read this.

 

Building the Example

The C++ preprocessor changes or removes the signals, slots, and emit keywords so that the compiler is presented with standard C++.

By running the moc on class definitions that contain signals or slots, a C++ source file is produced which should be compiled and linked with the other object files for the application. If you use qmake, the makefile rules to automatically invoke moc will be added to your project's makefile.

 

Thanks for your help. but problem is still there.

bskania.

Developer
Posts: 133
Registered: ‎03-28-2011
My Device: BlackBerry 9900 & PlayBook
My Carrier: Bell

Re: How to create own slot method in QT for blackberry Playbook

Well then you need to review the .pro file and the Makefile that Momentics is generating and see what's missing. In the project build options, are the Qt libraries properly included? 

 

I'll be honest, I've never used Momentics to do Qt development. You're better off using Qt Creator, or the command-line. 

 

However, I've fixed your code so that it should work. I've removed one potential problem from your issues. Now it's up to you to resolve the issues with your Momentics IDE.

 

I think marking the post as solved is fair. It's difficult to help you solve issues with your computer through a forum.


Founder of Pulsecode Inc. and taab
Authomator - Two-factor authentication codes on BlackBerry 10 - http://www.xitijpatel.com/ - Follow @xitijpatel
Is there a helpful or useful post in this thread? Click the thumbs up on it so that other people can find it more easily!
Developer
Posts: 105
Registered: ‎04-24-2012
My Device: blackberry developer
My Carrier: relience

Re: How to create own slot method in QT for blackberry Playbook

Thanks for your help.
I tried to work on QT creator.
But as soon as I tried to build my project and tried to deploy it on the Playbook simulator ,It is giving me enable to run due to error 8.
I tried to resolve that error but Not succeeded. So I move to QNX momentics. I am new to both.

can you guide me to remove that error of QT creator. I am using windows 7. Installed blackberry native sdk 2.0 and also QT creator 2.4.48 with QT SDK 4.8

It will be great if you can help me.

Thanks in advance.
Developer
Posts: 133
Registered: ‎03-28-2011
My Device: BlackBerry 9900 & PlayBook
My Carrier: Bell

Re: How to create own slot method in QT for blackberry Playbook

If I remember correctly, error 8 means that it's trying to run code that isn't compiled for that processor architecture.

 

Basically, if you're running the code in the simulator, you need to make sure it's compiled for the simulator, so it uses the x86 compiler. If you're running it on device, it needs to be compiled using ARM compiler.

 

Qt Creator can do it, but I don't think it's as straightforward. 

 

http://supportforums.blackberry.com/t5/Native-Development/How-to-port-your-Qt-Projects-to-Playbook/t...

 

Check that thread out for instructions on how to get your build environment set up. It should answer a lot of your questions.


Founder of Pulsecode Inc. and taab
Authomator - Two-factor authentication codes on BlackBerry 10 - http://www.xitijpatel.com/ - Follow @xitijpatel
Is there a helpful or useful post in this thread? Click the thumbs up on it so that other people can find it more easily!
Retired
Posts: 499
Registered: ‎05-07-2012
My Device: developer
My Carrier: developer

Re: How to create own slot method in QT for blackberry Playbook

I've marked the earlier post as a solution because it clearly is.  -- thank you HorizonXP for your clarity here.

 

As for build errors:

In Qt Creator, sometimes incremental builds after adding a Q_OBJECT does not properly moc.   You have to do a clean, and sometimes have to explicilty run qmake.

In Momentics you might need to do the same thing.

 

You need to understand signals and slots, and the moc step.  A slot is just a method of your class that is flagged as something you can connect.  Once everything builds and is hooked up right, the connect will not complain.  Between the post I marked as solution and Qt documentation you should be able to set up some trivial hello world applications and get them running, and from there you should be home free.  Make sure you understand why the solution is correct; if you're still stuck, simplify and do a Qt-only hello world application.

 

As for deploying, again, simplify.   If you are having trouble with the process, go back to the samples and make sure you can build and deliver those and understand the process.  Then see if you can apply that to your own example.

 

Good luck!

 

Stuart

Highlighted
Retired
Posts: 499
Registered: ‎05-07-2012
My Device: developer
My Carrier: developer

Re: How to create own slot method in QT for blackberry Playbook

One final addendum:

 

When you see: "undefined vtbl"  read this as "moc step isn't done".   If there's a makefile, the moc might be missing. If it's Qt Creator, incremental build might not have done the moc step.

 

Stuart