Hi,
I am trying to run a function when a Qt Push button is pressed. Here are my code snippets:
Interface header file
Class Constructor
Code:
connect(ui.action_button, SIGNAL(activated()), this, SLOT(function()));
Some help would be great!
Printable View
Hi,
I am trying to run a function when a Qt Push button is pressed. Here are my code snippets:
Interface header file
Class Constructor
Code:
connect(ui.action_button, SIGNAL(activated()), this, SLOT(function()));
Some help would be great!
First you might want to try the clicked() signal:
Code:
connect(ui.action_button, SIGNAL(clicked()), this, SLOT(function()));
If that doesn't work you need to provide more information (and code) so that we can see what you have done wrong.
Providing some idea what you want help with would be useful. Are you having trouble compiling, running, making it work at all, warning messages...?
Obvious issues:
- QPushButton does not emit a signal activated() and you should be receiving a warning to this effect when you run the program. Try clicked().
- You call the QPushButton "button" in where you create it, and ui.action_button where you try to connect() it. Does the value of "button" get placed into ui.action_button anywhere?
Other observations:
- Your "Interface header file" is an unusual place to put implementation details. Is this the header file the one that Designer/uic has built for you or one of your own creation?
- You also seem to be trying to manually position the widget rather than using the Qt layout system. While allowable, this is probably a bad idea.
Edit: Zlatomir beat me to it.
Ok, so I am now using proper variable names (action_button everywhere). I also changed signal to clicked()
Still does not work...
And the ui file is built by Qt creator. And yes, I do not use layouts.
Show some code from your files (not from auto-generated ui_fromName.h) so that we can see what is going on.
Also Chris post contains a lot more information that helps us help you solve this, and you didn't answer Chris questions, please do.
And use layouts, and don't try to write code in the generated files.
HEADER FILE
Code:
#include <QMainWindow> #include "ui_form.h" { Q_OBJECT public: Form1(); ~Form1(); protected slots: void function1(); }
CPP FILE
Code:
#include "form1.h" #include <QDebug> #include <QFile> #include <QTextStream> #include <QTimer> #include <QDialog> #include <QMessageBox> #include <QStringListModel> { connect(ui.action_button, SIGNAL(clicked()), this, SLOT(function1())); } void Form1::function1() { } Form1::~Form1() { }
I responded to the other thread you created.
Please don't open many threads for the same problem, since it will be hard for you to track the answers and for those who answer to track what has/hasn't been answered in many threads.
Which one is the thread where you answered?