PDA

View Full Version : Action using Push Button ?



steve.bush
17th March 2011, 21:17
Hi,

I am trying to run a function when a Qt Push button is pressed. Here are my code snippets:

Interface header file


QPushButton *button;
button = new QPushButton(tab);
button->setObjectName(QString::fromUtf8("action_button"));
button->setGeometry(QRect(20, 30, 210, 131));


Class Constructor


connect(ui.action_button, SIGNAL(activated()), this, SLOT(function()));


Some help would be great!

Zlatomir
17th March 2011, 21:51
First you might want to try the clicked() signal:

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.

ChrisW67
17th March 2011, 21:54
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.

steve.bush
18th March 2011, 22:18
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.

Zlatomir
18th March 2011, 22:59
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.

steve.bush
18th March 2011, 23:05
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


#include <QMainWindow>
#include "ui_form.h"

class Form1:public QMainWindow
{
Q_OBJECT
public:
Form1();
~Form1();

protected slots:
void function1();
}


CPP FILE


#include "form1.h"
#include <QDebug>
#include <QFile>
#include <QTextStream>
#include <QTimer>
#include <QDialog>
#include <QMessageBox>
#include <QStringListModel>

Form1::Form1():QMainWindow(NULL,0)
{
connect(ui.action_button, SIGNAL(clicked()), this, SLOT(function1()));
}

void Form1::function1()
{
}

Form1::~Form1()
{
}

Zlatomir
18th March 2011, 23:09
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.

llorenzi
25th February 2016, 15:36
Which one is the thread where you answered?