PDA

View Full Version : Buttons



valy12
18th April 2010, 21:43
Hello,

i have read few forum on it but i need your help for a little problem :

i want to put a button on my window,
i declared my button on the main with :
QPushButton *bouton = new QPushButton("Connecter",ui->pushButton);
bouton->setCheckable(true);

i would like to make an action named "connection" when i click on it. So i connect the button but i don't know what i should write instead of the "???"
connect(ui->pushButton,SIGNAL(clicked()),????,SLOT(connection( )));

should i create a new file to put my function into?

Thanks

Lykurg
18th April 2010, 21:52
first you have to declare connection() as a slot. second, it does not matter where (in which class) you declare that slot, just put a pointer to that class instead of ?????.
Most likely, you declare that slot in the widget you create your button, then use this for ???.

borisbn
19th April 2010, 04:21
1. Is it that you really wanted to put new buttun bouton INSIDE another button ui->pushButton ?
2. If you have a QAction, named connection, you should do

connect(ui->pushButton,SIGNAL(clicked
()),ui-> connection,SLOT(trigger()));

valy12
19th April 2010, 10:32
thanks, but i still have a problem ^^

i declared my function connection as a slot like that in my header :
class MainWindow : public QMainWindow
{
Q_OBJECT
....
public slots :
void connection();
...
};

in the .cpp i write that :
QPushButton *bouton = new QPushButton("Connecter Wiimote",ui->pushButton);
bouton->setCheckable(true);
QObject::connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(connection( )));

my function connection is :
void MainWindow::connection()
{
...
}

There is no errors at the compilation.
However when i cllick on the button, the code in my function connection is not executed... what's wrong?

Chexov
19th April 2010, 10:53
emmm)
Write this and all will earn:

QObject:: connect (bouton, SIGNAL (clicked(), this, SLOT (connection ()));

valy12
19th April 2010, 10:58
cool! i'm a joke... ^^
Thanks :D

Lykurg
19th April 2010, 11:01
if you use ui you don't have to create a new button. ui->pushButton is already a full button, so there is no need to recreate one. Simply use your
QObject::connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(connection( ))); and skip all the
QPushButton *bouton = new QPushButton("Connecter Wiimote",ui->pushButton);
bouton->setCheckable(true); things...

Lykurg

Chexov
19th April 2010, 11:10
Lykurg you are right, but its way is amusing and works)
valy12 when you add object in gui redactor, it is not obligatory to describe again the button in a code, the nobility its objective name suffices and to address to it it is possible through the index ui)
Sorry for my english,i am from Russia)

valy12
19th April 2010, 12:16
You 're right it's better! :)
i use c++ and qt since one week so forgive me :)

Thanks all