PDA

View Full Version : Need Help With Button



InterFiction
18th November 2011, 15:59
Creating a simple program with tab widget. User checks boxes that pertain to them, and after pressing button on the calculate tab it measures their good, and evil. Just a simple program to learn some QT.

right now the button isn't doing what I'd like it to do. When it's pressed all my dialogues come up, instead of the ones that should come up. I have a dialogue for if good == evil, and a dialog for if nothing is checked, and a dialogue for if some of the suicidal boxes are checked, and will implement one for if evil is greater than good, and if good is greater than evil. simple program, but I'm not sure about what I'm doing, lol. Here's the code for the button.


void MainWindow::on_CalcGoodEvil_clicked()
{
// Initialises Variables To Handle Good and Evil Values.

int Good = 0;
int Evil = 0;

// Handled Good Traits tab

if (ui->chkKind->isChecked()){
Good += 1;}
if (ui->chkCaring->isChecked()){
Good += 1;}
if (ui->chkSharing->isChecked()){
Good += 1;}
if (ui->chkSharing->isChecked()){
Good += 1;}
if (ui->chkLoving->isChecked()){
Good += 1;}
if (ui->chkHelpful->isChecked()){
Good += 1;}
if (ui->chkDoRight->isChecked()){
Good += 1;}
if (ui->chkObeyTruth->isChecked()){
Good +=1;}
if (ui->chkDonate->isChecked()){
Good += 1;}
if (ui->chkGiveToHomeless->isChecked()){
Good += 1;}
if (ui->chkObedient->isChecked()){
Good += 1;}
if (ui->chkCompassionate->isChecked()){
Good += 1;}
if (ui->chkWontSteerWrong->isChecked()){
Good += 1;}
if (ui->chkWontLie->isChecked()){
Good += 1;}
if (ui->chkNeverStole->isChecked()){
Good += 1;}
if (ui->chkDontLitter->isChecked()){
Good += 1;}
if (ui->chkHelpInNeed->isChecked()){
Good += 1;}
if (ui->chkSolveWorldHunger->isChecked()){
Good += 1;}
if (ui->chkCarAboutFriends->isChecked()){
Good += 1;}
if (ui->chkHumble->isChecked()){
Good += 1;}
if (ui->chkBlissful->isChecked()){
Good += 1;}
if (ui->chkContent->isChecked()){
Good += 1;}
if (ui->chkOptimistic->isChecked()){
Good += 1;}
if (ui->chkStrong->isChecked()){
Good += 1;}
if (ui->chkPatient->isChecked()){
Good += 1;}
if (ui->chkPure->isChecked()){
Good += 1;}
if (ui->chkNeeded->isChecked()){
Good += 1;}
if (ui->chkImSmart->isChecked()){
Good += 1;}
if (ui->chkWise->isChecked()){
Good += 1;}
if (ui->chkResilient->isChecked()){
Good += 1;}
if (ui->chkOutGoing->isChecked()){
Good += 1;}
if (ui->chkSerene->isChecked()){
Good += 1;}
if (ui->chkIntuitive->isChecked()){
Good += 1;}
if (ui->chkIdealistic->isChecked()){
Good += 1;}
if (ui->chkConfident->isChecked()){
Good += 1;}
if (ui->chkHappy->isChecked()){
Good += 1;}
if (ui->chkAttentetive->isChecked()){
Good += 1;}
if (ui->chkAchieving->isChecked()){
Good += 1;}

// Hadnles Evil Traits Tab

if (ui->chkLoveNightmares->isChecked()){
Evil += 1;}
if (ui->chkImJelous->isChecked()){
Evil += 1;}
if (ui->chkImEnvious->isChecked()){
Evil += 1;}
if (ui->chkIGetAngry->isChecked()){
Evil += 1;}
if (ui->chkEnjoyRage->isChecked()){
Evil += 1;}
if (ui->chkMakeFunOfPeople->isChecked()){
Evil += 1;}
if (ui->chkISteal->isChecked()){
Evil += 1;}
if (ui->chkICheat->isChecked()){
Evil += 1;}
if (ui->chkIRob->isChecked()){
Evil += 1;}
if (ui->chkISwindle->isChecked()){
Evil += 1;}
if (ui->chkLieAlot->isChecked()){
Evil += 1;}
if (ui->chkImALiar->isChecked()){
Evil += 1;}
if (ui->chkImGreedy->isChecked()){
Evil += 1;}
if (ui->chkImBelligerent->isChecked()){
Evil += 1;}
if (ui->chkImAFool->isChecked()){
Evil += 1;}
if (ui->chkSelfish->isChecked()){
Evil += 1;}
if (ui->chkBackBitter->isChecked()){
Evil += 1;}
if (ui->chkIgnorant->isChecked()){
Evil += 1;}
if (ui->chkCheap->isChecked()){
Evil += 1;}
if (ui->chkNotFogiving->isChecked()){
Evil += 1;}
if (ui->chkBitter->isChecked()){
Evil+= 1;}
if (ui->chkBalckDescribesMe->isChecked()){
Evil += 1;}
if (ui->chkImViolent->isChecked()){
Evil += 1;}
if (ui->chkImMurderous->isChecked()){
Evil += 1;}
if (ui->chkImAMooch->isChecked()){
Evil += 1;}
if (ui->chkFoolish->isChecked()){
Evil += 1;}
if (ui->chkUnwise->isChecked()){
Evil += 1;}
if (ui->chkNotCaring->isChecked()){
Evil += 1;}
if (ui->chkTief->isChecked()){
Evil += 1;}
if (ui->chkCowering->isChecked()){
Evil += 1;}
if (ui->chkSpiteful->isChecked()){
Evil += 1;}
if (ui->chkUnGreatful->isChecked()){
Evil += 1;}
if (ui->chkLazy->isChecked()){
Evil += 1;}
if (ui->chkVain->isChecked()){
Evil += 1;}
if (ui->chkThoughless->isChecked()){
Evil += 1;}
if (ui->chkRebellious->isChecked()){
Evil += 1;}
if (ui->chkWasteful->isChecked()){
Evil += 1;}

///that concludes all the tabs. Now Comes The Logic for that mathmatical side of things.

if (ui->chkSuicidal->isChecked() || ui->chkThinkAboutDying || ui->chkTryToDie->isChecked()
|| ui->chkTryToDie->isChecked() || ui->chkWouldLikeToDie->isChecked() || ui->chkWillKillMyself->isChecked()){
QMessageBox::warning(this,"Warning!","I see that you've clicked one of the many suicidal check boxes under the life tab. If you've checked these with all seriousness in mind you might want to consult someone about any life issues that are giving you trouble. Remember, it's not all that bad :");
}

//Handles if the user hasn't clicked anything.

if (Good == 0 && Evil == 0 ){
QMessageBox::warning(this,"Alert","You haven't clicked any of, the choices!"); }


//Handles if good and evil is equal

if (Good == Evil){
QMessageBox::information(this,"Findings","You have a perfect balance of good, and evil! You may want to think about the bad things in your life, and how you can get rid of them. As far as the good things go Keep Up The Good Work!");
}

InterFiction
19th November 2011, 19:55
Still can't find the error here. I've tried a couple of forums, but not coming up with any information. Does anybody know what went wrong in my logic?

norobro
19th November 2011, 20:37
You left out an "isChecked()" for ui->chkThinkAboutDying in line 66 so that statement always evaluates to true and if Good==0 and Evil==0 they are equal so the next two if statements evaluate to true.

InterFiction
19th November 2011, 20:46
Awesome, thanks, how I missed that I do not know, lol.

norobro
19th November 2011, 21:43
Here's a bit of code that will save typing all of those "if" statements:
foreach(QCheckBox *item, ui->Good_tab->findChildren<QCheckBox *>())
if(item->isChecked()) ++Good;
Then do the same for the Evil tab and eliminate the ifs.

InterFiction
20th November 2011, 00:37
Man, I wish I knew how to write code like that. I'm still learning the basics though. I have a copy of C++ primer. I'm learning quite a bit from it. Just getting into pointers, and allocating memory with new keyword..


foreach(QCheckBox *item, ui->Good_tab->findChildren<QCheckBox *>())
if(item->isChecked()) ++Good;

some of that I can understand.

parts I don't get are...


QCheckBox *item

this is creating a pointer of the QCheckBox typ? hahaha.

and


findChildren<QCheckBox *>

that just looks confusing. findchildren less than QCheckBox pointer greater than? lol

can you break those parts down a little for me?

norobro
20th November 2011, 01:37
Sorry, I didn't mean to overwhelm you with new stuff. Its just that I kept thinking about how long it must have taken to key in all of those ifs.

That statement is probably more clear written like this:
QList<QCheckBox *> listOfPtrs = ui->Good_tab->findChildren<QCheckBox *>();
QCheckBox *chkBoxPtr;
foreach(chkBoxPtr, listOfPtrs) if(chkBoxPtr->isChecked() ++Good; The first statement generates a list of pointers to QCheckBox(es) on the "Good_tab" of your QTabWidget. See here (http://doc.qt.nokia.com/latest/qobject.html#findChildren).
The foreach (link (http://doc.qt.nokia.com/latest/containers.html#the-foreach-keyword)) statement iterates through the list (for each pointer in a list of pointers) and checks to see if a checkbox is checked and if so it increments the variable Good.

This is IMO a really good C++ tutorial site with lots of example code: www.learncpp.com


Man, I wish I knew how to write code like that
It's pretty basic Qt code. Keep plugging and you'll be there before you know it. :D