Results 1 to 8 of 8

Thread: Action using Push Button ?

  1. #1
    Join Date
    Mar 2011
    Posts
    22
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question Action using Push Button ?

    Hi,

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

    Interface header file
    Qt Code:
    1. QPushButton *button;
    2. button = new QPushButton(tab);
    3. button->setObjectName(QString::fromUtf8("action_button"));
    4. button->setGeometry(QRect(20, 30, 210, 131));
    To copy to clipboard, switch view to plain text mode 

    Class Constructor
    Qt Code:
    1. connect(ui.action_button, SIGNAL(activated()), this, SLOT(function()));
    To copy to clipboard, switch view to plain text mode 

    Some help would be great!

  2. #2
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Action using Push Button ?

    First you might want to try the clicked() signal:
    Qt Code:
    1. connect(ui.action_button, SIGNAL(clicked()), this, SLOT(function()));
    To copy to clipboard, switch view to plain text mode 

    If that doesn't work you need to provide more information (and code) so that we can see what you have done wrong.

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Action using Push Button ?

    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.

  4. #4
    Join Date
    Mar 2011
    Posts
    22
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Action using Push Button ?

    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.

  5. #5
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Action using Push Button ?

    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.

  6. #6
    Join Date
    Mar 2011
    Posts
    22
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Action using Push Button ?

    Quote Originally Posted by Zlatomir View Post
    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
    Qt Code:
    1. #include <QMainWindow>
    2. #include "ui_form.h"
    3.  
    4. class Form1:public QMainWindow
    5. {
    6. Q_OBJECT
    7. public:
    8. Form1();
    9. ~Form1();
    10.  
    11. protected slots:
    12. void function1();
    13. }
    To copy to clipboard, switch view to plain text mode 

    CPP FILE
    Qt Code:
    1. #include "form1.h"
    2. #include <QDebug>
    3. #include <QFile>
    4. #include <QTextStream>
    5. #include <QTimer>
    6. #include <QDialog>
    7. #include <QMessageBox>
    8. #include <QStringListModel>
    9.  
    10. Form1::Form1():QMainWindow(NULL,0)
    11. {
    12. connect(ui.action_button, SIGNAL(clicked()), this, SLOT(function1()));
    13. }
    14.  
    15. void Form1::function1()
    16. {
    17. }
    18.  
    19. Form1::~Form1()
    20. {
    21. }
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Action using Push Button ?

    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.

  8. #8

    Default Re: Action using Push Button ?

    Which one is the thread where you answered?

Similar Threads

  1. Bitmap on Push Button in QT 3
    By Gokulnath in forum Newbie
    Replies: 2
    Last Post: 12th January 2011, 07:34
  2. [QT]Push button-no action
    By nqn in forum Newbie
    Replies: 4
    Last Post: 30th May 2010, 19:08
  3. Hareware Push Button
    By mickeyk191 in forum Newbie
    Replies: 2
    Last Post: 26th January 2010, 12:41
  4. shape of push button
    By Seema Rao in forum Qt Programming
    Replies: 23
    Last Post: 2nd April 2008, 01:05
  5. Push Button problem!!
    By Seema Rao in forum Qt Programming
    Replies: 1
    Last Post: 22nd April 2006, 16:31

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.