I am trying to embed the gnuplot window into my application using the socket/plug concept in gtkmm 3 library. I have followed the example in the official page here and everything works as expected.
Then I moved to embedding gnuplot window. I modified the socket.cpp as follows:
#include <iostream>
#include <fstream>
#include <gtkmm.h>
#include <gtkmm/socket.h>
using namespace std;
class MySocketWindow : public Gtk::Window
{
public:
MySocketWindow()
{
auto socket = Gtk::manage(new Gtk::Socket());
add(*socket);
cout << "Socket id is: " << hex << socket->get_id() << endl;
show_all();
}
};
int main(int argc, char** argv)
{
auto app = Gtk::Application::create(argc, argv, "org.gtkmm.example.socket");
MySocketWindow win;
app->run(win);
return 0;
}
I compile/build the code with:
g++ --std=c++11 socket.cpp -o socket `pkg-config gtkmm-3.0 --cflags --libs`
And run it. A black socket window appears with Socket id is 3e0000b message printed on the terminal.
.\socket
Then I run gnuplot in x11 terminal with the corresponding window id above:
Now when I plot sin(x) in gnuplot, I am expecting the socket window to show the plot, but nothing happens. What I am doing wrong here?
I am running Ubuntu 16.04, 64-bit.
I found the cause of the problem on gnuplot's site - see my original question. Now it's left to find how to fix it on the socket's site.
Related
I want to make a GTK widget, when dragged and dropped in another program, will act as if a file was dropped (behaving the same way as dragging a file from Nautilus).
I have tried 3 different ways of setting the drag data in the callback, but nothing seems to work:
#include <bits/stdc++.h>
#include <gtkmm.h>
#define FILENAME "/tmp/foo.txt"
int main(int argc, char *argv[]) {
auto app = Gtk::Application::create(argc, argv, "com.tyilo.foo");
Gtk::Window window;
window.set_default_size(200, 200);
window.set_keep_above();
Gtk::Button button("Drag this");
window.add(button);
std::vector<Gtk::TargetEntry> listTargets;
listTargets.push_back(Gtk::TargetEntry("text/uri-list"));
//listTargets.push_back(Gtk::TargetEntry("text/plain"));
//listTargets.push_back(Gtk::TargetEntry("text/plain;charset=utf-8"));
//listTargets.push_back(Gtk::TargetEntry("UTF8_STRING"));
//listTargets.push_back(Gtk::TargetEntry("COMPOUND_STRING"));
//listTargets.push_back(Gtk::TargetEntry("TEXT"));
//listTargets.push_back(Gtk::TargetEntry("STRING"));
button.drag_source_set(listTargets, Gdk::ModifierType(GDK_BUTTON1_MASK | GDK_BUTTON3_MASK),
Gdk::DragAction(GDK_ACTION_COPY | GDK_ACTION_MOVE));
button.drag_source_set_icon("document-save");
button.signal_drag_data_get().connect([&](auto, auto selection_data, auto, auto) {
std::cout << "Data get: " << selection_data.get_target() << std::endl;
auto uri = Glib::filename_to_uri(FILENAME);
std::cout << uri << std::endl;
// All but one of (1), (2) and (3) should be commented out
// (1)
selection_data.set_uris({uri});
// (2)
//uri += "\r\n";
//selection_data.set(selection_data.get_target(), 8, (const guchar*)uri.c_str(), uri.bytes());
// (3)
//selection_data.set("text/uri-list", "file:///tmp/foo.txt\r\n");
});
window.show_all();
return app->run(window);
}
Do I need to specify more targets than just text/uri-list? Do I need to handle these differently?
Or am I doing something wrong with setting the selection data?
I'm using gtkmm3 version 3.24.1 and gtk3 version 3.24.8 and I'm compiling with:
g++ foo.cpp $(pkg-config gtkmm-3.0 --cflags --libs)
An easy way to test if it works, is try to drag the file to the following <input> tag in your browser:
<input type=file value="Drop here">
I'm trying to get Qt creator to print a user input by using a push button on an UI into the terminal. As of now, the code is executable on the terminal via human input. Here is the code:
void MainWindow::on_pushButton_clicked()
{
QProcess::execute("/catkin_ws/devel/lib/submodbus");
system("cd catkin_ws/devel/lib/submodbus");
system("./submodbus_node");
}
Current output when using the code
Output via human input
The versions i'm running on are:
-Ubuntu 16.04
-QT Creator 3.5.1
system can't change the current directory globally. but could use like this:
system("cd /catkin_ws/devel/lib/submodbus && ./submodbus_node");
or using QProcess::setProgram with QProcess::setWorkingDirectory
QProcess p;
p.setProgram("submodbus_node");
//p.setArguments(QStringList()<<args); // if you need
p.setWorkingDirectory("/catkin_ws/devel/lib/submodbus");
p.start();
or QDir::setCurrent
QDir::setCurrent("/catkin_ws/devel/lib/submodbus");
QProcess::startDetached("submodbus_node");
Test demo, create three files in the parent directory:
#include <QApplication>
#include <QProcess>
#include <QDir>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
system("cd ../ && touch test1.txt");
QProcess p;
p.setProgram("touch");
p.setArguments(QStringList()<<"test2.txt");
p.setWorkingDirectory("../");
p.start();
QDir::setCurrent("../");
QProcess::startDetached("touch test3.txt");
return a.exec();
}
I am trying to grab the names of webcams plugged into my computer and shove them into a combobox, then access the name later. Here is my code:
#include <QApplication>
#include <QComboBox>
#include <QCameraInfo>
#include <iostream>
int main(int argc, char *argv[])
{
QApplication app{ argc, argv };
QComboBox combo;
QList<QCameraInfo> info = QCameraInfo::availableCameras();
foreach(QCameraInfo i, info)
combo.addItem(i.description());
combo.show();
std::cout << combo.currentText().toStdString() << std::endl;
return app.exec();
}
The code creates and shows a combo box that has the name of a webcam that I have plugged in to the computer. It will then toss me an access violation exception in trying to print the combo box string to the console.
If I comment the cout line out, all is well, but on exit I get a Debug Assertion Failed! message:
Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)
which I take to mean I am deleting an object that has been deleted (the QString in the combobox???).
If I change the code to fill the combo box with dummies:
#include <QApplication>
#include <QComboBox>
#include <QCameraInfo>
#include <iostream>
int main(int argc, char *argv[])
{
QApplication app{ argc, argv };
for(int i=0; i<2; i++)
combo.addItem(QString("la la la");
combo.show();
std::cout << combo.currentText().toStdString() << std::endl;
return app.exec();
}
I get the same error on the cout, but if I comment that line out, the application exits correctly. I am using Visual Studio 2013, Windows 7, and Qt5.
Now it works. I kept the same source code, but completely scrapped the existing project and started a new one from scratch.
I've discovered that if I set the Runtime Library flag to Multi-Threaded DLL Debug, I will get access violation errors. If I set it to Multi-Threaded DLL, it is fine.
There may have been some other project settings that contributed, but this seems to be the main culprit.
I am trying to run a simple image show program in Eclipse CDT in MinGW built using cmake.
OpenCV Include Path : "E:\cv\opencv\eclipse\install\include"
OpenCV Library Path : "E:\cv\opencv\eclipse\lib" (has all libraries eg.libopencv_highgui310)
My Code is,
#include <iostream>
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
using namespace std;
using namespace cv;
int main(int argc, const char** argv) {
Mat img(500, 500, CV_8UC3, Scalar(100, 0, 0));
cout << "LOL!!!" << endl;
if (img.empty()) {
cout << "Error: Image cannot be loaded." << endl;
system("pause");
return -1;
}
namedWindow("Image Window", CV_WINDOW_AUTOSIZE);
imshow("Image Window", img);
if (waitKey(10) == 27) {
return -1;
}
destroyWindow("Image Window");
return 1;
}
When I build the code my console shows,
07:19:50 **** Incremental Build of configuration Release for project opencv_cpp ****
Info: Internal Builder is used for build
g++ "-IE:\\cv\\opencv\\eclipseBuild\\install\\include" -O3 -Wall -c -fmessage-length=0 -o "src\\faceDetect.o" "..\\src\\faceDetect.cpp"
g++ "-LE:\\cv\\opencv\\eclipseBuild\\lib" -o opencv_cpp.exe "src\\faceDetect.o" -llibopencv_highgui310 -llibopencv_core310 -llibopencv_imgproc310 -llibopencv_imgcodecs310 -llibopencv_objdetect310
07:19:56 Build Finished (took 5s.647ms)
When I run the program it just terminates and nothing happens. Even the print statement is not executed.
Here is the youtube link for the video of the problem,
https://youtu.be/kCrz_WPi_AI
Can someone help me with this?
Even I too faced this problem for longtime.
I researched through all the forum's , websites and all over google but i'm not able to find the answer.
But suddenly I thought of switching off the Windows Firewall and Windows Defender, And it magically works for me.. You too try it and let me know the result !
Happy coding :)
hy,
I found out, that gtkmm is delaying the output I wrote to std::cerr/std::cout until the Gtk::Window is closed.
But after a bit of researching I found out, that this is not always true. For example, if I add a Button to the Window, and I show the button, outputs on STDOUT and STDERR are working as exepcted.
To test this, I have written an MWE: (If you uncomment the line _button.show();, you would see, that output is not delayed. But if you don't show the button, the output is delayed, until the window is closed.)
Also I have addedd a ofstream, which outputs the contents in both cases directly to /tmp/test.
#include <gtkmm/window.h>
#include <gtkmm/button.h>
#include <gtkmm/application.h>
#include <fstream>
#include <iostream>
class window_t : public Gtk::Window {
public:
window_t(void);
private:
Gtk::Button _button;
};
window_t::window_t(void): _button("Click ME") {
add(_button);
//_button.show();
std::cerr << "construct-cerr" << std::endl;
std::cout << "construct-cout" << std::endl;
std::cout.flush();
std::cerr.flush();
std::ofstream file("/tmp/test");
file << "construct-file" << std::endl;
file.close();
}
int main(int argc, char* argv[]) {
auto app = Gtk::Application::create(argc, argv, "com.example.test");
window_t gtkmm_window;
return app->run(gtkmm_window);
}
You can compile this code by:
g++ main.cpp -o main `pkg-config --cflags --libs gtkmm-3.0` -Wall -pedantic -Wextra -Werror -Wcast-qual -Wcast-align -Wconversion -fdiagnostics-color=auto -g -O2 -std=c++11
Why does gtkmm delay outputs to STD(OUT|ERR) and how can I prevent this?
I am using gcc version 4.9.2 and gtkmm 3.14.