PDA

View Full Version : Numerous functions with similar names/actions



kramed
14th August 2007, 03:57
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:


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.

jpn
14th August 2007, 06:25
This is how you could do it with arrays:


class MainWindow
{
...
private:
QLabel* ass_labels[8];
QSpinBox* ass_grade_SpinBoxes[8];
QSpinBox* ass_weight_SpinBoxes[8];
};

void MainWindow::show_ass(int i)
{
ass_labels[i]->show();
ass_grade_SpinBoxes[i]->show();
ass_weight_SpinBoxes[i]->show();
}

marcel
14th August 2007, 07:03
Lots of asses in there...

rajesh
14th August 2007, 07:17
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

marcel
14th August 2007, 07:52
:)
I think a moderator will delete the last two posts.
The show_ass function is ambiguous.

Regards

rajesh
14th August 2007, 08:32
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.

wysota
14th August 2007, 12:26
I think a moderator will delete the last two posts.
Why should we? :)

kramed
14th August 2007, 14:00
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.

marcel
14th August 2007, 14:17
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 http://www.qtcentre.org/forum/images/buttons/viewpost.gif (http://www.qtcentre.org/forum/f-newbie-4/t-numerous-functions-with-similar-namesactions-8551-post45726.html#post45726)
I think a moderator will delete the last two posts.

Why should we? :)

They have nothing to do with the original question...:)


Regards

rajesh
14th August 2007, 14:27
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

kramed
14th August 2007, 16:52
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); }

jpn
14th August 2007, 17:27
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:


setupUi(this);
ass_labels[0] = ass_label1;
ass_labels[1] = ass_label2;
...




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

kramed
14th August 2007, 17:54
Thank you very much for the assistance, you have been a great help indeed.