i'm trying to learn c++, but i can not find if it's possible to extend a class in this way:
main.cc
#include "mWindow.h"
using namespace std;
int main( int argc, char* argv[] ) {
gtk_init( &argc, &argv );
mWindow win = mWindow();
gtk_main();
return 0;
}
mWindow.cc
#include "mWindow.h"
mWindow::mWindow() {
gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (this, "my window");
gtk_widget_show_all (GTK_WIDGET(this));
}
mWindow.h
#ifndef MWINDOW_H_INCLUDED
#define MWINDOW_H_INCLUDED
#include <gtk/gtk.h>
using namespace std;
class mWindow : public GtkWindow {
public:
mWindow();
};
#endif
I suggest you take a look at gtkmm (http://www.gtkmm.org/) if you want to use GTK+ in conjunction with C++, i.e. there is no need to try to reinvent the wheel and write your own C++ interface for GTK+ (which is a C library).
thanks,
I was trying to use C libraries as if they were C++.
This is how I solved with gtkmm:
main.cc
#include <gtkmm/main.h>
#include "examplewindow.h"
int main(int argc, char *argv[])
{
Gtk::Main kit(argc, argv);
ExampleWindow window;
Gtk::Main::run(window);
return 0;
}
examplewindow.h
#ifndef GTKMM_EXAMPLEWINDOW_H
#define GTKMM_EXAMPLEWINDOW_H
#include <gtkmm-2.4/gtkmm.h>
class ExampleWindow : public Gtk::Window {
public:
ExampleWindow();
};
#endif //GTKMM_EXAMPLEWINDOW_H
examplewindow.cc
#include "examplewindow.h"
ExampleWindow::ExampleWindow() {
set_title("Gtk::TextView example");
set_border_width(5);
set_default_size(400, 200);
show_all_children();
}
also add the command to complete successfully, at least on Arch Linux:
g++ $(pkg-config --cflags --libs gtkmm-2.4) main.cc examplewindow.cc examplewindow.h -o executable
another small indication, what i shouldl use as dynamic arrays or vectors and for hashmap?
Related
I Want to use MediaInfo.dll downloaded from here [DLL v0.7.94][1]
[1]: https://mediaarea.net/bg/MediaInfo/Download/Windows. My question is how to call some function in this .dll using Qt framework
#include <QCoreApplication>
#include <QLibrary>
#include <QDebug>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
if (QLibrary::isLibrary("MediaInfo.dll")) { // C:/MediaInfo.dll
QLibrary lib("MediaInfo.dll");
lib.load();
if (!lib.isLoaded()) {
qDebug() << lib.errorString();
}
if (lib.isLoaded()) {
qDebug() << "success";
}
}
return a.exec();
}
You have a good example in QLibrary documentation. Basically you have to know the function name (the symbol) and it's prototype.
#include <QCoreApplication>
#include <QLibrary>
#include <QDebug>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
if (QLibrary::isLibrary("MediaInfo.dll")) { // C:/MediaInfo.dll
QLibrary lib("MediaInfo.dll");
lib.load();
if (!lib.isLoaded()) {
qDebug() << lib.errorString();
}
if (lib.isLoaded()) {
qDebug() << "success";
// Resolves symbol to
// void the_function_name()
typedef void (*FunctionPrototype)();
auto function1 = (FunctionPrototype)lib.resolve("the_function_name");
// Resolves symbol to
// int another_function_name(int, const char*)
typedef int (*AnotherPrototypeExample)(int, const char*);
auto function2 = (AnotherPrototypeExample)lib.resolve("another_function_name");
// if null means the symbol was not loaded
if (function1) function1();
if (function2) int result = function2(0, "hello world!");
}
}
return a.exec();
}
You need to declare a function prototype and get a pointer to a function in DLL.
QLibrary myLib("mylib");
typedef void (*MyPrototype)();
MyPrototype myFunction = (MyPrototype) myLib.resolve("mysymbol");
if (myFunction)
myFunction();
See more on QLibrary.
Why do you want to use QLibrary when there is a C/C++ binding?
Include file with functions prototypes.
Example with dynamic call of the DLL.
A bit hidden, but all is included in the the DLL zip package in the link you provided in your question.
Jérôme, developer of MediaInfo
I am starting to learn threads in the C++11 standard in Qt.I can't include library ,no such of directory For example, I have the following simple code:
#include <QCoreApplication>
#include <thread>
#include <iostream>
using namespace std;
void func();
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
MyThread th1;
th1.run();
return a.exec();
}
void func()
{
cout << "This string from thread!"<<endl;
}
On the second string of the code, I have an error. The compiler doesn't "see" , I know that i must "include" 11 standard, so i go to .pro and paste CONFIG += c++11, but it isn't helping me :C
Please, I need your help!
You try to use QThread subclass, but you said that you want to use C++11 thread so use this:
#include <thread>
#include <QDebug>
#include <QApplication>
#include <iostream>
void foo()
{
std::cout << "This string from thread!"<<endl;
//while(true)
//{
// qDebug() <<"works";
// Sleep(500);
//}
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
std::thread t(foo);
t.join();
return a.exec();
}
Also next is wrong:
MyThread th1;
th1.run();
Should be:
MyThread th1;
th1.start();
Details about threading with Qt classes:
https://www.qt.io/blog/2010/06/17/youre-doing-it-wrong
http://qt-project.org/doc/qt-5/thread-basics.html
http://qt-project.org/doc/qt-5/qtconcurrent-index.html
Given below is the code for embedding an application using QX11EmbedContainer.
#include "mainwindow.h"
#include <QApplication>
#include <QX11EmbedContainer>
#include <QProcess>
using namespace std;
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
QX11EmbedContainer container;
container.show();
QProcess process(&container);
process.start("C:/Users/Administrator/Desktop.../.exe");
int status=a.exec();
process.close();
return status;
}
On running the application the error that I am getting is:
C:\Qt4.8.5\src\gui\kernel\qx11embed_x11.h:77: error: C2061: syntax error : identifier 'XEvent'
Where am I going wrong?
QX11EmbedContainer is X11-specific, it will not work under other systems (like Windows).
Here is my implementation
Film.h //header
#ifndef FILM_H
#define FILM_H
#include <QString>
class Film {
protected:
QString title;
double dailyRate;
public:
Film(QString ti,double dr);
virtual double calculateRental(int num)const;
};
#endif // FILM_H
Film.cpp
#include "film.h"
#include <QString>
Film::Film(QString ti,double dr){
title=ti;
dailyRate=dr;
}
double Film::calculateRental(int num)const {
return dailyRate*num;
}
main.cpp
#include <QtCore/QCoreApplication>
#include <QtCore/QTextStream>
#include "film.h"
using namespace std;
int main(int argc, char *argv[]) {
QCoreApplication a(argc, argv);
QTextStream cout(stdout, QIODevice::WriteOnly);
Film f("Top Gun", 10.00); //create an instance of a film
cout <<f.calculateRental(2);
return a.exec();
}
how do I count the number of Film instances created? I know is something like that:
static int numOfFilms;
numOfFilms++;
how do I use the code?
It depends. If you replace Class with class, and QString has a conversion constructor from const char*, then yes.
I'm implementing gtest now, and it gives me an error : main previously defined here.
Here's utest.cpp
// Bring in my package's API, which is what I'm testing
#include "../src/test.cpp"
// Bring in gtest
#include <gtest/gtest.h>
// Declare a test
TEST(TestSuite, testCase1)
{
EXPECT_EQ(5,getX(5));
}
// Run all the tests that were declared with TEST()
int main(int argc, char **argv){
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
And here's the code that i'm testing
test.cpp
#include "ros/ros.h"
#include "std_msgs/String.h"
#include <Project2Sample/R_ID.h>
#include <geometry_msgs/Twist.h>
#include <nav_msgs/Odometry.h>
#include <sensor_msgs/LaserScan.h>
#include <sstream>
#include "math.h"
int getX(int x)
{
return x;
}
int main(int argc, char **argv)
{
return 0;
}
There's nothing in test.cpp main but actual code will have some codes in main.
I dont have header files for utest and test cpp files
I tried
#ifndef UTEST_H
#define UTEST_H
and didn't solve the error.
The error message states what the problem is, you have two main() functions. I believe you want to remove the duplicate main() from test.cpp.