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.
myclass.h:
@
#include <QPushButton>
#include <QMessageBox>
#include <QString>class MyClass : public QWidget
{
Q_OBJECTpublic:
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) -
[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 -
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:
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.