Urbi
Urbi

Reputation: 1

QT/C++ - How to forward the status of a bool from mainwindow.cpp to a different class?

I'm a beginner to both C++ and Qt, and I know my question is trivial. But I somehow don't understand how to connect SIGNAL and SLOT when they belong to two different classes. I have in mainwindow.ui a QAction object "actionDebug_mode" which sets in mainwindow.cpp among others "m_debugMode = true".

void MainWindow::on_actionDebug_mode ()
{
    m_debugMode = true;
}

Now I need the bool "m_debugMode" also in "Logging.cpp" to switch something else there.

void Logging::method_ABC ()
{
if (m_debugMode) { ... }
}

So in the end I only need to get the state of "m_debugMode" (true/false) transferred from mainwindow.cpp to Logging.cpp. What is the easiest way to realize this? Do I also need to create a method in Logging.cpp so that I can connect void MainWindow::on_actionDebug_mode () to it? Or is there an easier way?

Which code do I have to put into mainwindow.cpp, mainwindow.h, Logging.cpp and Logging.hpp? Thanks!

Upvotes: 0

Views: 69

Answers (1)

Sergey Kozharinov
Sergey Kozharinov

Reputation: 64

Place the following in the Logging.cpp and call setDebugMode from mainwindow.cpp. Do not forget to add function signatures to Logging.hpp.

static bool g_debugMode = false;

void setDebugMode(bool value) {
  g_debugMode = value;
}

bool getDebuggingMode() {
  return g_debugMode;
}

Upvotes: 0

Related Questions