Results 1 to 5 of 5

Thread: multiple questions (global variables and setText() for QLabel)

  1. #1
    Join Date
    Jul 2010
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default multiple questions (global variables and setText() for QLabel)

    from "mainScreen.h"
    Qt Code:
    1. #ifndef __MAIN_H__
    2. #define __MAIN_H__
    3. #include <QMainWindow>
    4. #include <QApplication>
    5. #include <QPushButton>
    6. #include <QDialog>
    7. #include <cstdlib>
    8. #include "main.cpp"
    9.  
    10. // connects the code to the UI
    11. class mainScreen : public :: QDialog, private Ui::mainScreenDLG
    12. {
    13. Q_OBJECT
    14.  
    15. public:
    16. mainScreen(QDialog *parent = 0);
    17. };
    18.  
    19.  
    20. #endif
    To copy to clipboard, switch view to plain text mode 

    from "mainScreen.cpp"

    Qt Code:
    1. #include <QApplication>
    2. #include <QDataStream>
    3. #include <QtGui>
    4. #include <cstdlib>
    5. #include <ctime>
    6. #include "ui_mainScreen.h"
    7. #include "mainScreen.h"
    8.  
    9.  
    10.  
    11.  
    12. // code to begin the programme
    13. mainScreen::mainScreen(QDialog *parent)
    14.  
    15. {
    16. setupUi(this);
    17.  
    18.  
    19. // declaration of variables
    20. int operand1, operand2, divisi1, divisi2, sumAnswer, subAnswer, multiAnswer, divisiAnswer, i;
    21. bool selectSum, selectSub, selectMulti, selectDivisi;
    22.  
    23. // timer for random generator
    24. srand (time(0));
    25.  
    26. // selects which operations are being used
    27. if (selectSum = true)
    28. {
    29.  
    30. operand1 = rand()/1638 + 1;
    31. operand2 = rand()/1638 + 1;
    32. lblOperand1.text()->setText(operand1);
    33. lblOperand2.text()->setText(operand2);
    34. sumAnswer = txtAnswer.text();
    35.  
    36. }
    37. void mainScreen::on_btnCheck_clicked()
    38. {
    39. if (selectSum = true)
    40. {
    41. // if the correct answer is put in, then a message box shows this
    42. if (operand1 + operand2 == sumAnswer)
    43. {
    44. QMessageBox::about(this, "Correct!", "Correct!");
    45. }
    46.  
    47. // if the incorrect answer is put in, then a message box shows this
    48. if (operand1 + operand2 != sumAnswer)
    49. {
    50. QMessageBox::about(this, "Incorrect", "Incorrect. The correct answer is" + sumAnswer);
    51. }
    52. }
    53. };
    To copy to clipboard, switch view to plain text mode 

    i want to make the text of lblOperand1 (which is a QLabel) the value of operand1
    but i get the error
    Qt Code:
    1. 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*’
    To copy to clipboard, switch view to plain text mode 
    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:n_btnCheck_clicked()"
    but it tells me that the variables "selectSum", "operand1" etc are not declared in that scope?

    thankyou in advance!!

  2. #2
    Join Date
    Jun 2010
    Posts
    6
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: multiple questions (global variables and setText() for QLabel)

    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.

  3. #3
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: multiple questions (global variables and setText() for QLabel)

    assuming lblOperand1 is a valid pointer to your label, then you have to use
    Qt Code:
    1. lblOperand1->setText("foo");
    To copy to clipboard, switch view to plain text mode 
    with
    Qt Code:
    1. lblOperand1->text();
    To copy to clipboard, switch view to plain text mode 
    you get the current set text back, you can't mix those commands.

  4. #4
    Join Date
    Jul 2010
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: multiple questions (global variables and setText() for QLabel)

    "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

  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: multiple questions (global variables and setText() for QLabel)

    If lblOperand1 is a QLabel and not a pointer to one then:
    Qt Code:
    1. lblOperand1.setText( QString::number(operand1) );
    2. OR
    3. lblOperand1.setText( QString("%1").arg(operand1) );
    To copy to clipboard, switch view to plain text mode 

    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).
    Last edited by ChrisW67; 29th July 2010 at 06:14. Reason: updated contents

Similar Threads

  1. QDevelop - global variables & help
    By impeteperry in forum Qt-based Software
    Replies: 2
    Last Post: 9th June 2011, 23:28
  2. Qt and global variables
    By Morea in forum Qt Programming
    Replies: 11
    Last Post: 1st February 2007, 23:42
  3. QLabel::setText() how to
    By freak in forum Qt Programming
    Replies: 1
    Last Post: 30th October 2006, 17:19
  4. Global variables
    By Mariane in forum Newbie
    Replies: 14
    Last Post: 10th October 2006, 17:23
  5. declaration of global variables???
    By pranav_kavi in forum Newbie
    Replies: 6
    Last Post: 31st January 2006, 19:56

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.