Important: Please read the Qt Code of Conduct -

How to call public QString that have declared in MainWindow.h from other form?

  • Hello ,
    I have declared a string in the public of mainwindow.h and initialized in mainwindow.cpp. I want to call this string from other source file which is a qwidget. When I call this string program goes to infinite loop. How can I solve this? Thank you in advance.

  • @aysenur

    You need to give some source lines of your code on how you access the string and what you are doing with it.

    Otherwise you should use the debugger and try to find the condition of your endless loop. Most likely it has nothing to with the string.

  • Thank you for your reply. I need a global string variable in MainWindow. I have declared it under the public of mainwindow.h code is:
    QString mystring;
    I have assigned the selected file name under button click to this variable. In the Mainwindow there is an object which shows the stl file. This part has another cpp file and I need to call declared string form this .cpp. Is it possible? When I add #include "mainwindow.h" header I can access mystring from this .cpp class but program goes to infinite loop.
    Code :MainWindow a;
    QFile file(a.deneme);

  • Hi,
    I would suggest different approach: declare the QString in target cpp that holds the file name (private), along with public slot that acts as a setter.
    Then, in MainWindow, connect the QPushButton::clicked(signal) (or create your own that would pass the desired value to the slot).

Log in to reply