Results 1 to 4 of 4

Thread: What's the difference?

  1. #1
    Join Date
    Jun 2011
    Posts
    203
    Thanks
    7
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default What's the difference?

    Hi, I'm just wondering what the difference between the Qt documentation example:

    Qt Code:
    1. signalMapper = new QSignalMapper(this);
    2. signalMapper->setMapping(taxFileButton, QString("taxfile.txt"));
    3. signalMapper->setMapping(accountFileButton, QString("accountsfile.txt"));
    4. signalMapper->setMapping(reportFileButton, QString("reportfile.txt"));
    5.  
    6. connect(taxFileButton, SIGNAL(clicked()),
    7. signalMapper, SLOT (map()));
    8. connect(accountFileButton, SIGNAL(clicked()),
    9. signalMapper, SLOT (map()));
    10. connect(reportFileButton, SIGNAL(clicked()),
    11. signalMapper, SLOT (map()));
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. connect(signalMapper, SIGNAL(mapped(QString)),
    2. this, SLOT(readFile(QString)));
    To copy to clipboard, switch view to plain text mode 

    As opposed to having something like this would be?:

    button.h
    Qt Code:
    1. class Button : public QWidget
    2. {
    3. Q_OBJECT
    4. public:
    5. explicit Button(QWidget *parent = 0);
    6.  
    7. QString m_sName;
    8.  
    9. Button(QString sName) : m_sName(sName)
    10. {}
    11.  
    12. signals:
    13. void ButtonClicked(QString m_sName);
    14. }
    To copy to clipboard, switch view to plain text mode 

    otherfile.cpp
    Qt Code:
    1. {
    2. //somewhere init something like 20 button classes.
    3.  
    4. void class::class_sSlot(QString name)
    5. {
    6. if(name == "a")
    7. {
    8. //do something
    9. }
    10. else if(name == "b")
    11. {
    12. //do something
    13. }
    14. //...etc for remaining 18 comparisons
    15. }
    To copy to clipboard, switch view to plain text mode 

    Would there be a speed advantage of doing it the way it's presented in the Qt documentation over the second example?
    Last edited by Atomic_Sheep; 5th September 2012 at 18:22.

  2. #2
    Join Date
    May 2011
    Posts
    239
    Thanks
    4
    Thanked 35 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Symbian S60

    Default Re: What's the difference?

    I would guess that there is no speed difference.

    Now, the difference is in maintenance. The QSignalMapper brings the mappings (!) into plain view, in a declarative stype that is easy to modify when needed. The alternative style is a mess of code, logic and symbols. In fact, every time that one sees such a structure with 20 comparisons, the first thought should be: how can I get rid of all this code and just describe these mappins in some neat way; in a table or a list perhaps... and a list is exactly what QSignalMapper makes possible.

  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: What's the difference?

    The signal mapper has two signal/slot calls to make: button to mapper, lookup, mapper to slot. The other approach has two also: button clicked to button slot emitting ButtonClicked() to end slot. Not much difference.

    You should only worry about tweaking performance when the program works and you have a measurable performance bottleneck... and then you work the bottleneck. Your CPU could probably process the signal and call the slot in the order of a thousand of times per second... how slow is the actual work of the slot in comparison?

  4. #4
    Join Date
    Jun 2011
    Posts
    203
    Thanks
    7
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: What's the difference?

    Thanks, makes sense.

Similar Threads

  1. difference between two objects
    By rk0747 in forum Newbie
    Replies: 1
    Last Post: 15th April 2010, 09:30
  2. Difference between the regulare MVC
    By Jonas_ in forum Newbie
    Replies: 3
    Last Post: 2nd September 2009, 14:59
  3. A difference between debug build with gcc and vc++
    By piotr.dobrogost in forum Qt Tools
    Replies: 12
    Last Post: 30th July 2009, 12:09
  4. how to calculate difference b/w two times
    By dummystories in forum Newbie
    Replies: 1
    Last Post: 9th March 2009, 13:58
  5. Replies: 1
    Last Post: 7th October 2008, 12:11

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.