Qt: Connecting SIGNAL to SLOT in 2 different windows - visual-studio-2010

I'm going nuts trying to find the problem here. I have a main window, and a form type window made in Qt. I'm using the Visual Studio 2010 addon. For some reason, my SLOT is never called in the main window; however, the signal appears to be emitted.
Here's what I've done:
This is the form:
class ScalerValuesWindow : public QWidget
{
Q_OBJECT
private:
Ui::ScalerValuesWindow ui;
// Variables
std::vector<int> scalerValues;
public slots:
void storeScalerValues();
signals:
void ScalerValues(std::vector<int>);
public:
ScalerValuesWindow(QWidget *parent = 0);
};
void ScalerValuesWindow::storeScalerValues()
{
emit ScalerValues(scalerValues);
hide();
}
Then here's my main window connection line in my constructor:
scalerValuesWindow = new ScalerValuesWindow;
connect(scalerValuesWindow, SIGNAL(ScalerValues(std::vector<int>)), this, SLOT(RetrieveScalerValues(std::vector<int>)));
This is in my main window's class declaration:
public slots:
void RetrieveScalerValues(vector<int> scalerValues);
And this is the slot:
void RelayduinoGuiThreading::RetrieveScalerValues(vector<int> scalerVals)
{
scalerValues = scalerVals;
}
I have Q_OBJECT declared in both. I have no idea what could be causing this.
Any advice would be greatly appreciated.

You must define your slot as:
public slots:
void RetrieveScalerValues(std::vector<int> scalerValues);
^^^
(Inspect the generated moc file to see exactly what signal/slot signatures are being generated.)
Don't use using namespace std; in your headers, that's bad practice anyway (pulls in that huge namespace into all the users of that header, which is impolite).

Related

C++11 Call virtual member function

I`m trying to implement something like this using C++11.
class Proto{
public:
virtual void fu() = 0;
};
class Impl: public Proto{
public:
void fu();
};
void Impl::fu(){
LOG_INFO("im fu");
}
class Inv{
public:
void useFu(void (Proto::*)());
};
void Inv::useFu(void (Proto::*fu)()){
//fu();
};
void main(){
Impl impl;
Inv inv;
//inv.useFu(impl.fu);
}
useFu(void (Proto::*)()) must be declared in this way because, fu() uses some specific to Proto functionality's
I have two places were things going wrong.
First is fu() call itself and second how to pass fu as parameter inv.useFu(impl.fu).
Edit after bipll answer
The suggested usage of inv.useFu() solves the second problem of my question.
inv.useFu(static_cast<void (Proto::*)(void)>(&Impl::fu));
But I still need to call fu as a pointer to member function;
The way your useFu is declared now, it should be called as
inv.useFu(static_cast<void (Proto::*)(void)>(&Impl::fu));
But I guess that's not what you wanted. It should rather be
template<class F> void useFu(F &&f) { std::invoke(std::forward<F>(f)); }
or simply
void useFu(std::function<void()> f) { std::invoke(std::move(f)); }
and called as
useFu([&]{ impl.fu(); });
(Rather than using a lambda you can bind the method to the object with std::bind in the latter call but almost nobody ever does that.)

wxTimer not calling overriden Notify()

I'm running into an issue where I implemented a derived wxTimer class to override the Notify() call since I'm not using an owner implementation as described in the documentation.
When I debug the run, I can see
the timer is being instantiated
my_timer_instance->IsRunning() returns true
MyTimer::Notify() is never called
This leads me to believe that the timer is being set and running, but when it expires it's calling the base class Notify() procedure and not my override it's not calling notify() but I'm not sure why.
EDIT: I added frame->getTimer()->Notify(); to my app and the correct procedure was called. Therefore, the timer just isn't calling Notify when it expires.
EDIT2: Added this minimal working example, and the timer works as expected. I'll try to compare the two and see what the problem is.
MyApp.hpp
#pragma once
#ifndef __NONAME_H__
#define __NONAME_H__
#include <wx/artprov.h>
#include <wx/xrc/xmlres.h>
#include <wx/statusbr.h>
#include <wx/gdicmn.h>
#include <wx/font.h>
#include <wx/colour.h>
#include <wx/settings.h>
#include <wx/string.h>
#include <wx/frame.h>
#include <wx/timer.h>
///////////////////////////////////////////////////////////////////////////
class MyTimerClass : public wxTimer
{
wxFrame* MyFrame;
public:
MyTimerClass(wxFrame* frame): MyFrame(frame) {};
void Notify() override;
};
///////////////////////////////////////////////////////////////////////////////
/// Class MyFrame1
///////////////////////////////////////////////////////////////////////////////
class MyFrame1 : public wxFrame
{
private:
protected:
wxStatusBar* m_statusBar1;
MyTimerClass* MyTimer;
public:
void StartTimer(int TimeInSeconds);
MyFrame1(wxWindow* parent, wxWindowID id = wxID_ANY, const wxString& title = wxEmptyString, const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxSize(500, 300), long style = wxDEFAULT_FRAME_STYLE | wxTAB_TRAVERSAL);
~MyFrame1();
};
#endif //__NONAME_H__
MyApp.cpp
#include "MyApp.hpp"
#include "wx/wxprec.h"
// for all others, include the necessary headers (this file is usually all you
// need because it includes almost all "standard" wxWidgets headers)
#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif
///////////////////////////////////////////////////////////////////////////
void MyTimerClass::Notify()
{
MyFrame->SetStatusText("Timer popped", 0);
}
MyFrame1::MyFrame1(wxWindow* parent, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size, long style) : wxFrame(parent, id, title, pos, size, style)
{
MyTimer = new MyTimerClass(this);
this->SetSizeHints(wxDefaultSize, wxDefaultSize);
m_statusBar1 = this->CreateStatusBar(1, wxSTB_SIZEGRIP, wxID_ANY);
this->Centre(wxBOTH);
this->StartTimer(5);
}
void MyFrame1::StartTimer(int TimeInSeconds)
{
SetStatusText("Timer started with " + std::to_string(TimeInSeconds) + " seconds.");
MyTimer->Start(TimeInSeconds * 1000);
}
MyFrame1::~MyFrame1()
{
}
// ----------------------------------------------------------------------------
// resources
// ----------------------------------------------------------------------------
// the application icon (under Windows it is in resources and even
// though we could still include the XPM here it would be unused)
#ifndef wxHAS_IMAGES_IN_RESOURCES
#include "../sample.xpm"
#endif
// ----------------------------------------------------------------------------
// private classes
// ----------------------------------------------------------------------------
class MyApp : public wxApp
{
public:
virtual bool OnInit() wxOVERRIDE;
};
enum
{
// menu items
Minimal_Quit = wxID_EXIT,
Minimal_About = wxID_ABOUT
};
wxIMPLEMENT_APP(MyApp);
bool MyApp::OnInit()
{
// call the base class initialization method, currently it only parses a
// few common command-line options but it could be do more in the future
if (!wxApp::OnInit())
return false;
// create the main application window
MyFrame1 *frame = new MyFrame1(NULL, -1, "Test Frame");
frame->Show(true);
return true;
}
#BobbyTables,
From the documentation:
This member should be overridden by the user if the default
constructor was used and SetOwner() wasn't called.
Is it the case?
Nothing seems to be wrong in the code you show (although I'd change a few things, such as using raw pointer for my_timer_instance), so the problem must be elsewhere. As usual, the best would be to come up with a SSCCE, without it I can only offer some guesses as to what the problem actually is.
Are you running the event loop? The timers will only fire when it's running, so if you block doing some computation, this wouldn't happen.
Also, what is frame in Notify()? Is this a global (I'd rather pass it as parameter to MyTimer ctor)?
So after mimicking the code provided in the question, the following changes were made:
Instead of using a getter and setter to access the private timer member, I instead use
void refreshTimer(int time_in_seconds) in my parent frame class and create the timer in the parent frame's constructor rather than letting the app create it and pass it in.
I don't see why either of those two things would change the behavior of the timer but the timer now works as expected. I apologize for not being able to identify a concrete bug as the source of the problem.
NOTE: This behavior was caused by the timer being invoked outside the wxwindow's thread. Be careful when creating multithreaded programs using wxwidgets as a GUI. To circumvent this issue since I needed the timer to be invoked in a different thread, I created my own timer class that works correctly.

No call to CWinThread:ExitInstance

In my dialog based application, in CWinApp::InitInstance I create class that has framed window v1:
creating modHolder
CWinThread* pThread= AfxBeginThread(RUNTIME_CLASS(modHolder));
declaring modHolder
class modHolder : public CWinThread
{
DECLARE_DYNCREATE(modHolder)
protected:
modHolder(); // protected constructor used by dynamic creation
virtual ~modHolder();
public:
CMainWindow * v1;
virtual BOOL InitInstance();
virtual int ExitInstance();
protected:
DECLARE_MESSAGE_MAP()
};
During application close I expect to get call to modHolder::ExitInstance()
Unfortunately I have no this function. What might be problem? I think, that CWinThread modHolder should terminate automatically. Am I wrong?
CWinThread::ExitInstance is called when CWinThread::Run finishes.
You have the source code!

A delegate to a virtual method where does it point to (base/derived)?

I recently started using C++/Cli for wrapping purposes.
Now I'm at a point where I've to know more about the internals.
Consider the following code:
Header file (ignoring .NET namespaces for this example):
public ref class BaseyClass
{
protected:
delegate void TestMethodDelegate(); // TestMethod delegate
BaseyClass(); // constructor
virtual void TestMethod(); // member: method
GCHandle _testMethodHandle; // member: method handle
};
CPP file (ignoring .NET namespaces for this example):
BaseyClass::BaseyClass()
{
_testMethodHandle
= GCHandle::Alloc(
gcnew TestMethodDelegate(this, &BaseyClass::TestMethod));
}
void TestMethod()
{
}
Eventually this class will be used as base class (for a DerivedClass) later and the method "TestMethod()" gets overridden and called from unmanaged code through the delegate pointer.
Now the question: Which method will be referenced by the delegate?
BaseyClass::TestMethod();
or
DerivedClass::TestMethod();
Personally I think the "BaseyClass::TestMethod()" will be referenced by the delegate because even when it's overridden, the delegate points to the (base-)address of BaseyClass. Hence a DerivedClass cannot override the "TestMethod" and use the delegate from BaseyClass.
I just want to be sure. Thanks for your comments and enlightment.
The delegate will be a reference to the derived class's TestMethod. Even though you're passing &BaseyClass::TestMethod, that's a virtual method, you're also passing this, which is the derived type, and both of those are taken into account when the delegate is created.
Other notes:
TestMethodDelegate doesn't need to be inside the class definition. The more standard way is to have the delegate outside of the class, just in the namespace. (Or use the existing built-in one, Action.)
You don't need to GCHandle::Alloc (I assume that's what you meant by Allow). Instead, declare _testMethodHandle as TestMethodDelegate^ (or Action^). In general, you shouldn't need to deal with GCHandle unless you're interfacing with unmanaged code, and this code is all managed.
Here's my test code:
public ref class BaseyClass
{
public:
BaseyClass() { this->_testMethodHandle = gcnew Action(this, &BaseyClass::TestMethod); }
virtual void TestMethod() { Debug::WriteLine("BaseyClass::TestMethod"); }
Action^ _testMethodHandle;
};
public ref class DerivedClass : BaseyClass
{
public:
virtual void TestMethod() override { Debug::WriteLine("DerivedClass::TestMethod"); }
};
int main(array<System::String ^> ^args)
{
BaseyClass^ base = gcnew DerivedClass();
base->_testMethodHandle();
return 0;
}
Output:
DerivedClass::TestMethod

Visual Studio 2010 debugger doesn't show values when using multiple inheritance

In my application I have a class for which I need to keep a set of interfaces delivered by the application.
Since the class itself doesn't know all the possible interfaces, it can't simply keep one data member for every interface. Instead, it should keep a vector of interfaces.
In practice this means that I define one 'generic' interface (IGeneric) of which all the more specific interfaces inherit. That way, I can easily keep a vector with all the interfaces (in the code hereafter I replaced the actual interface names by some dummy names; in my application the names are much more meaningful):
#include <vector>
#include <windows.h>
class IGeneric
{
public:
virtual ~IGeneric() {}
};
class IFirst : public IGeneric {public: virtual void one() = 0;};
class ISecond : public IGeneric {public: virtual void two() = 0;};
class IThird : public IGeneric {public: virtual void three() = 0;};
class IFourth : public IGeneric {public: virtual void four() = 0;};
typedef std::vector<IGeneric *> GenericContainer;
To install the interfaces, other modules (which really know all the interfaces), provide functions to install the interfaces. They could be something like this:
void installFirst (GenericContainer &bc, IFirst &b) { bc.push_back(&b); }
void installSecond (GenericContainer &bc, ISecond &b) { bc.push_back(&b); }
void installThird (GenericContainer &bc, IThird &b) { bc.push_back(&b); }
void installFourth (GenericContainer &bc, IFourth &b) { bc.push_back(&b); }
In practive, applications will write classes that implement multiple of these interfaces, like this:
class DoAll : public IFirst, public ISecond, public IThird, public IFourth
{
public:
virtual void one() {}
virtual void two() {}
virtual void three() {}
virtual void four() {}
DoAll(int i) : m_i(i) {}
private:
int m_i;
};
Installing this implementation is now really easy. This is the main routine from my example:
int main()
{
GenericContainer gc;
DoAll all (123);
installFirst(gc,all);
installSecond(gc,all);
installThird(gc,all);
installFourth(gc,all);
DebugBreak();
}
At the breakpoint, I now investigate the contents of the vector in the debugger. It looks like this:
As you can see, it shows that the implementation of the interfaces is DoAll (in all 4 cases), and it even shows as which interface (IFirst, ISecond, ...) DoAll has been installed in the vector.
Problem is that the debugger doesn't seem to be able to show the contents of the most derived class (DoAll). It show the datamember m_i, but it is unable to show the value of m_i.
Only if I explicitly take the first interface address, and cast it to "DoAll *", I can correctly see the contents:
I compile and link using the following commands:
cl /c /EHsc /GR /Zi /Od test.cpp
link /debug test.obj
Is this a bug in Visual Studio 2010 or am I missing something?
EDIT:
If I use single inheritance, like in this class:
class DoFirst : public IFirst
{
public:
virtual void one() {}
DoFirst(int i) : m_i(i) {}
private:
int m_i;
};
And install it in the vector like this:
DoFirst f (456);
installFirst (gc,f);
The debugger shows me the correct values and it also immediately shows the most derived class:
In vector you are holding pointers to IGeneric. IGeneric does not have any clue about m_i, which is member of DoAll. We might even say that there actually is a bug, because the debugger is showing {m_i = ???}, but it should not.

Resources