PDA

View Full Version : connect a qpushbutton a slot



Lycus HackerEmo
20th March 2010, 22:15
I am creating a text editor. I create 2 QPushButton one called Abrir and a Guardar, as you do for that clicking these button do what it say the slot?

void on_menuGuardar_triggered();
void on_menuAbrir_triggered();

and another question. in most of the programs in the Help menu, is an option called About. the windows is a QDialog or QMessageBox?

wysota
21st March 2010, 02:02
I am creating a text editor. I create 2 QPushButton one called Abrir and a Guardar, as you do for that clicking these button do what it say the slot?
Could you rephrase your question?


and another question. in most of the programs in the Help menu, is an option called About. the windows is a QDialog or QMessageBox?
QMessageBox is also a QDialog, so...

progman
21st March 2010, 02:29
Slot for pushbutton:



connect(menuGuardar, SIGNAL(clicked()),this, SLOT(on_menuGuardar_triggered()));


and about box is pretty much the same



void myProg::on_about_clicked()
{
QMessageBox msgBox;
msgBox.setWindowTitle("My About");
msgBox.setText("About my program");
msgBox.exec();
}

Lycus HackerEmo
21st March 2010, 03:33
Could you rephrase your question?

in the file *.ui add 2 buttons i wanted to do was connect it to the slot.
What i did was:



connect(ui->botonAbrir, SIGNAL(clicked()),this, SLOT(on_menuAbrir_triggered()));
connect(ui->botonGuardar, SIGNAL(clicked()),this, SLOT(on_menuGuardar_triggered()));


and on the About menu. was how to create this window? and how you create this link on the website?

http://img291.imageshack.us/img291/8339/instantnea3x.th.png (http://img291.imageshack.us/i/instantnea3x.png/)

Lykurg
21st March 2010, 08:05
and on the About menu. was how to create this window? and how you create this link on the website?
QVBoxLayout, QTabWidget, QLabel, QPushbutton. A QLabel can display links. Just set html as a text.

wysota
21st March 2010, 08:41
in the file *.ui add 2 buttons i wanted to do was connect it to the slot.
What i did was:



connect(ui->botonAbrir, SIGNAL(clicked()),this, SLOT(on_menuAbrir_triggered()));
connect(ui->botonGuardar, SIGNAL(clicked()),this, SLOT(on_menuGuardar_triggered()));

You shouldn't be doing it like this. The on_objectname_signalname() naming convention says the slot will be executed upon a "signalname" signal from "objectname" object. So either change the name to something meaningful and leave the connect statements as they are or get rid of the connect statements and change slot names so that they have "clicked" instead of "triggered" in their names.


and on the About menu. was how to create this window? and how you create this link on the website?
Take a look at QMessageBox::about(). You can pass rich text (including links) there.

Lykurg
21st March 2010, 08:57
You shouldn't be doing it like this. The on_objectname_signalname() naming convention says the slot will be executed upon a "signalname" signal from "objectname" object. So either change the name to something meaningful and leave the connect statements as they are or get rid of the connect statements and change slot names so that they have "clicked" instead of "triggered" in their names.
:) I thought the same way but then I saw that there is also an action named menuAbrir for which the slot is for. So the button is only additional. So the decission is to make all by hand or let Qt do at least one connection...

Lycus HackerEmo
22nd March 2010, 01:32
You shouldn't be doing it like this. The on_objectname_signalname() naming convention says the slot will be executed upon a "signalname" signal from "objectname" object. So either change the name to something meaningful and leave the connect statements as they are or get rid of the connect statements and change slot names so that they have "clicked" instead of "triggered" in their names.

the signalname "triggered" only used in the menuBar and not be used in QPushButton?
have an example of what i should do?

I delete the "triggered" and change it to the "clicked", but does not work the menuAbrir and the menuGuardar.


connect(ui->botonAbrir, SIGNAL(clicked()),this, SLOT(on_menuAbrir_clicked()));
connect(ui->botonGuardar, SIGNAL(clicked()),this, SLOT(on_menuGuardar_clicked()));
connect(ui->menuAbrir, SIGNAL(clicked()),this, SLOT(on_menuAbrir_clicked()));
connect(ui->menuGuardar, SIGNAL(clicked()),this, SLOT(on_menuGuardar_clicked()));

squidge
22nd March 2010, 07:08
menuArir and menuGuardar don't have a "clicked()" signal. Use triggered() for these.

wysota
22nd March 2010, 08:55
I would really suggest to change the slot names to something meaningful and issue connect statements for both the buttons and the actions. Or better yet make your buttons tool buttons and add the actions you created to them. Then you won't have to double the connect statements.


QAction *act = new QAction(...);
QToolButton *button = new QToolButton(...);
button->addAction(act);
connect(act, SIGNAL(triggered()), this, SLOT(doSomethingUseful()));

Lycus HackerEmo
23rd March 2010, 04:27
and it's ok like this?

and mark error:


‘class QAction’ has no member named ‘addAction’

squidge
23rd March 2010, 07:51
‘class QAction’ has no member named ‘addAction’Which is correct. Note that wysota used addAction method of QToolButton.

Lycus HackerEmo
29th March 2010, 07:53
forgiveness for late reply, was busy with affairs of the school
and happy to have 2 weeks of easter holiday. xD

okay this program?

there is an error here? not working wants.

void TextEditor::acerca_de()
{
QMessageBox::about(this, tr("Acerca de: TextEditor"), tr("Este Programa fue creado por:\n\n"
"<h3>Lycus HackerEmo</h3>\n\n"
"<a href="http://tuxmaya.wordpress.com">TuxMaya</a> "));
}

wysota
29th March 2010, 09:14
The code you pasted is syntactically incorrect. Even the forum highlighting shows that...