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 Problem with variable in main.cpp and mainwindow.cpp

    Hi.
    I need a help.
    I have a variable int bit2, which is a mainwindow.h.
    In main.cpp I calculate her value, and I want to send this value to mainwindow.cpp. Is this possible?
    I include part of the cod:

    mainwindow.h
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4.  
    5. #include <QMainWindow>
    6. static int bit2;
    7. namespace Ui {
    8. class MainWindow;
    9.  
    10. }
    11.  
    12. class MainWindow : public QMainWindow
    13. {
    14. Q_OBJECT
    15.  
    16.  
    17. public:
    18. explicit MainWindow(QWidget *parent = 0);
    19. ~MainWindow();
    20. int flag;
    21. void zapisz(int wartosc);
    22.  
    23.  
    24.  
    25. private slots:
    26.  
    27.  
    28. private:
    29. Ui::MainWindow *ui;
    30.  
    31.  
    32. };
    33.  
    34. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 


    main.cpp
    Qt Code:
    1. int main(int argc, char **argv)
    2. {
    3. QApplication a(argc, argv);
    4. MainWindow w;
    5. w.show();
    6. ...
    7. ...
    8. w.zapisz(51);
    To copy to clipboard, switch view to plain text mode 

    mainwindow.cpp
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4.  
    5. MainWindow::MainWindow(QWidget *parent) :
    6. QMainWindow(parent),
    7. ui(new Ui::MainWindow)
    8. {
    9. ui->setupUi(this);
    10. QPixmap pix("/home/bananapi/wizu-build-desktop-Qt_4_8_2__System__Release/new_grn.bmp");
    11. QPixmap pix2("/home/bananapi/wizu-build-desktop-Qt_4_8_2__System__Release/new_red.bmp");
    12.  
    13. ui->label->setPixmap(pix);
    14. ui->label_2->setPixmap(pix);
    15. ui->label_3->setPixmap(pix);
    16. ui->label_4->setPixmap(pix);
    17. flag=0;
    18. int bit;
    19. bit=bit2;//TU JEST BLAD ;/
    20. ...
    21. ...
    22. void MainWindow::zapisz(int wartosc)
    23. {
    24. bit2=wartosc;
    25. }
    To copy to clipboard, switch view to plain text mode 

    When I write in mainwindow.cpp ¨bit=51¨ (value which I want calculate in main.cpp) programm is working. In my opinion bit2 in main.cpp and bit2 in mainwindow.cpp is different variable. Can someone help? There is any way to send value from main.cpp to mainwindow.cpp?

  2. #2
    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

    Don't use a global variable for this. Either pass the variable to the constructor of MainWindow or add a method to MainWindow where you can pass the variable as a function argument and store in class member variable, etc.

    Edit: I see now that you did try to add a method to set the value, but instead of declaring bit2 as a static global variable, make it a private member variable of MainWindow, then assign the value passed in your MainWindow::zapisz method.

    Qt Code:
    1. // MainWindow.h
    2. class MainWindow
    3. {
    4. ...
    5. private:
    6. int bit2;
    7. }
    8.  
    9. // MainWindow.cpp
    10. void MainWindow::zapisz(int wartosc)
    11. {
    12. bit2 = wartosc;
    13. }
    To copy to clipboard, switch view to plain text mode 

    Does that not do what you want?
    Last edited by jefftee; 10th July 2015 at 06:40.
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

  3. #3

    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 :/

  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

    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.

  5. #5

    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?

  6. #6
    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.

  7. #7

    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 :/

  8. #8
    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,
    _

  9. #9
    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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.