Results 1 to 14 of 14

Thread: Problem with variable in main.cpp and mainwindow.cpp

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1

    Default Re: Problem with variable in main.cpp and mainwindow.cpp

    I made in private, like you, but still isn´t work. While I use a method m.zapisz(51), value 51 is in bit2, or m.bit2? Because if in m.bit2, mainwindow.cpp can´t see this value :/

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Problem with variable in main.cpp and mainwindow.cpp

    As a general note, don't define static variables in header files, as each .cpp file which includes the header will get its own copy of the variable.
    Typically you only declare the variable in header:
    Qt Code:
    1. // header.h
    2. extern int my_var;
    To copy to clipboard, switch view to plain text mode 
    and define it in exactly one .cpp file:
    Qt Code:
    1. // impl.cpp
    2. #include "header.h"
    3.  
    4. int my_var = 0;
    To copy to clipboard, switch view to plain text mode 
    In your case I suggest not using the global variable at all anyway.

  3. #3

    Default Re: Problem with variable in main.cpp and mainwindow.cpp

    Hi, Iḿ not sure I understand you.
    I should write extern int bit in private section in mainwindow.h?
    And then, where I should write this:

    // impl.cpp
    #include "header.h"

    int my_var = 0;


    impl.cpp is my main.cpp or mainwindow.cpp? and int my_var is my bit?

  4. #4
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Problem with variable in main.cpp and mainwindow.cpp

    I should write extern int bit in private section in mainwindow.h?
    No no, just do that as @jefftee suggested - remove the static variable completely. Use only member variable in the MainWindow class.
    I mentioned the "extern" keyword as a sidenote, so that you know it for future.

  5. #5

    Default Re: Problem with variable in main.cpp and mainwindow.cpp

    Ok, i did it how @jefftee show, but still doesn´t work how I want :/ In main.cpp i have w.zapisz(51), so bit should have value 51, but in mainwindow.cpp bit don´t have a value :/

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Problem with variable in main.cpp and mainwindow.cpp

    Are you sure you are reading the variable after your wrote to it?

    Cheers,
    _

  7. #7
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Problem with variable in main.cpp and mainwindow.cpp

    Repost your current code for all of main.cpp, MainWindow.cpp, and MainWindow.h.
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

Similar Threads

  1. Refresh the mainwindow / functions called in the main loop
    By valerianst in forum Qt Programming
    Replies: 7
    Last Post: 2nd October 2013, 14:57
  2. how can dialog access variable from parent MainWindow?
    By krezix in forum Qt Programming
    Replies: 5
    Last Post: 30th April 2012, 00:20
  3. Replies: 1
    Last Post: 20th January 2012, 04:39
  4. Replies: 3
    Last Post: 12th April 2011, 10:58
  5. main.cpp variable access question
    By MarkoSan in forum Qt Programming
    Replies: 10
    Last Post: 10th March 2008, 20:48

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
  •  
Qt is a trademark of The Qt Company.