PDA

View Full Version : multiple questions (global variables and setText() for QLabel)



Baxter
28th July 2010, 11:33
from "mainScreen.h"

#ifndef __MAIN_H__
#define __MAIN_H__
#include <QMainWindow>
#include <QApplication>
#include <QPushButton>
#include <QDialog>
#include <cstdlib>
#include "main.cpp"

// connects the code to the UI
class mainScreen : public :: QDialog, private Ui::mainScreenDLG
{
Q_OBJECT

public:
mainScreen(QDialog *parent = 0);
};


#endif

from "mainScreen.cpp"


#include <QApplication>
#include <QDataStream>
#include <QtGui>
#include <cstdlib>
#include <ctime>
#include "ui_mainScreen.h"
#include "mainScreen.h"




// code to begin the programme
mainScreen::mainScreen(QDialog *parent)

{
setupUi(this);


// declaration of variables
int operand1, operand2, divisi1, divisi2, sumAnswer, subAnswer, multiAnswer, divisiAnswer, i;
bool selectSum, selectSub, selectMulti, selectDivisi;

// timer for random generator
srand (time(0));

// selects which operations are being used
if (selectSum = true)
{

operand1 = rand()/1638 + 1;
operand2 = rand()/1638 + 1;
lblOperand1.text()->setText(operand1);
lblOperand2.text()->setText(operand2);
sumAnswer = txtAnswer.text();

}
void mainScreen::on_btnCheck_clicked()
{
if (selectSum = true)
{
// if the correct answer is put in, then a message box shows this
if (operand1 + operand2 == sumAnswer)
{
QMessageBox::about(this, "Correct!", "Correct!");
}

// if the incorrect answer is put in, then a message box shows this
if (operand1 + operand2 != sumAnswer)
{
QMessageBox::about(this, "Incorrect", "Incorrect. The correct answer is" + sumAnswer);
}
}
};

i want to make the text of lblOperand1 (which is a QLabel) the value of operand1
but i get the error
src/mainScreen.cpp:32: error: request for member ‘text’ in ‘((mainScreen*)this)->mainScreen::<anonymous>.Ui::mainScreenDLG::<anonymous>.Ui_mainScreenDLG::lblOperand1’, which is of non-class type ‘QLabel*’
i checked and made sure that the label was named and referenced to correctly

i also want to be able to use the "operand1" variable in "void mainScreen::on_btnCheck_clicked()"
but it tells me that the variables "selectSum", "operand1" etc are not declared in that scope?

thankyou in advance!!

Qtbl
28th July 2010, 14:04
Maybe "lblOperand1" is a pointer to a QLabel. Also try "lblOperand1.setText(QString("%1").arg(operand1));"

"operand1" variable is a local variable not a class member.

Lykurg
28th July 2010, 14:46
assuming lblOperand1 is a valid pointer to your label, then you have to use
lblOperand1->setText("foo"); with
lblOperand1->text();you get the current set text back, you can't mix those commands.

Baxter
29th July 2010, 04:08
"lblOperand1" is a QLabel, and I want its text value to be the contents of "operand1", an int
I also need "operand1" to be a global variable, so that i may reference it when "btnCheck" is clicked

ChrisW67
29th July 2010, 05:07
If lblOperand1 is a QLabel and not a pointer to one then:


lblOperand1.setText( QString::number(operand1) );
OR
lblOperand1.setText( QString("%1").arg(operand1) );


The mainScreen.cpp you have posted is mangled. It's not clear where your "global" variables are being declared. Are the existing declarations and the following code in the constructor? It certainly doesn't look like it belongs there. Your "global" variables should be class members if possible: they currently appear to be local variables in the constructor (as already pointed out).