Results 1 to 13 of 13

Thread: Numerous functions with similar names/actions

  1. #1
    Join Date
    Aug 2007
    Location
    Ontario, Canada
    Posts
    22
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Numerous functions with similar names/actions

    Hi there. This is more of a C++ question than Qt but this is the first time I have run into it...

    I am working on a simple grade calculator using spin boxes. The number of spin boxes (for grade and assignment weighting) is determined by how many grades the user inputs in another slider/spinbox. My program works fine but there is ALOT of redundant code it seems. I would really like to shorten it up, make it more managable because there are some changes I would like to make.

    For example. Right now I have 8 functions like this:

    Qt Code:
    1. void MainWindow::show_ass_1()
    2. {
    3. ass_label_1->show();
    4. ass_grade_SpinBox_1->show();
    5. ass_weight_SpinBox_1->show();
    To copy to clipboard, switch view to plain text mode 

    Labelled 1 - 8 with accompanying labels and spinboxes of the same number. These are my functions just to unhide part of the interface. To hide them, there are 8 more functions that do the opposite. To call the functions I am using a switch statement.

    Is there a way that I can somehow reduce the number of lines? If someone can point me in the right direction I would appreciate it, thanks.

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

    Post Re: Numerous functions with similar names/actions

    This is how you could do it with arrays:
    Qt Code:
    1. class MainWindow
    2. {
    3. ...
    4. private:
    5. QLabel* ass_labels[8];
    6. QSpinBox* ass_grade_SpinBoxes[8];
    7. QSpinBox* ass_weight_SpinBoxes[8];
    8. };
    9.  
    10. void MainWindow::show_ass(int i)
    11. {
    12. ass_labels[i]->show();
    13. ass_grade_SpinBoxes[i]->show();
    14. ass_weight_SpinBoxes[i]->show();
    15. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  3. The following user says thank you to jpn for this useful post:

    kramed (14th August 2007)

  4. #3
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Numerous functions with similar names/actions

    Lots of asses in there...

  5. #4
    Join Date
    May 2006
    Location
    Bangalore,India
    Posts
    235
    Thanks
    7
    Thanked 25 Times in 24 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Numerous functions with similar names/actions

    Marcel,
    do you mean if convert ass to horse then it work fine...ha..ha..ha..

    do you know how to convert ass to horse??
    .
    .
    .
    .
    ans: just multiply by horse/ass.

  6. #5
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Numerous functions with similar names/actions


    I think a moderator will delete the last two posts.
    The show_ass function is ambiguous.

    Regards

  7. #6
    Join Date
    May 2006
    Location
    Bangalore,India
    Posts
    235
    Thanks
    7
    Thanked 25 Times in 24 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Numerous functions with similar names/actions

    marcel,
    it is just a coding, it is a sort name of somthing, but you made it ambiguous.
    I request to moderator to delete the last four posts including my this post also.

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

    Default Re: Numerous functions with similar names/actions

    Quote Originally Posted by marcel View Post
    I think a moderator will delete the last two posts.
    Why should we?

  9. #8
    Join Date
    Aug 2007
    Location
    Ontario, Canada
    Posts
    22
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Numerous functions with similar names/actions

    Thank you jpn. That will help greatly.

    show_ass stands for show_assignments. I didnt think people would get such a rouse out of it, haha.

  10. #9
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Numerous functions with similar names/actions

    Quote Originally Posted by kramed View Post
    Thank you jpn. That will help greatly.

    show_ass stands for show_assignments. I didnt think people would get such a rouse out of it, haha.
    It is still funny.

    Quote:
    Originally Posted by marcel
    I think a moderator will delete the last two posts.

    Why should we?
    They have nothing to do with the original question...


    Regards

  11. #10
    Join Date
    May 2006
    Location
    Bangalore,India
    Posts
    235
    Thanks
    7
    Thanked 25 Times in 24 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Talking Re: Numerous functions with similar names/actions

    marcel,
    I saw that JPN answer was correct so I replyed your funny thing only.
    it look like you are funny guy and enjoying while programming. good carryon..
    but dont make more fun on this that make bad.

  12. #11
    Join Date
    Aug 2007
    Location
    Ontario, Canada
    Posts
    22
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Numerous functions with similar names/actions

    jpn, I have changed my code to what you suggested and it compiles but unforunately I am setgfaulting on an invalid call to show(); Do the object names need to be named like an array (ie, label[1]) or do they stay the same (ie, label1). I have created the UI in designer and it wont let me input square brackets into the object name. If I must name the object names as arrays I would have to modifiy the .ui manually.

    Is there anyway to determine the exact line that the program is failing on, gdb's output was:
    Program received signal SIGSEGV, Segmentation fault.
    [Switching to Thread -1223964144 (LWP 19874)]
    0x0804bd1b in QWidget::show (this=0x0) at /usr/include/qt4/QtGui/qwidget.h:463
    463 inline void show() { setVisible(true); }

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

    Default Re: Numerous functions with similar names/actions

    Quote Originally Posted by kramed View Post
    jpn, I have changed my code to what you suggested and it compiles but unforunately I am setgfaulting on an invalid call to show(); Do the object names need to be named like an array (ie, label[1]) or do they stay the same (ie, label1). I have created the UI in designer and it wont let me input square brackets into the object name.
    This happens because you have an uninitialized array of pointers. You must assign sensible values to pointers in the array. Unfortunately there is no way to tell designer to assign to a custom array. What you can do about it is to assign them by hand:
    Qt Code:
    1. setupUi(this);
    2. ass_labels[0] = ass_label1;
    3. ass_labels[1] = ass_label2;
    4. ...
    To copy to clipboard, switch view to plain text mode 

    Is there anyway to determine the exact line that the program is failing on, gdb's output was:
    Try command "bt" aka backtrace.
    J-P Nurmi

  14. The following user says thank you to jpn for this useful post:

    kramed (14th August 2007)

  15. #13
    Join Date
    Aug 2007
    Location
    Ontario, Canada
    Posts
    22
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Numerous functions with similar names/actions

    Thank you very much for the assistance, you have been a great help indeed.

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.