Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Custom event
Forum Updated to NodeBB v4.3 + New Features

Custom event

Scheduled Pinned Locked Moved General and Desktop
14 Posts 3 Posters 7.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mlong
    wrote on last edited by
    #2

    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.

    myclass.h:
    @
    #include <QPushButton>
    #include <QMessageBox>
    #include <QString>

    class MyClass : public QWidget
    {
    Q_OBJECT

    public:
    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);
    }

    private:
    QPushButton *m_pushbutton;
    };
    @

    main.cpp:
    @
    #include <QApplication>
    #include "myclass.h"

    int main(int argc, char **argv)
    {
    QApplication app(argc,argv);
    MyClass *thing = new MyClass();
    thing->show();
    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)

    Software Engineer
    My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      Monkey666
      wrote on last edited by
      #3

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

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mlong
        wrote on last edited by
        #4

        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.

        Software Engineer
        My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mlong
          wrote on last edited by
          #5

          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()));
          @

          Software Engineer
          My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

          1 Reply Last reply
          0
          • M Offline
            M Offline
            Monkey666
            wrote on last edited by
            #6

            [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

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mlong
              wrote on last edited by
              #7

              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) {
              @

              Software Engineer
              My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

              1 Reply Last reply
              0
              • M Offline
                M Offline
                Monkey666
                wrote on last edited by
                #8

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

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  mlong
                  wrote on last edited by
                  #9

                  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.

                  Software Engineer
                  My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    Monkey666
                    wrote on last edited by
                    #10

                    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.

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      mlong
                      wrote on last edited by
                      #11

                      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 :)

                      Software Engineer
                      My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

                      1 Reply Last reply
                      0
                      • M Offline
                        M Offline
                        Monkey666
                        wrote on last edited by
                        #12

                        I got this to work:

                        http://qt-project.org/wiki/How_to_Use_QPushButton

                        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.

                        1 Reply Last reply
                        0
                        • M Offline
                          M Offline
                          Monkey666
                          wrote on last edited by
                          #13

                          Still looking for an answer on my last question.

                          1 Reply Last reply
                          0
                          • G Offline
                            G Offline
                            goetz
                            wrote on last edited by
                            #14

                            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.

                            http://www.catb.org/~esr/faqs/smart-questions.html

                            1 Reply Last reply
                            0

                            • Login

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • Users
                            • Groups
                            • Search
                            • Get Qt Extensions
                            • Unsolved