I have two ui header - ui_FlowChartEditor.h and ui_Notepad.h
There was a problem with this codes :
//ui_FlowChartEditor.h
namespace Ui {
class FlowChartEditorClass: public Ui_FlowChartEditorClass {};
} // namespace Ui
//ui_Notepad.h
namespace Ui {
class NotepadClass: public Ui_NotepadClass {};
} // namespace Ui
'Ui' namespace was duplicated, so I solve this problem by changing name of namespace.
like this :
//ui_FlowChartEditor.h
namespace Ui_FlowChartEditor {
class FlowChartEditorClass: public Ui_FlowChartEditorClass {};
} // namespace Ui
//ui_Notepad.h
namespace Ui_Notepad {
class NotepadClass: public Ui_NotepadClass {};
} // namespace Ui
It works at Debug configuration, but not at Release configuration.
Compiler says : [error C2653: 'Ui_FlowChartEditor': is not a class or namespace name (compiling source file FlowChartEditor.cpp)], etc..
Is there any solution to solve this problem?
Thanks in advance.
With #zgyarmati's solution, I solved this problem.
I changed objectName in Qt Designer.
ui_ header file became like this:
namespace FlowChartEditorUi {
namespace Ui {
class FlowChartEditorClass : public Ui_FlowChartEditorClass {};
} // namespace Ui
} // namespace FlowChartEditorUi
Thx.
The ui_*.h files are regenerated by uic from your forms (.ui) files. You changed the generated ui_*.h files only for the debug build, but not for the release build, so you have the pristine, uic-generated files there.
You could change the files also for the release build, but
you are not supposed to change the generated files, as they will be overwritten.
To set a namespace for your ui class, when you are creating a Designer Form Class in QtCreator, specify a name with a namespace, e.g. Ui_Notepad::NotepadClass in the "Class name" field, and the generated ui_*.h file will contain that namespace.
If you already have your forms, then you can also change the "objectName" property for them accordingly in the QtCreator form editor.
Related
I have the following code:
#interface Model ()
{
//...
std::function<bool(CGFloat, CGFloat)> greaterThan;
std::function<bool(CGFloat, CGFloat)> lesserThan;
//...
}
#end
Everything worked fine in my original project.
I just created a new project, and dragged and dropped the files between the two, and in the new project I have the following errors for the above two lines:
No type named 'function' in namespace 'std'
I don't know what the problem is - The files are .mm , why can't it find function in the names space std ?
Add #include <functional> to the top of your code.
Also remember to use -std=c++0x (or equivalent) in your compiler parameters; this will enable C++11 standard which is required for std::function.
I have began making a project in Visual Studio 2012 C++/CLI .
But I have some questions about correct formatting and placing.
Firstly, where should be main code located ? Creating GUI means that it will be a lot of events(like button clicks, text inputs and so on ) . But is it correct to place code of events in header file ?
My .cpp file looks like that
#include "MyForm.h"
using namespace System;
using namespace System::Windows::Forms;
[STAThread]
void Main(array<String^>^args) {
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
MultiLevelList::MyForm form;
Application::Run(%form);
}
All events are in MyForm.h .
Also events are private , so I haven't possibility to use them in separate .cpp file.
What is correct style of writting ?
Thx in advance .
I decided to switch my hobby project application (a dictionary lookup program) to a plugin architecture to enable all kinds of different dictionaries to be developed for other languages in the future. The application is developed in Visual C++ with Qt (5.0.2). I added this header to the application code to define the interface for the dictionary plugins:
// dict_plugin.h
#ifndef DICT_PLUGIN_H
#define DICT_PLUGIN_H
#include <QtPlugin>
class PluginInterface
{
public:
virtual ~PluginInterface() {}
virtual QString language() const = 0;
virtual class QWidget* ui() const = 0;
};
Q_DECLARE_INTERFACE(PluginInterface, "pl.ksmvision.winona.PluginInterface")
#endif // DICT_PLUGIN_H
Next, I created a new project from the "Qt library" template for the plugin itself (using the Qt Visual Studio add-in) which is used to make dll's. The main header file looks like this:
#ifndef JP_PLUGIN_H
#define JP_PLUGIN_H
// created by the template to define Q_DECL_EXPORT
// and _IMPORT macros but the plugin engine takes
// care of that (I think)
//#include "jp_plugin_global.h"
#include <QObject>
#include <QtPlugin>
#include <dict_plugin.h>
class JpPlugin : public QObject, public PluginInterface
{
Q_OBJECT
Q_PLUGIN_METADATA(IID "pl.ksmvision.winona.JpPlugin")
Q_INTERFACES(PluginInterface)
public:
JpPlugin();
virtual ~JpPlugin();
virtual QString language() const;
virtual QWidget* ui() const;
};
#endif // JP_PLUGIN_H
When I try to build this, I get an error from moc on the Q_INTERFACES line specifying the interfaces my plugin is supposed to be implementing:
3>------ Build started: Project: jp_plugin, Configuration: Debug Win32 ------
3> Moc'ing jp_plugin.h...
3>F:\moje\src\cpp\winona\build\jp_plugin\jp_plugin.h(15): error : Undefined interface
========== Build: 2 succeeded, 1 failed, 2 up-to-date, 0 skipped ==========
It looks like moc'ing takes place before the dict_plugin.h file is included, because when I introduce a typo to the include filename, it doesn't complain that the file doesn't exist, just terminates the build with that same error message about the interface being undefined.
What am I doing wrong? Thanks.
The reason moc failed was because the interface declaration was unavailable. The #include directive failed because the file could not be found. Apparently, moc can process #include directives by itself, but doesn't (by default?) print an error message or halt processing if the file to be included can't be found.
The reason the header file with the interface declaration could not be found is that the custom build settings which cause moc to be invoked that are generated by the Qt VS add-in don't inherit the project's include path. I managed to add the required path manualy to moc's command line by entering the property pages of the plugin's header file, browsing to Custom Build Tool->General->Command Line and adding an extra "-I..." include option at the end. After that, moc processed the header and the build was successfull.
For those who venture down this path like I did. My issue was slightly different and had to do with namespaces. I was getting the exact same "undefined interface" error, but the path resolution had no effect for me.
I had something like the following:
namespace foo {
class Interface
{
// ...
};
} // namespace foo
Q_DECLARE_INTERFACE(foo::Interface, "my.interface/1.0")
Incorrect
namespace foo {
class Derived : public QObject, public Interface
{
Q_OBJECT
Q_INTERFACES(Interface)
};
}
Correct
namespace foo {
class Derived : public QObject, public Interface
{
Q_OBJECT
Q_INTERFACES(foo::Interface) //!< Notice foo:: is still provided
};
}
Reason
From the documentation:
If you want to use Q_DECLARE_INTERFACE with interface classes declared in a namespace then you have to make sure the Q_DECLARE_INTERFACE is not inside a namespace though.
But what they don't mention is that the Q_INTERFACES(), even when inside of that namespace by scope, still requires the namespace be provided as though it were global.
I currently have a C# class library as such
namespace Library
{
[ComVisible(true), InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("aa950e58-7c6e-4818-8fc9-adecbc7a8f14")]
public interface IQuote
{
........
}
}
I then register it using the following
RegAsm.exe C:\MyFile.dll /tlb:MyFile.tlb /codebase
Now the problem in my C++ application is that
#import "c:\\MyFile.tlb" raw_interfaces_only
using namespace Library;
and then I get the message
Error 7 error C2653: 'Library' : is not a class or namespace name
Any suggestions on how to resolve this issue ?
COM itself does not have a concept of namespace (which is language-specific).
RegAsm.exe uses the assembly name as the name of the library statement in the IDL. It does not care which namespace your COMVisible class is in.
By default the C++ compiler imports a type library into a namespace with its name specified by the library statement in the IDL. Use the assembly name as the namespace name (or rename the namespace in the import statement) in your C++ code.
Took 2 hours, to figure this out.
Sample code is here:
#import "path\to\MyLibrary.tlb" // DEFAULT assembly name is namespace
namespace MyNameSpace = MyLibrary; // make alias back to your namespace
MyNameSpace::IMyClass1Ptr obj; // use namespace here
obj.CreateInstance(__uuidof(MyNameSpace::MyClass1)); // access class in my namespace
Hope that helps.
I have a old native MFC/c++ dll I have managed to get it compiled with /CLR flag.
Now I have added a managed C++/CLI class to the dll within a namaspace.
The header file is below, and the cpp file only has #include for the header file.
The native dll is a huge dll project with lot of un managed code, but it has only one managed c++ class like below.
When I add that dll as a reference to a .net winforms project I don't see that namespace / class, in the object browser,
and I get compile error for not finding the namespace "ShashiTest"
I am using Visual studio 2008.
Native dlls in mixed mode can not be added as reference to a managed project ?
Or am I missing something
Please help.
#pragma once
#using<mscorlib.dll>
#using<system.windows.forms.dll>
// Class derived from Forms
using namespace System::Windows::Forms;
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Diagnostics;
using namespace System::Windows::Forms;
namespace ShashiTest {
/// <summary>
/// Summary for test
/// </summary>
public ref class test
{
public:
test(void)
{
};
void ShowMessage()
{
MessageBox::Show("Hello World");
}
};
}
When I simplified my problem..I created a fresh MFC dll and added a manged C++ class to it (same class as above) . Compiled with /CLR flag.
When I add this dll to winforms project and run it i get
System.BadImageFormatException. Any clue ?
However I see the class and the name space and winform project compiles fine unlike the problem I have the above.
System.BadImageFormatException is usually caused by having an AnyCPU .NET application reference a DLL containing 32-bit native code. You get an error when running on a 64-bit platform, because the AnyCPU application runs as 64-bit, and can't load the DLL. The fix for this is (easy) to mark the application as x86-only or (hard) to provide both 32-bit and 64-bit versions of all DLLs containing native code.
Of course, you could have some other problem. Checking your DLL with Red Gate Reflector as suggested by #cdleonard in the comments is a great next step. Or ILSpy, which is free.