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:
Code:
void MainWindow::show_ass_1()
{
ass_label_1->show();
ass_grade_SpinBox_1->show();
ass_weight_SpinBox_1->show();
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.
Re: Numerous functions with similar names/actions
This is how you could do it with arrays:
Code:
class MainWindow
{
...
private:
};
void MainWindow::show_ass(int i)
{
ass_labels[i]->show();
ass_grade_SpinBoxes[i]->show();
ass_weight_SpinBoxes[i]->show();
}
Re: Numerous functions with similar names/actions
Lots of asses in there...
Re: Numerous functions with similar names/actions
Marcel,
do you mean if convert ass to horse then it work fine...ha..ha..ha..:D
do you know how to convert ass to horse??:cool:
.
.
.
.
ans: just multiply by horse/ass.:p
Re: Numerous functions with similar names/actions
:)
I think a moderator will delete the last two posts.
The show_ass function is ambiguous.
Regards
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:mad:.
I request to moderator to delete the last four posts including my this post also.
Re: Numerous functions with similar names/actions
Quote:
Originally Posted by
marcel
I think a moderator will delete the last two posts.
Why should we? :)
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.
Re: Numerous functions with similar names/actions
Quote:
Originally Posted by
kramed
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. :)
They have nothing to do with the original question...:)
Regards
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.:D
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:
Quote:
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); }
Re: Numerous functions with similar names/actions
Quote:
Originally Posted by
kramed
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:
Code:
setupUi(this);
ass_labels[0] = ass_label1;
ass_labels[1] = ass_label2;
...
Quote:
Is there anyway to determine the exact line that the program is failing on, gdb's output was:
Try command "bt" aka backtrace.
Re: Numerous functions with similar names/actions
Thank you very much for the assistance, you have been a great help indeed.