Results 1 to 14 of 14

Thread: connect a qpushbutton a slot

  1. #1
    Join Date
    Dec 2009
    Location
    Mexico
    Posts
    26
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default connect a qpushbutton a slot

    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?
    Attached Files Attached Files

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,365
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: connect a qpushbutton a slot

    Quote Originally Posted by Lycus HackerEmo View Post
    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...
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Dec 2009
    Posts
    12
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: connect a qpushbutton a slot

    Slot for pushbutton:

    Qt Code:
    1. connect(menuGuardar, SIGNAL(clicked()),this, SLOT(on_menuGuardar_triggered()));
    To copy to clipboard, switch view to plain text mode 

    and about box is pretty much the same

    Qt Code:
    1. void myProg::on_about_clicked()
    2. {
    3. QMessageBox msgBox;
    4. msgBox.setWindowTitle("My About");
    5. msgBox.setText("About my program");
    6. msgBox.exec();
    7. }
    To copy to clipboard, switch view to plain text mode 

  4. The following user says thank you to progman for this useful post:

    Lycus HackerEmo (21st March 2010)

  5. #4
    Join Date
    Dec 2009
    Location
    Mexico
    Posts
    26
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: connect a qpushbutton a slot

    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:

    Qt Code:
    1. connect(ui->botonAbrir, SIGNAL(clicked()),this, SLOT(on_menuAbrir_triggered()));
    2. connect(ui->botonGuardar, SIGNAL(clicked()),this, SLOT(on_menuGuardar_triggered()));
    To copy to clipboard, switch view to plain text mode 

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


  6. #5
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: connect a qpushbutton a slot

    Quote Originally Posted by Lycus HackerEmo View Post
    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.

  7. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,365
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: connect a qpushbutton a slot

    Quote Originally Posted by Lycus HackerEmo View Post
    in the file *.ui add 2 buttons i wanted to do was connect it to the slot.
    What i did was:

    Qt Code:
    1. connect(ui->botonAbrir, SIGNAL(clicked()),this, SLOT(on_menuAbrir_triggered()));
    2. connect(ui->botonGuardar, SIGNAL(clicked()),this, SLOT(on_menuGuardar_triggered()));
    To copy to clipboard, switch view to plain text mode 
    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #7
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: connect a qpushbutton a slot

    Quote Originally Posted by wysota View Post
    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...

  9. #8
    Join Date
    Dec 2009
    Location
    Mexico
    Posts
    26
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: connect a qpushbutton a slot

    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.

    Qt Code:
    1. connect(ui->botonAbrir, SIGNAL(clicked()),this, SLOT(on_menuAbrir_clicked()));
    2. connect(ui->botonGuardar, SIGNAL(clicked()),this, SLOT(on_menuGuardar_clicked()));
    3. connect(ui->menuAbrir, SIGNAL(clicked()),this, SLOT(on_menuAbrir_clicked()));
    4. connect(ui->menuGuardar, SIGNAL(clicked()),this, SLOT(on_menuGuardar_clicked()));
    To copy to clipboard, switch view to plain text mode 
    Attached Files Attached Files

  10. #9
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: connect a qpushbutton a slot

    menuArir and menuGuardar don't have a "clicked()" signal. Use triggered() for these.

  11. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,365
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: connect a qpushbutton a slot

    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.

    Qt Code:
    1. QAction *act = new QAction(...);
    2. QToolButton *button = new QToolButton(...);
    3. button->addAction(act);
    4. connect(act, SIGNAL(triggered()), this, SLOT(doSomethingUseful()));
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. #11
    Join Date
    Dec 2009
    Location
    Mexico
    Posts
    26
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: connect a qpushbutton a slot

    and it's ok like this?

    and mark error:

    Qt Code:
    1. ‘class QAction’ has no member named ‘addAction’
    To copy to clipboard, switch view to plain text mode 
    Attached Files Attached Files

  13. #12
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: connect a qpushbutton a slot

    Quote Originally Posted by Lycus HackerEmo View Post
    Qt Code:
    1. ‘class QAction’ has no member named ‘addAction’
    To copy to clipboard, switch view to plain text mode 
    Which is correct. Note that wysota used addAction method of QToolButton.

  14. #13
    Join Date
    Dec 2009
    Location
    Mexico
    Posts
    26
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: connect a qpushbutton a slot

    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.
    Qt Code:
    1. void TextEditor::acerca_de()
    2. {
    3. QMessageBox::about(this, tr("Acerca de: TextEditor"), tr("Este Programa fue creado por:\n\n"
    4. "<h3>Lycus HackerEmo</h3>\n\n"
    5. "<a href="http://tuxmaya.wordpress.com">TuxMaya</a> "));
    6. }
    To copy to clipboard, switch view to plain text mode 
    Attached Files Attached Files

  15. #14
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,365
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: connect a qpushbutton a slot

    The code you pasted is syntactically incorrect. Even the forum highlighting shows that...
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. problem connect signal - slot
    By jaca in forum Newbie
    Replies: 13
    Last Post: 9th March 2010, 19:38
  2. A signal/slot connect isn't working.
    By Daimonie in forum Qt Programming
    Replies: 6
    Last Post: 15th February 2009, 22:55
  3. QObject::connect: No such slot !?!
    By Mystical Groovy in forum Qt Programming
    Replies: 3
    Last Post: 18th September 2008, 18:31
  4. Qt Designer & Qt4, connect to my own slot.
    By geitosten in forum Newbie
    Replies: 2
    Last Post: 17th February 2007, 19:22
  5. SLOT and QPushButton
    By mickey in forum Qt Programming
    Replies: 15
    Last Post: 15th February 2006, 06:46

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.