Results 1 to 5 of 5

Thread: Can I pass a default value to a slot?

  1. #1
    Join Date
    Apr 2009
    Posts
    132
    Thanks
    67
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Can I pass a default value to a slot?

    Hi friends!

    I know the arguments of signals and slots are types, not variables, but is there any way to do something like this?

    connect(ui.pushButtonResetZoom, SIGNAL(pressed()), ui.doubleSpinBoxZoomFactor, SLOT(setValue("1.0")));

    (see last argument, 1.0)

    Thanks a lot.

  2. #2
    Join Date
    Dec 2009
    Location
    Romania, Iasi
    Posts
    18
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Can I pass a default value to a slot?

    You can try QSignalMapper. This way you can arrange to pass values with pressed() signal that your pushButtonResetZoom sends.

  3. #3
    Join Date
    Oct 2006
    Posts
    279
    Thanks
    6
    Thanked 40 Times in 39 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Can I pass a default value to a slot?

    You can define the function as:
    Qt Code:
    1. public slots:
    2. void setValue(QString value ="1.0");
    To copy to clipboard, switch view to plain text mode 
    this will actually create two slots.
    You can then connnect with the line
    Qt Code:
    1. public slots:
    2. connect(ui.pushButtonResetZoom, SIGNAL(pressed()), ui.doubleSpinBoxZoomFactor, SLOT(setValue()));
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Apr 2009
    Posts
    132
    Thanks
    67
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Can I pass a default value to a slot?

    Thanks spud , I already know that, the problem is that setValue belongs to a QDoubleSpinBox (btw, parameter is a double, change "1.0" by 1.0)

    Any idea?

    Thanks.

  5. #5
    Join Date
    Nov 2009
    Posts
    129
    Thanks
    4
    Thanked 29 Times in 29 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Can I pass a default value to a slot?

    I’m guessing that both the signal and the slot come from existing classes, so that it is inconvenient to re-implement them. If (as I’m also guessing) the slot is QDoubleSpinBox::setValue, which takes a double, then QSignalMapper probably won’t help since double is not among the types to which it can map.

    Probably the simplest way is to create a new signal and slot combination:
    Qt Code:
    1. signals:
    2. void setZoomFactor(double);
    3. public slots:
    4. void resetZoomFactor() {signal setZoomFactor(1.0);}
    To copy to clipboard, switch view to plain text mode 
    in any QObject you’re already sub-classing that lives in the GUI thread and is guaranteed to exist during the time you’ll need the signal. Then connect in the obvious way, using this object as a bridge.

    If ui.doubleSpinBoxZoomFactor is directly accessible from the QObject you use to tie the two widgets together (and it probably will be, because you’ll probably use the main window) you can eliminate the setZoomFactor signal and just call ui.doubleSpinBoxZoomFactor.setValue in the resetZoomFactor slot.

Similar Threads

  1. Replies: 4
    Last Post: 3rd May 2009, 18:32
  2. How to pass a QComboBox to a function?
    By Ricardo_arg in forum General Programming
    Replies: 4
    Last Post: 9th March 2008, 22:16
  3. Replies: 2
    Last Post: 16th August 2007, 00:20
  4. Pass by reference
    By vermarajeev in forum General Programming
    Replies: 6
    Last Post: 20th July 2007, 14:53
  5. signal slot conection using a string, not a SLOT
    By rianquinn in forum Qt Programming
    Replies: 6
    Last Post: 5th February 2006, 18:52

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
  •  
Qt is a trademark of The Qt Company.