GTK drawing area is not realized - c++

In my application I am using a Gtk::DrawingArea like this:
Window win;
DrawingArea area;
Box box(ORIENTATION_VERTICAL);
area.signal_realize().connect(sigc::ptr_fun(&on_video_area_realize));
box.pack_start(myWidgets, true, true);
box.pack_start(area, false, false);
win.add(box);
win.show_all();
The problem is, the function on_video_area_realize is not being called and if I query the status of the DrawingArea with area.get_realized(), it is false, so it has not been realized yet.
I do not understand why it has not been realized? As far as I understand, a widget is realized when it is added to a window - which, as far as I think, I am doing already.

The realize signal happens when the window (and its children) is showed. The following code (I tested this with Gtkmm 3.24.20):
#include <iostream>
#include <gtkmm.h>
void on_video_area_realize()
{
std::cout << "Video drawing area realized!" << std::endl;
}
int main(int argc, char *argv[])
{
std::cout << "Gtkmm version : " << gtk_get_major_version() << "."
<< gtk_get_minor_version() << "."
<< gtk_get_micro_version() << std::endl;
auto app = Gtk::Application::create(argc, argv, "org.gtkmm.examples.base");
Gtk::Window window;
Gtk::Box layout{Gtk::ORIENTATION_VERTICAL};
Gtk::Label label{"Example"};
std::cout << "Checkpoint 1" << std::endl;
Gtk::DrawingArea area;
std::cout << "Checkpoint 2" << std::endl;
area.signal_realize().connect(sigc::ptr_fun(&on_video_area_realize));
std::cout << "Checkpoint 3" << std::endl;
layout.pack_start(label, true, true);
layout.pack_start(area, false, false);
std::cout << "Checkpoint 4" << std::endl;
window.add(layout);
window.show_all();
std::cout << "Checkpoint 5" << std::endl;
return app->run(window);
}
Yields the following output:
Gtkmm version : 3.24.20
Checkpoint 1
Checkpoint 2
Checkpoint 3
Checkpoint 4
Video drawing area realized!
Checkpoint 5
From this, we can see that the drawing area is realized, but not when added to the window.

Related

Why isn't pigpio - gpioHardwarePWM Working when gpioServo is in c++

I have been reading the documentation
I can get gpioServo to run
My motor spins
my goal is to spin my motor one revolution per second
which means; as far as i understand it; a frequency of 1 hz and a duty cycle of 100%
but i can only get gpio servo to work
here is my current code
//#include <QDebug>
//#include <QApplication>
#include <pigpio.h>
#include <iostream>
#include <chrono>
int main(int argc, char *argv[]) {
// QApplication a(argc, argv);
gpioInitialise();
//
//
// system("clear");
// std::cout << std::endl << "Running..." << std::endl;
//
// gpioServo(13, 2000);
// std::cout << std::endl << "Calibrate hi..." << std::endl;
// time_sleep(3);
//
//
// gpioServo(13, 1000);
// std::cout << std::endl << "Calibrate low..." << std::endl;
// time_sleep(3);
//
// gpioServo(13, 0);
// std::cout << std::endl << "Calibrate stop..." << std::endl;
std::cout << std::endl << gpioHardwarePWM(13, 10000, 100000) << std::endl;
std::cout << std::endl << "Run duty cycle..." << std::endl;
time_sleep(3);
system("clear");
std::cout << std::endl << "Done" << std::endl;
gpioTerminate();
// qDebug() << "Hello World";
// return QApplication::exec();
return 0;
}
To me I was assuming the duty cycle would be 100 and the frequency would be 1
but the numbers in my function parameters 10000 and 100000 come from a post on one of the forums i read by the author of pigpio
either way nothing has worked yet with gpioPWM for me
I am in the learning stage so this is very basic but gpioHardwarePWM returns a 0 and the documention on pigpio says that means its good but no spinning motor
I'm just trying learn please a help me see what I'm missing?
Documentation: https://abyz.me.uk/rpi/pigpio/

How can I get joystick data using glfw?

I am trying to get joystick input using glfw. I am using nintendo pro controller.
The code seems pretty simple and straightfoward, but I can't seem to get accurate joystick data.
I used
int axesCount;
const float *axes = glfwGetJoystickAxes(GLFW_JOYSTICK_1, &axesCount);
and tried to access joystick data by
axes[0], axes[1], axes[2], and axis[3]
glfwGetJoystickAxes gave me an accurate number for axesCount, which is 4.
But even if I moved the joystick the axes array always returned 1.52590219e-05.
Below is the whole code for my appliation
#include "pch.h"
#include <iostream>
#include <gl/glew.h>
#include <GLFW/glfw3.h>
int main()
{
GLFWwindow *window;
// initialize
if (!glfwInit()) {
return -1;
}
// create window and its opengl context
window = glfwCreateWindow(640, 480, "OpenGL Project Tutorial", NULL, NULL);
if (!window) {
glfwTerminate();
return -1;
}
// make the window's context current
// loop untill the user closes the window
while (!glfwWindowShouldClose(window)) {
glClear(GL_COLOR_BUFFER_BIT);
// render OpenGL here
int present = glfwJoystickPresent(GLFW_JOYSTICK_1);
std::cout << "Joystick/Gamepad status: " << present << std::endl;
int axesCount;
const float *axes = glfwGetJoystickAxes(GLFW_JOYSTICK_1, &axesCount);
if (1 == present) {
//std::cout << "Number of axes available: " << axesCount << std::endl;
std::cout << "Axis 1: " << axes[0] << std::endl;
std::cout << "Axis 2: " << axes[1] << std::endl;
std::cout << "Axis 3: " << axes[2] << std::endl;
std::cout << "Axis 4: " << axes[3] << std::endl;
std::cout << std::endl;
std::cout << std::endl;
std::cout << std::endl;
}
// swap front and back buffers
glfwSwapBuffers(window);
// poll for and process events
glfwPollEvents();
}
glfwTerminate();
return 0;
}
It didn't matter whether I put
int axesCount;
const float *axes = glfwGetJoystickAxes(GLFW_JOYSTICK_1, &axesCount);
inside the while loop or not.
Any help would be appreciated, thanks.

gdk_screen_get_default() returns null

Please help with this beginner's program for fetching gdk screen attributes. I have a small C++ program to find out the connected display units. I am using c++ on Linux Debian. gdk_screen_get_default() doesn't return Screen object. If I don't check for screen object then the following error occurs.
Error
(process:8023): Gdk-CRITICAL **: gdk_screen_get_monitor_geometry: assertion 'GDK_IS_SCREEN (screen)' failed
I went through related posts and referred to this for the below code snippet.
Thanks for your help. Any pointers/guidelines to resolve this would be helpful.
I have one monitor connected and the display settings are
$ echo $XDG_CURRENT_DESKTOP
GNOME
$ echo $DISPLAY
:0
CODE
#include <gdk/gdk.h>
#include <iostream>
/*
GTK version 3.14.5
g++ getScreenInfo.cpp -o getScreenInfo `pkg-config gtk+-3.0 --cflags --libs`
*/
int main()
{
GdkScreen *screen;
screen = gdk_screen_get_default();
int num_monitors;
int i;
if (screen)
{
num_monitors = gdk_screen_get_n_monitors(screen);
for (i = 0; i < num_monitors; i++)
{
GdkRectangle rect;
gdk_screen_get_monitor_geometry (screen, i, &rect);
std::cout << "monitor " << i << ": coordinates (" << rect.x << ","
<< rect.y << ", size (" << rect.width << "," << rect.height << ")"
<< std::endl;
}
}else
{
std::cout << "Couldn't obtain default screen object" << std::endl;
}
}
27 Apr 2017 EDIT: RESOLVED
#include <iostream>
#include <gdk/gdk.h>
#include <gtk/gtk.h>
/*
GTK version 3.14.5
To compile:
g++ getScreenInfo.cpp -o getScreenInfo `pkg-config gtk+-3.0 --cflags --libs`
*/
int main(int argc, char *argv[])
{
gtk_init(&argc, &argv);
GdkScreen *screen = gdk_screen_get_default();
int num_monitors;
int i;
if (screen)
{
num_monitors = gdk_screen_get_n_monitors(screen);
for (i = 0; i < num_monitors; i++)
{
GdkRectangle rect;
gdk_screen_get_monitor_geometry (screen, i, &rect);
std::cout << "monitor " << i << ": offsets (" << rect.x << ","
<< rect.y << ", size (" << rect.width << "," << rect.height << ")"
<< std::endl;
}
}
else
{
std::cout << "Couldn't obtain default screen object" << std::endl;
}
// To query primary display properties
guint monitor = gdk_screen_get_primary_monitor(screen);
GdkRectangle screen_geometry = { 0, 0, 0, 0 };
gdk_screen_get_monitor_geometry(screen, monitor, &screen_geometry);
std::cout << screen_geometry.x << std::endl;
std::cout << screen_geometry.y << std::endl;
std::cout << screen_geometry.width << std::endl;
std::cout << screen_geometry.height << std::endl;
}
Answering my own question.
Finally, figured out the resolution. gtk_init( ) was missing before fetching the screen. Added that and respective include for gtk/gtk.h and now the code looks like
int main(int argc, char *argv[])
{
gtk_init(&argc, &argv); // <---- added this
GdkScreen *screen = gdk_screen_get_default();
:
followed by rest of the code shared in the problem description above.

QT frame visualisation

Currently started to work with qt and found some bug in my code, and I can't understand where it comes from. Maybe you will see and explain why it happens.
Here is main.cpp code part which changes shutter from 3000 to maximum:
if (camera.test_cam->liveFrameReady())
{
camera.visualizeFrame(camera.test_cam->liveFrame());
camera.set_shutter(0, 1, 3000 + i * 200);
controlWidget->update(camera.test_cam->liveFrame());
i++;
}
The code works slowly(1 fps ), because of the camera.visuzlizeFrame() method:
void OsCam::visualizeFrame(Common::FrameHandle hFrame)
{
void* buffer_ptr = this->getFrameData(hFrame);
int width = hFrame->dataType()->width();
int height = hFrame->dataType()->height();
cv::Mat m(height, width, CV_8UC3, (int*)buffer_ptr);
cv::imshow("test image", m);
cv::waitKey(1);
}
Gui interface shows the camera frame in real time, test image from visualizeFrame at the background
Actionally, I don't need to call this method (I used it just to be sure that I can read the memory and I used opencv because I am more familiar with it).
But if I get rid of this camera.visuzlizeFrame() my gui becomes white and does not give any response.
Even if I use cv::waitKey or Sleep functions, nothing happens to the gui.
void ControlWidget::update(Common::FrameHandle hFrame)
{
try
{
QImage img((uchar*)hFrame->buffer()->data(), hFrame->dataType()->width(), hFrame->dataType()->height(), QImage::Format::Format_RGB888);
QSize standart_size = QSize(hFrame->dataType()->width() / 3, hFrame->dataType()->height() / 3);
QPixmap rectPxmp = QPixmap::fromImage(img).scaled(standart_size);
this->camera_1->setPixmap(rectPxmp);
this->camera_2->setPixmap(rectPxmp);
cv::waitKey(300);
}
catch (GeneralException& e)
{
std::cout << e.what() << std::endl;
}
}
Shall I go to QThreads? One thread for reading the buffer and another one for visualization of gui?
Thank you!
some additional code:
void* OsCam::getFrameData(Common::FrameHandle hFrame)
{
bool displayFrameData = false;
void* pFrameData = NULL;
try
{
if (hFrame)
{
if (displayFrameData)
{
// display information about the frame
cout << "Frame index: " << hFrame->frameIndex();
cout << "Frame timestamp: " << hFrame->timestamp();
// display information about the frame "data type"
DataTypeHandle hDataType = hFrame->dataType();
cout << "Frame size in bytes: " << hDataType->frameSizeInBytes() << endl;
cout << "Width in pixels: " << DataType::width(hDataType) << endl;
cout << "Height in rows: " << DataType::height(hDataType) << endl;
cout << "Bit depth: " << DataType::bitDepth(hDataType) << endl;
cout << "Bytes/line (stride): " << DataType::stride(hDataType) << endl;
// display the frame video format
VideoDataType::Format videoFormat = VideoDataType::format(hDataType);
cout << "Video format: " << VideoDataType::formatString(videoFormat).c_str() << endl;
// get a pointer to the frame data
}
pFrameData = hFrame->buffer()->data();
}
}
catch (GeneralException& e)
{
cout << e.what() << endl;
}
return pFrameData;
}
and main (I changed it a little bit) :
int main(int argc, char **argv)
{
QApplication app(argc, argv);
ControlWidget *controlWidget = new ControlWidget;
//controlWidget->show();
try
{
OsCam camera;
int i = 0;
for (int j = 0; j<10000 ; j++)
{
if ((camera.test_cam->liveFrameReady()))
{
Common::FrameHandle loaded = camera.test_cam->liveFrame()->clone();
camera.visualizeFrame(loaded);
controlWidget->update(loaded);
camera.set_shutter(0, 1, 3000 + i * 200);
loaded->~Frame();
}
i++;
}
}
catch (GeneralException& e)
{
std::cout << e.what() << std::endl;
int a;
std::cin >> a;
return 1;
}
}
After cv::named_window

What's causing this code to increase memory usage

I have the following sample program (C++):
#include <iostream>
#include <gtkmm.h>
#include <boost/thread.hpp>
using namespace std;
Gtk::Window * window;
Glib::Dispatcher dispatcher;
void do_updates();
void load_layout();
int x;
int main(int argc, char ** argv)
{
Glib::RefPtr<Gtk::Application> app = Gtk::Application::create(argc, argv, "nl.mycroes.test");
window = new Gtk::Window;
window->set_default_size(200, 200);
Gtk::Label * label = new Gtk::Label("label");
window->add(*label);
dispatcher.connect(&load_layout);
window->show_all();
boost::thread t = boost::thread(do_updates);
app->run(*window);
Gtk::Widget * child = window->get_children().front();
delete child;
return 0;
}
void load_layout()
{
stringstream layout;
layout << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
<< endl << "<interface>"
<< endl << "<object class=\"GtkWindow\" id=\"window\">"
<< endl << "<child>"
<< endl << "<object class=\"GtkLabel\" id=\"text\">"
<< endl << "<property name=\"visible\">True</property>"
<< endl << "<property name=\"can_focus\">False</property>"
<< endl << "<property name=\"label\">" << x << "</property>"
<< endl << "</object>"
<< endl << "</child>"
<< endl << "</object>"
<< endl << "</interface>";
Glib::RefPtr<Gtk::Builder> builder = Gtk::Builder::create_from_string(layout.str());
Gtk::Window * win;
builder->get_widget("window", win);
std::vector<Gtk::Widget *> old_children = window->get_children();
Gtk::Widget * child = old_children.front();
window->remove();
delete child;
std::vector<Gtk::Widget *> new_children = win->get_children();
child = new_children.front();
child->reparent(*window);
delete win;
}
void do_updates()
{
for (x = 0; x < 10000; x++)
{
boost::this_thread::sleep(boost::posix_time::milliseconds(1));
dispatcher();
}
}
If I keep an eye on the memory usage (watch -n 1 "grep ^Vm /proc/\$(pgrep GtkLeakTest)/status") it keeps increasing, but I don't know what's staying around in memory. Also, valgrind doesn't report any leaks at all, which makes it seem like I'm properly cleaning everything up...
I actually tried if I had to recursive delete the window child, because this is a testcase for a program where I'm loading a larger galde layout and that also leaks, but I tried and and logically doesn't make a difference because I don't put nested components in the window. Any hints are appreciated!