I would like create my CustomQSpinBox .
This CustomQSpinBox must inherit of QPushButton and QSpinBox
but when I compile this code :
#include <QSpinBox>
#include <QPushButton>
class CustomQSpinBox : public QSpinBox, public QPushButton
{
Q_OBJECT
public:
CustomQSpinBox (QWidget *parent = 0);
~CustomQSpinBox ();
void initMinMax(int min, int max);
void init();
signals:
void needNumpad();
public slots:
void clicked();
};
I get an error :
erreur : C2594: 'static_cast'ÿ: conversions ambigu‰s de 'QObject *' en
'CustomQSpinBox *'
How I must do my inheritance ?
Yes, when the numPad is closed, the value is set in the QSpinBox. The problem is so to open the numPad when I click on the QSpinBox.
For the moment I manage with this code :
#include <QSpinBox>
#include <QPushButton>
#include <QMoveEvent>
#include <QResizeEvent>
class CustomQSpinBox: public QSpinBox
{
Q_OBJECT
public:
CustomQSpinBox(QWidget *parent = 0);
~CustomQSpinBox();
void resizeEvent(QResizeEvent *event);
void moveEvent(QMoveEvent * event);
signals:
void needNumpad();
public slots:
void clicked();
private:
QPushButton * button;
};
Related
part of c++ code :
class CTB_DLL ctb::Terrain {
public:
/// Create an empty terrain object
Terrain();
/// Instantiate using terrain data on the file system
Terrain(const char *fileName);
/// Read terrain data from a file handle
Terrain(FILE *fp);
/// Read terrain data from the filesystem
void
readFile(const char *fileName);
/// Write terrain data to a file handle
void
writeFile(FILE *fp) const;
}
java code:
#Platform(include = "TerrainTile.hpp")
#Namespace("ctb")
public class TerrainTile {
public static class Terrain extends Pointer{
public Terrain() { allocate();}
public native void allocate();
public native void Terrain();
//to call the basic methods
/* public native void Terrain(Pointer filename);
public native void readFile(#Const Pointer filename);
public native void writeFile();*/
}
public static void main(String[] args) {
TerrainTile terrain = new TerrainTile();
}
}
when i using cmd : java -jar javacpp-1.5.5.jar TerrainTile.java
error occur:
Exception in thread "main" java.lang.ClassNotFoundException
This my first time trying to create windows application using vs clr and i'm trying to implement it on my existing cpp project which i almost finished but the main problem that i'm facing is the i can't access the functions of myForm.h (buttons , text box , etc..) from my main source.cpp , any help would be appreciated and thanks
MyForm.h
#pragma once
namespace Project2 {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
/// <summary>
/// Summary for MyForm
/// </summary>
public ref class MyForm : public System::Windows::Forms::Form
{
public:
MyForm(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~MyForm()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::Button^ button1;
private: System::Windows::Forms::RichTextBox^ richTextBox1;
protected:
private:
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
this->button1 = (gcnew System::Windows::Forms::Button());
this->richTextBox1 = (gcnew System::Windows::Forms::RichTextBox());
this->SuspendLayout();
//
// button1
//
this->button1->Location = System::Drawing::Point(104, 177);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(75, 23);
this->button1->TabIndex = 0;
this->button1->Text = L"button1";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &MyForm::button1_Click);
//
// richTextBox1
//
this->richTextBox1->Location = System::Drawing::Point(47, 36);
this->richTextBox1->Name = L"richTextBox1";
this->richTextBox1->Size = System::Drawing::Size(208, 135);
this->richTextBox1->TabIndex = 1;
this->richTextBox1->Text = L"";
this->richTextBox1->TextChanged += gcnew System::EventHandler(this, &MyForm::richTextBox1_TextChanged);
//
// MyForm
//
this->AutoScaleDimensions = System::Drawing::SizeF(8, 16);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(282, 253);
this->Controls->Add(this->richTextBox1);
this->Controls->Add(this->button1);
this->Name = L"MyForm";
this->Text = L"MyForm";
this->ResumeLayout(false);
}
#pragma endregion
public: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {};
public: System::Void richTextBox1_TextChanged(System::Object^ sender, System::EventArgs^ e) {}
};
}
Main.c
#include <iostream>
#include <fstream>
#include <sstream>
#include <locale>
#include <string>
#include<vector>
#include <time.h>
#include "MyForm.h"
using namespace std;
using namespace System;
[STAThread]
int main()
{
System::Windows::Forms::Application::Run(gcnew Project2::MyForm());
MyForm F; \\ can't create an object of MyForm class
F.richtextbox->AppendText("Hello world"); \\ here is the confusing part
System::Windows::Forms::Application::Run(gcnew Project2::MyForm());
return 0;
}
I'm a newbie of JavaCPP, right now I've got a problem.
my TestLibrary.h:
#include <string>
#include <map>
class TestClass {
public:
TestClass() {
property["a"]="b";
}
const std::map<std::string,std::string>& getMap(std::string str) {
if (str == "a"){
return property;
}
}
std::map<std::string,std::string> property;
};
TestLibrary.java
import org.bytedeco.javacpp.*;
import org.bytedeco.javacpp.annotation.*;
#Platform(include="TestLibrary.h")
public class TestLibrary {
public static class TestClass extends Pointer {
static { Loader.load(); }
public TestClass() { allocate(); }
private native void allocate();
public static native #ByRef KeyValueMap getMap(String str);
}
#Name("std::map<std::string,std::string>") public static class
KeyValueMap extends Pointer {
static { Loader.load(); }
public KeyValueMap(Pointer p) { super(p); }
public KeyValueMap() { allocate(); }
private native void allocate();
public native long size();
#Index public native #StdString BytePointer get(#StdString
BytePointer i);
public native KeyValueMap put(#StdString BytePointer i, BytePointer
value);
}
public static void main(String[] args) {
TestClass l = new TestClass();
KeyValueMap m = l.getMap("a");
System.out.println(m);
//System.out.println(m.get("a"));
}
}
when
javac -cp javacpp.jar TestLibrary.java
java -jar javacpp.jar TestLibrary
jniTestLibrary.cpp:2238:30: error: call to non-static member
function without an object argument
rptr = &::TestClass::getMap(ptr0);
~~~~~~~~~~~~~^~~~~~
the code above is modified from the NativeLibrary example. But How to solve the compile problem?
And can I use m.get("a") like that?
I managed to do this by changing...
TestLibrary.h:
#include <string>
#include <map>
class TestClass {
public:
TestClass() {
property["a"]="b";
}
std::map<std::string,std::string>& getMap(std::string str) {
if (str == "a"){
return property;
}
}
std::map<std::string,std::string> property;
};
TestLibrary.java
import org.bytedeco.javacpp.*;
import org.bytedeco.javacpp.annotation.*;
#Platform(include="TestLibrary.h")
public class TestLibrary {
public static class TestClass extends Pointer {
static { Loader.load(); }
public TestClass() { allocate(); }
private native void allocate();
public native #ByRef KeyValueMap getMap(String str);
}
#Name("std::map<std::string,std::string>")
public static class KeyValueMap extends Pointer {
static { Loader.load(); }
public KeyValueMap(Pointer p) { super(p); }
public KeyValueMap() { allocate(); }
private native void allocate();
public native long size();
#Index public native #StdString BytePointer get(#StdString BytePointer i);
public native KeyValueMap put(#StdString BytePointer i, BytePointer value); }
public static void main(String[] args) {
TestClass l = new TestClass();
KeyValueMap m = l.getMap("a");
System.out.println(m.size());
System.out.println(m.get(new BytePointer("a")).getString());
}}
I using qt creator 3.0.1 while building it shows MainWindow has not been declared
I tried changing order of #include statements but no luck.
code that gives error in function insert and display:
#ifndef WEATHER_H
#define WEATHER_H
#include "node.h"
#include "weather.h"
#include "mainwindow.h"
#define NODE_H
class Weather
{
private:
Node *head;
public:
Weather();
void insert(MainWindow *);
void disp(MainWindow *);
};
#endif // WEATHER_H
my other header files:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "weather.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
Weather *w;
};
#endif // MAINWINDOW_H
node.h
#ifndef NODE_H
#define NODE_H
class Node
{
private:
some variables
friend class Weather;
public:
Node();
};
#endif // NODE_H
weather.h:
i want to convert that line from c# to c++/cli
Idocobj is IPart
IPart is an interface and Idocobj is an object.Are there any way to do this conversion.
i used this code :
Idocobj->GetType() == IPart::typeid
but it dosen't work
You can use dynamic_cast to check for "is". Here is an example:
using namespace System;
namespace NS
{
public interface class IFoo
{
void Test();
};
public ref class Foo : public IFoo
{
public: virtual void Test() {}
};
public ref class Bar
{
public: virtual void Test() {}
};
}
template<class T, class U>
bool isinst(U u) {
return dynamic_cast< T >(u) != nullptr;
}
int main()
{
NS::Foo^ f = gcnew NS::Foo();
NS::Bar^ b = gcnew NS::Bar();
if (isinst<NS::IFoo^>(f))
Console::WriteLine("f is IFoo");
if (isinst<NS::IFoo^>(b) == false)
Console::WriteLine("f is not IFoo");
Console::ReadKey();
}
But normally, you never use "is".... you always want to do something with the check... so normally you should use "as" which directly mapps to dynamic_cast:
NS::IFoo^ ifoo = dynamic_cast<NS::IFoo^>(f);
if (ifoo != nullptr)
{
// Do something...
ifoo->Test();
}