Results 1 to 3 of 3

Thread: Problem with scope of variables

  1. #1
    Join Date
    Jun 2016
    Posts
    4
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Problem with scope of variables

    In the following code the compiler says that the variable "currentRow" is unused in "on_pushButton_2_clicked()" and not declared in "on_pushButton_clicked()". I've tried declaring "int currentRow" in every place and in every way I can think of and nothing works. Seems like a trivial issue, but I'm stumped.
    Qt Code:
    1. void Dialog::on_pushButton_2_clicked()
    2. {
    3. // declare and set variables
    4. QMessageBox::information(this, "Settings","Setting Row and Column");
    5. int currentRow = 3;
    6. int currentCol = 5;
    7. }
    8.  
    9. void Dialog::on_pushButton_clicked()
    10. {
    11. // view variable values
    12. QString nmbr = QString::number(currentRow);
    13. QMessageBox::information(this,"info","currentRow = " + nmbr);
    14. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by anda_skoa; 6th June 2016 at 18:48. Reason: missing [code] tags

  2. #2
    Join Date
    Dec 2011
    Location
    Living in Spain 100km North of Valencia
    Posts
    11
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problem with scope of variables

    Hi JustBobC
    The variables currentRow and currentCol in on_pushButton_2_clicked function are 'private' to that routine only. The function on_pushButton_clicked does not know about them.
    Declare them in your header file private section.
    G

  3. #3
    Join Date
    Jun 2016
    Posts
    4
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: Problem with scope of variables

    Thanks so much GrahamB. For some reason I thought they needed to be public.

    I've got lots of experience with VB and Lisp but C++ is totally new to me. I've been developing a number of apps in VBA for Microsoft Access (some hardly using SQL features at all), and I'm really excited to turn them into stand alone apps, not least to be free of Access which seems to make trouble every time they upgrade it.

Similar Threads

  1. C++ Scope Issue / Stray Member Variables
    By 0backbone0 in forum General Programming
    Replies: 5
    Last Post: 23rd August 2015, 13:20
  2. Another scope problem!
    By mikea in forum Newbie
    Replies: 5
    Last Post: 5th October 2014, 07:25
  3. Scope problem maybe?
    By Nefastious in forum Newbie
    Replies: 6
    Last Post: 16th September 2009, 23:00
  4. QStringList scope problem
    By ht1 in forum Qt Programming
    Replies: 5
    Last Post: 30th November 2007, 19:44
  5. QT4 scope problem
    By the_bis in forum Newbie
    Replies: 5
    Last Post: 29th January 2007, 23:01

Tags for this Thread

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.