Qt World Summit: Register Today!

Custom event

  • Could someone give me a quick example of how I would make a button, when clicked run a method which does a math calculation and opens a messagebox to display the result. I checked slots and signals which show how to link one event to another but it doesn't explain what I want at least not in a way I comprehend..

  • There are a number of examples of how to do signals and slots and create buttons, and that sort of thing.

    In your case:

    • You should create a class that publicly inherits from QObject.
    • You need to define a slot to do the calculation. You could call it doCalc() or something.
    • Once you have a QPushButton instantiated somewhere, you connect the button's clicked() signal to your doCalc() slot. In your slot, you do the calculation, and display the result using "QMessageBox::information()":http://qt-project.org/doc/qt-4.8/qmessagebox.html#information or something similar.

    #include <QPushButton>
    #include <QMessageBox>
    #include <QString>

    class MyClass : public QWidget

    MyClass(QWidget *parent) : QWidget(parent) {
    m_pushbutton = new QPushButton("Number Crunch",this);
    connect(m_pushbutton, SIGNAL(clicked(), SLOT(doCalc()));

    public slots:
    void doCalc() {
    int a = 34 * 26 + 5;
    QString resultstr = QString("The result is: %1").arg(a);
    QMessageBox::information(this, "Calculation Results", resultstr);

    QPushButton *m_pushbutton;

    #include <QApplication>
    #include "myclass.h"

    int main(int argc, char **argv)
    QApplication app(argc,argv);
    MyClass *thing = new MyClass();
    return app.exec();
    Brain to terminal. YMMV. (Caveat: It's better to use a .cpp file for the implementation than to shove it all in the .h)

  • Hi thanks for the reply. I have typed everything exactly as you posted but I get a bunch of compile errors.

  • That is not a complete code sample. (See disclaimers after my sample code.)

    What kind of compile errors are you getting? My crystal ball is in the shop for repairs today.

  • Offhand I can spot one error in my code (which I didn't compile or test, I just typed it in, so typos are likely...)

    Line 12 should read:
    connect(m_pushbutton, SIGNAL(clicked()), SLOT(doCalc()));

  • [quote author="mlong" date="1331597147"]What kind of compile errors are you getting?[/quote]

    d:\my documents\qt projects\test\myclass.h(12) : warning C4002: too many actual parameters for macro 'SIGNAL'
    d:\my documents\qt projects\test\myclass.h(13) : error C2958: the left parenthesis '(' found at 'd:\my documents\qt projects\test\myclass.h(12)' was not matched correctly
    d:\my documents\qt projects\test\myclass.h(12) : error C2143: syntax error : missing ')' before ';'
    d:\my documents\qt projects\test\myclass.h(12) : error C2661: 'QObject::connect' : no overloaded function takes 2 arguments
    d:\my documents\qt projects\test\myclass.h(15) : error C2059: syntax error : 'public'
    d:\my documents\qt projects\test\myclass.h(19) : error C2355: 'this' : can only be referenced inside non-static member functions
    ..\Test\main.cpp(7) : error C2512: 'MyClass' : no appropriate default constructor available

  • I'd start by making the correction I mentioned above. See how those messages kind of correspond to the scenario of a missing ")" ?

    And change line 10 in myclass.h to:
    MyClass(QWidget *parent = 0) : QWidget(parent) {

  • Hello mate, I still get the same errors, minus the one on the last line which you fixed in your last post.

  • Well, you're going to have to do some debugging. My code was just a guideline to the concepts that you would need to use in your program. The mechanics of it should be inconsequential.

    You had asked for an example of how to lay out the structure of your program to do something. My code is to illustrate how a signal and a slot work together to do what you had asked about. Dig in a little bit, and write some code of your own, and I'm sure you'll figure it out!

    Good luck! We're here to help, but I don't have the time or energy to debug the specifics right now.

  • The thing is, nothing is really explained in your example, so it's like trying to learn Greek without any English to reference. I'm a C# coder and I'm fairly good at it and can figure things out myself generally, but this is almost completely out of my comfort zone and I'm struggling somewhat.

    Edit: Well you have explained a little, but most of the code is unexplained and I can't really make heads or tails of it.

  • A great easy introduction is the "Getting Started with Qt":http://qt-project.org/doc/qt-4.8/gettingstartedqt.html example here. It's a good step-by-step introduction with a lot of good references to other information (and where to get help) as it goes along.

    Qt's got a lot of good examples and is extremely well documented. I know there's a learning curve of sorts, but the payoff on the other side is great.

    There's a basic understanding of C++ stuff which will be required -- after all, it is a C++ tookit :)

  • I got this to work:


    However one thing I'm a little confused about. Instead of creating a button programmatically, I want to create it with the GUI editor, so I did just that and replace all references of m_button with the object name of my created button, and then removed the creation of the old button m_button = new QPushButton, but when I run the app it crashes.

  • Still looking for an answer on my last question.

  • Did you setup the ui? Have a look a the Designer docs on "how to use .ui files":/doc/qt-4.8/designer-using-a-ui-file.htm in your app.

Log in to reply