Page 1 of 2 12 LastLast
Results 1 to 20 of 27

Thread: Signal-slots connection problem.

  1. #1
    Join Date
    Jan 2006
    Location
    Riverside, Rhode Island, USA
    Posts
    245
    Thanks
    52
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Signal-slots connection problem.

    I am using the Qt-4 designer "ui" front end for controlling all aspects of a complex program I am writing.

    Each of the "pushbuttons" in the designer window will have many different actions (and labels) depending on program requirements.

    My problem is connecting the "signal" (click) of a button to a "slot" in a "control.cpp" function.

    In qt3, i could write a "slot", but am at a loss herep

    help would be appreciated.
    thanks.

  2. #2
    Join Date
    Jan 2006
    Location
    Alingsås, Sweden
    Posts
    437
    Thanks
    3
    Thanked 39 Times in 39 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Signal-slots connection problem.


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

    Default Re: Signal-slots connection problem.


  4. #4
    Join Date
    Jan 2006
    Location
    Riverside, Rhode Island, USA
    Posts
    245
    Thanks
    52
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Signal-slots connection problem.

    Thanks
    No I haven't on both counts. Will do.

  5. #5
    Join Date
    Mar 2006
    Location
    Mountain View, California
    Posts
    489
    Thanks
    3
    Thanked 74 Times in 54 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Signal-slots connection problem.

    Quote Originally Posted by impeteperry View Post
    In qt3, i could write a "slot", but am at a loss here
    Designer is no longer a text editor in Qt 4. It's not trying to be an IDE. It's just a form editor. If you want to write code, you will need to use your favorite text editor. Write the code for the slot in your widget class using your text editor. It really isn't that hard.

  6. #6
    Join Date
    Jan 2006
    Location
    Riverside, Rhode Island, USA
    Posts
    245
    Thanks
    52
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Signal-slots connection problem.

    Hi mysota,
    I had actually used the "calculatorform" you suggested as a guide for my program
    In the example, Items change from within the designer as a result of a user action on an item.

    I have a row of 12 "PushButtons" and a row of 12 labels (F1....F12)
    The labels on the buttons change as the program progresses.

    In my program the user has to click on a button, press a keyboard function key or enter an accelerator letter to do anything.

    So, I need to know in my calculatorform.cpp which button was clicked. (function keys and accelerator letters aren't any problem).

    I have looked at other examples and i haven't found this condition addressed.

    I know this approach is not standard, but neither is my program.
    To better understand what I'm trying to do look at "Qcad" . Instead of Icons, I have push buttons across the bottom of the screen to perform the same type of function.

    thanks

  7. #7
    Join Date
    Mar 2006
    Location
    Mountain View, California
    Posts
    489
    Thanks
    3
    Thanked 74 Times in 54 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Signal-slots connection problem.

    Quote Originally Posted by impeteperry View Post
    So, I need to know in my calculatorform.cpp which button was clicked. (function keys and accelerator letters aren't any problem).
    Sorry, I misunderstood your question in my response. What you are looking for is either QSignalMapper, or using QObject::sender().

    But be careful of your design. If you are using function keys, accelerators and buttons to initiate actions, then you might want to use QActions for all of your actions. Just a thought.

  8. #8
    Join Date
    Jan 2006
    Location
    Riverside, Rhode Island, USA
    Posts
    245
    Thanks
    52
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Signal-slots connection problem.

    thanks for you suggestions, I will look into them.

    I have been writting this program over the past year or so in straight Qt4. using hard coding for the user interface. (which remains the same throughout the program.) This coding is large, unwieldy and time consuming to make changes.

    So, for the new year I would go back and use "designer" for the front end. (I did the original in Qt-3 designer and then used the "ui_,,," file as a guide for the hard coding)

    By the way,I had written a crude "labels" program using the same type of user interface for an elderly friend of mine who was having all kinds of trouble with "Notes" and "MS office" sorting routines etc.
    It took her 20 minutes to learn and use my program. No icons, no popup or popdown menus etc.

  9. #9
    Join Date
    Jan 2006
    Location
    Riverside, Rhode Island, USA
    Posts
    245
    Thanks
    52
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Signal-slots connection problem.

    Yes, QActions could do it. i had used them in a visualBasic program several years ago, but I would prefer to do it in designer, not as a menu bar.

    I don't think QSignalMapper or QObject::sender() are designed to do what I want.

    I am not sure what your warning obout using, button, function keys and/or accelerator keys concerns.

    thanks for the help and suggestions.

  10. #10
    Join Date
    Jan 2006
    Location
    Riverside, Rhode Island, USA
    Posts
    245
    Thanks
    52
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Signal-slots connection problem.

    I found this in the documentation
    void QAbstractButton::click () [slot]

    Performs a click.

    All the usual signals associated with a click are emitted as appropriate. If the button is checkable, the state of the button is toggled.

    This function does nothing if the button is disabled.
    so went back to my non-detigner program for button click.
    Qt Code:
    1. connect( pb5, SIGNAL( clicked() ), this, SLOT( slotPb5() ) );
    To copy to clipboard, switch view to plain text mode 
    Then went to the implementation
    Qt Code:
    1. void dlgMain::slotPb5()
    2. {
    3.  
    4. if( pb5->text() == "~" ) return;
    5. emit setButtonActionList( 5, "5" );
    To copy to clipboard, switch view to plain text mode 
    so why not in designer? In the "Signnl Slot Editor" i have:
    " pb5 clicked Pb5 click() "
    and in my program I have
    Qt Code:
    1. #include <QtGui>
    2.  
    3. #include "pmform.h"
    4.  
    5. PmForm::PmForm(QWidget *parent)
    6. : QWidget(parent)
    7. {
    8. ui.setupUi(this);
    9.  
    10. ui.leZone->setText("leZone");
    11. }
    12.  
    13. void PmForm::Pb5()
    14. {
    15. ui.leZone->setText("This is what I want");
    16. }
    To copy to clipboard, switch view to plain text mode 
    it compiles ok, but when I run it and click on the "Pb5" button I get
    pete@peteBox:~/Desktop/PM-designer/pm$ ./pm
    Segmentation fault
    pete@peteBox:~/Desktop/PM-designer/pm$
    At least I am getting a connection form inside designer to my "pmform.cpp"
    Now, how do I get rid of the segmentation fault?

    Thanks, Help would be most appreciatei.

  11. #11
    Join Date
    Jan 2006
    Location
    Riverside, Rhode Island, USA
    Posts
    245
    Thanks
    52
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Signal-slots connection problem.

    I was wrong in the above post. I did not get the connection, i just exited the designer. There HAS to be a simple asswer somewhere!!!

    discouraged.

  12. #12
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,518 Times in 1,388 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Signal-slots connection problem.

    How about QButtonGroup? You can either identify each button with an identifier and use signal QButtonGroup::clicked(int id), or alternatively you can use signal QButtonGroup::clicked(QAbstractButton* button) to get pointer to the button which was clicked. The already suggested QSignalMapper and QObject::sender() would have had done basically the same.
    J-P Nurmi

  13. #13
    Join Date
    Jan 2006
    Location
    Riverside, Rhode Island, USA
    Posts
    245
    Thanks
    52
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Signal-slots connection problem.

    Thanks, I'll look at your suggestions before I go back to Qt3 which worked fine.

  14. #14
    Join Date
    Jan 2006
    Location
    Riverside, Rhode Island, USA
    Posts
    245
    Thanks
    52
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Signal-slots connection problem.

    OK thanks to you all who tried to help me. I appreciate it.
    I have solved my problem.

    I did not use the "signal/slot editor" in designer.

    I added the followi code to my constructor
    Qt Code:
    1. connect( ui.pb1, SIGNAL(clicked()), this, SLOT(slotPb1()));
    2. connect( ui.pb2, SIGNAL(clicked()), this, SLOT(slotPb2()));
    3. connect( ui.pb3, SIGNAL(clicked()), this, SLOT(slotPb3()));
    4. connect( ui.pb4, SIGNAL(clicked()), this, SLOT(slotPb4()));
    5. connect( ui.pb5, SIGNAL(clicked()), this, SLOT(slotPb5()));
    6. connect( ui.pb6, SIGNAL(clicked()), this, SLOT(slotPb6()));
    7. connect( ui.pb7, SIGNAL(clicked()), this, SLOT(slotPb7()));
    8. connect( ui.pb8, SIGNAL(clicked()), this, SLOT(slotPb8()));
    9. connect( ui.pb9, SIGNAL(clicked()), this, SLOT(slotPb9()));
    10. connect( ui.pb10, SIGNAL(clicked()), this, SLOT(slotPb10()));
    11. connect( ui.pb11, SIGNAL(clicked()), this, SLOT(slotPb11()));
    12. connect( ui.pb12, SIGNAL(clicked()), this, SLOT(slotPb12()));
    To copy to clipboard, switch view to plain text mode 
    and the slots
    Qt Code:
    1. void PmForm::slotPb1()
    2. {
    3. ui.leZone->setText("This is Pb1");
    4. }
    5. void PmForm::slotPb2()
    6. {
    7. ui.leZone->setText("This is Pb2");
    8. }
    9. void PmForm::slotPb3()
    10. {
    11. ui.leZone->setText("This is Pb3");
    12. }
    To copy to clipboard, switch view to plain text mode 
    etc.
    This does what I want. How simple and obvious!!

  15. #15
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,518 Times in 1,388 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Signal-slots connection problem.

    Qt Code:
    1. QButtonGroup* grp = new QButtonGroup(this);
    2. grp->addButton(ui.pb1, 1);
    3. grp->addButton(ui.pb2, 2);
    4. ...
    5. grp->addButton(ui.pb12, 12);
    6. connect(grp, SIGNAL(buttonClicked(int)), this, SLOT(slotPb(int))
    7.  
    8. void PmForm::slotPb(int id)
    9. {
    10. ui.leZone->setText(QString("This is Pb%1").arg(id));
    11. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  16. #16
    Join Date
    Jan 2006
    Location
    Riverside, Rhode Island, USA
    Posts
    245
    Thanks
    52
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Signal-slots connection problem.

    jpn, I had posted my last post before I opened yours which would have given me the clue I needed. thanks

  17. #17
    Join Date
    Mar 2006
    Location
    Mountain View, California
    Posts
    489
    Thanks
    3
    Thanked 74 Times in 54 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Signal-slots connection problem.

    Quote Originally Posted by impeteperry View Post
    Qt Code:
    1. connect( ui.pb1, SIGNAL(clicked()), this, SLOT(slotPb1()));
    2. connect( ui.pb2, SIGNAL(clicked()), this, SLOT(slotPb2()));
    3. connect( ui.pb3, SIGNAL(clicked()), this, SLOT(slotPb3()));
    4. connect( ui.pb4, SIGNAL(clicked()), this, SLOT(slotPb4()));
    5. connect( ui.pb5, SIGNAL(clicked()), this, SLOT(slotPb5()));
    6. connect( ui.pb6, SIGNAL(clicked()), this, SLOT(slotPb6()));
    7. connect( ui.pb7, SIGNAL(clicked()), this, SLOT(slotPb7()));
    8. connect( ui.pb8, SIGNAL(clicked()), this, SLOT(slotPb8()));
    9. connect( ui.pb9, SIGNAL(clicked()), this, SLOT(slotPb9()));
    10. connect( ui.pb10, SIGNAL(clicked()), this, SLOT(slotPb10()));
    11. connect( ui.pb11, SIGNAL(clicked()), this, SLOT(slotPb11()));
    12. connect( ui.pb12, SIGNAL(clicked()), this, SLOT(slotPb12()));
    To copy to clipboard, switch view to plain text mode 
    Ouch! As jpn says, this is a candidate for QButtonGroup.

    When you have many similar controls connecting to many similar slots, it is a clue that come sort of consolidation is needed. One appproach is QSignalMapper, which you can use to map senders to specific slot parameter values. It is good for the general case, but you have a specific case where all senders are buttons. Because of that, QButtonGroup is the simplest choice.

  18. #18
    Join Date
    Jan 2006
    Location
    Riverside, Rhode Island, USA
    Posts
    245
    Thanks
    52
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Signal-slots connection problem.

    Ok, as I see it, with a button group, I would need 1 signal slot in which the position of the button is returned and that is what I want.

    I shall try it.
    Thanks

  19. #19
    Join Date
    Jan 2006
    Location
    Riverside, Rhode Island, USA
    Posts
    245
    Thanks
    52
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Signal-slots connection problem.

    I tried the "button box" as you suggested. I didn't spend much time, but the button box insisted on an "OK" and a "Cancel" button which I don't want

    I then tried the group box. It took up more vertical height then just have the buttons as I have done.

    I am willing to spend more time in programming to save valuable screen space, but it was a worthwhile suggestion. thanks.

  20. #20
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,518 Times in 1,388 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Signal-slots connection problem.

    ??

    QButtonGroup is nothing visible, it just manages buttons as a group. There is no way to setup a QButtonGroup from the Designer. The above example shows how to use QButtonGroup to avoid separate slots for each of those 12 buttons...
    J-P Nurmi

Similar Threads

  1. Signal and slots
    By villy in forum Qt Programming
    Replies: 1
    Last Post: 12th January 2007, 10:10
  2. connection problem
    By mhoover in forum Qt Programming
    Replies: 1
    Last Post: 7th July 2006, 22:14
  3. Problem with signals and slots
    By conexion2000 in forum Qt Programming
    Replies: 2
    Last Post: 23rd March 2006, 10:20
  4. Problem with Signals and Slots
    By Kapil in forum Newbie
    Replies: 11
    Last Post: 15th February 2006, 11:35
  5. Problem with Signal and Slots
    By Kapil in forum Installation and Deployment
    Replies: 2
    Last Post: 10th February 2006, 08:51

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.