When I debug the pcl documentation visualization mouse event function code as following shows:
viewer->registerMouseCallback(mouseEventOccurred, (void*)&viewer);
void mouseEventOccurred(const pcl::visualization::MouseEvent &event,void* viewer_void)
{
boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer = *static_cast<boost::shared_ptr<pcl::visualization::PCLVisualizer> *> (viewer_void);
if (event.getButton() == pcl::visualization::MouseEvent::LeftButton &&
event.getType() == pcl::visualization::MouseEvent::MouseButtonRelease)
{
std::cout << "Left mouse button released at position (" << event.getX() << ", " << event.getY() << ")" << std::endl;
char str[512];
sprintf(str, "text#%03d", text_id++);
viewer->addText("clicked here", event.getX(), event.getY(), str);
}
}
If I remove the code:
viewer->addText("clicked here", event.getX(), event.getY(), str);
It would work well, or I encountered an error:
I do not know how to solve this problem. Thanks!
After I clicked the Retry, the exception shows as:
Exception
Related
What is the difference between the output of event.window.windowID and SDL_GetWindowID()?
Why is it that std::cout << m_SDLEvent.window.windowID << std::endl;
outputs 1819558491 in console while std::cout << SDL_GetWindowID(m_SDLWindow) << std::endl; outputs 1 ?
How would I achieve getting the right ID of my SDL_Window* in the method below?
void InputManager::Update()
{
SDL_PollEvent(&m_SDLEvent);
switch (m_SDLEvent.type)
{
case SDL_QUIT:
std::cout << m_SDLEvent.window.windowID << std::endl;
SDL_HideWindow(SDL_GetWindowFromID(m_SDLEvent.window.windowID));
break;
}
}
You're seeing garbage window ID because you access an inactive union field. That's undefined behavior.
You can only access m_SDLEvent.window if m_SDLEvent.type == SDL_WINDOWEVENT.
But if m_SDLEvent.type == SDL_QUIT, you have to use m_SDLEvent.quit structure, which has no field for window id (because SDL_QUIT is not specific to a window, but means that the entire application should be closed).
Okay so HolyBlackCat's answer brought me to the right direction.
Instead of using SDL_QUIT (which is the quit event for the entire app, not one window) I should've checked for SDL_WINDOWEVENT_CLOSE which is an SDL_WINDOWEVENT which can be received by m_SDLEvent.window.event instead of m_SDLEvent.type
So the code now looks like this:
void InputManager::Update()
{
SDL_PollEvent(&m_SDLEvent);
if (m_SDLEvent.type == SDL_WINDOWEVENT)
{
switch (m_SDLEvent.window.event)
{
case SDL_WINDOWEVENT_CLOSE:
std::cout << m_SDLEvent.window.windowID << std::endl;
SDL_HideWindow(SDL_GetWindowFromID(m_SDLEvent.window.windowID));
break;
}
}
}
Now std::cout << m_SDLEvent.window.windowID << std::endl; outputs the correct ID.
I have a folder with many images, I've loaded the filenames into a vector of string and want to display the images in a GTK::Image component like a video, 25 frames (images) per second. But I the component don't re-draw after run the app.
I call the queue_draw(), but nothing change. How I do it?
EDIT:
The main function is like:
Glib::RefPtr<Gtk::Application> app = Gtk::Application::create("org.gtkmm.example");
refBuilder = Gtk::Builder::create();
try{
refBuilder->add_from_file("main_window2.glade");
}
catch(const Glib::FileError& ex){
std::cerr << "FileError: " << ex.what() << std::endl;
return 1;
}
catch(const Glib::MarkupError& ex){
std::cerr << "MarkupError: " << ex.what() << std::endl;
return 1;
}
catch(const Gtk::BuilderError& ex){
std::cerr << "BuilderError: " << ex.what() << std::endl;
return 1;
}
refBuilder->get_widget("main_window", main_window);
cout << "window: " << main_window << endl;
if(main_window)
{
Gtk::ImageMenuItem* newButton = nullptr;
Gtk::ImageMenuItem* openButton = nullptr;
Gtk::ImageMenuItem* saveButton = nullptr;
Gtk::Button* playButton = nullptr;
refBuilder->get_widget("new_button", newButton);
refBuilder->get_widget("open_button", openButton);
refBuilder->get_widget("save_button", saveButton);
refBuilder->get_widget("play_button", playButton);
refBuilder->get_widget("image_container", container);
refBuilder->get_widget("image_component", image_component);
if(playButton)
playButton->signal_clicked().connect(sigc::ptr_fun(on_image_draw));
if(newButton)
newButton->signal_activate().connect( sigc::ptr_fun(on_new_button_activate) );
if(openButton)
openButton->signal_activate().connect( sigc::ptr_fun(on_open_button_activate) );
if(saveButton)
saveButton->signal_activate().connect( sigc::ptr_fun(on_save_button_activate) );
cout << "running main window" << endl;
app->run(*main_window);
}
delete main_window;
And when clicked in playButton, the drawing code is called
static void on_image_draw(){
string path = video.getDirectory();
string aux = path + "/";
cout << "reproduzindo: " << aux << endl;
vector<string> vetor = video.get_imageVector();
cout << "vetor" << endl;
for(auto frame: video.get_imageVector()){
aux += frame;
cout << "frame: " << aux << endl;
image->clear();
image->set(aux);
Glib::RefPtr<Gdk::Pixbuf> pix = image->get_pixbuf();
pix = pix->scale_simple(WIDTH, HEIGHT, Gdk::INTERP_BILINEAR);
image->set(pix);
image->queue_draw();
aux = path+"/";
sleep(1);
}
}
You're not supposed to draw all your frames in the draw signal handler. You basically need to set the image, then call queue_draw. You're doing this right, but then you must let GTK+ do its part of the work. You can't just loop: calling queue_draw puts a draw event in the main loop that GTK+ needs to process to draw your image.
Remember you are single-threaded, so the CPU is like a shovel. When you are in a callback, you get the shovel and do your work. You can't expect GTK+ to process the events you send to it if you don't give it the shovel back.
Here's how you're supposed to architcture this stuff. Please keep in mind that if you need 25fps, this approach is sub-optimal, and you should probably give a look at video libraries like gstreamer.
So when you press play, you're supposed to trigger an event source like a timer or idle handler. That's because you need something to say: I need to isplay a new image. Give a look at g_timeout_add or g_idle_add in the GLib for that (I know only the names for the C api, find the equivalent ones in gtkmm).
Then in the callback associated to that timeout or idle event, you set your image (a single one, no loop), and that's all. Not sure calling queue_draw is even required. It would be if you were drawing by hand in a GtkDrawingArea, but as that's a GtkImage, so by setting a new image you say implicitely that the image has changed and should be redrawn.
I'm creating a Qt WebEngine application for Windows that display a webpage and uses printer to print out whatever is displayed. I'm trying to get an error state out of my Custom vkp80III ticket printer using windows' winspool library and this example https://support.microsoft.com/en-us/kb/160129
App has this print call. (printerState is useless, it seems to be always idle but anyways):
void printReceipt() {
if (mDefaultPrinter->printerState() == QPrinter::Error) {
this->printCallback(false);
}
else {
this->print(mDefaultPrinter, [=](bool success) { this->printCallback(success); });
}
}
And then comes the problem, callback after print succesfully started:
void printCallback(bool success){
DWORD size;
bool result = GetDefaultPrinter (NULL, &size);
qDebug() << "LEN: " << size;
LPWSTR pname = (LPWSTR)malloc(size+1);
result = GetDefaultPrinter(pname, &size);
if(!result){
qDebug() << "GetDefaultPrinter err: " << GetLastError();
}
else {
pname[size] = 0;
qDebug() << "PRINTER NAME: " << &pname << ", LEN: " << size;
}
QString name = mDefaultPrinter->printerName();
//These give more or less odd results
//wchar_t* w_str = const_cast<wchar_t*>(name.toStdWString().c_str());
//LPWSTR w_str = (LPWSTR)name.utf16();
/*wchar_t* w_str = (wchar_t*) malloc (sizeof(wchar_t)*name.length()+1);
int len = name.toWCharArray(w_str);
w_str[len]=0;*/
//OpenPrinter example uses LPHANDLE but that didn't work
HANDLE hPrinter = NULL;
PRINTER_DEFAULTS pd;
ZeroMemory(&pd, sizeof(pd));
pd.DesiredAccess = PRINTER_ALL_ACCESS;
qDebug() << "TRYING GET PRINTER: " << name;
if(OpenPrinter(pname, &hPrinter, &pd)) {
qDebug() << "GOT PRINTER ERR STATE: " << IsPrinterError(&hPrinter);
}
free(pname);
emit printReceiptComplete(success);
}
I put couple of debug logs inside MSDN example and it shows that when code tries to GetPrinter(hPrinter) it returns INVALID_HANDLE_ERROR. So I never get to point of getting the printer's errors. First I thought it was the wrong printer name, that's why there are different lines of code getting it / converting it to LPWSTR.
So what works:
-I get default printer's name "CUSTOM VKP80III" so it IS there
-OpenPrinter(&handle) returns true and I get the handle
Problem
-GetPrinter(handle) returns false and GetLastError() is invalid handle, why?
Oh so much facepalming right now. This:
IsPrinterError(&hPrinter);
should be this:
IsPrinterError(hPrinter);
I'm trying to use WindowFromPoint() to retrieve the handle of a button and then use SendMessage() to click the button.
I don't get an error with my code but the button isn't getting clicked either..
Basically nothing happens.
I have already confirmed that the coordinates point directly to the location of the button.
I have also tried ChildWindowFromPoint() but it didn't change anything.
Here is my code:
std::cout << "Assigning parent Window..." << std::endl;
for (std::list<Window>::iterator i = mainDetector.getWindowList().begin(); i != mainDetector.getWindowList().end(); ++i)
{
if (i->getTitle().find(windowTitle) != std::string::npos)
{
parentWindowHandle = (HWND)i->getHandle();
std::cout << "Parent Window assigned." << std::endl;
break;
}
}
std::cout << "Clicking button..." << std::endl;
buttonHandle = ChildWindowFromPoint(parentWindowHandle, registerButtonLoc);
SendMessage(buttonHandle, WM_LBUTTONDOWN, NULL, NULL);
SendMessage(buttonHandle, WM_LBUTTONUP, NULL, NULL);
Hope someone tell me where my mistake is..
I'm modifying the stock quoter example from the wustl CORBA release. The assignment is to implement a reply handler for the StockFactory class that handles calls to get_stock()
Here's my FactoryHandler implementation:
FactoryHandler_i.h:
#ifndef TAO_TUTORIALS_QUOTER_AMI_CLIENT_FACTORYHANDLER_I_H
#define TAO_TUTORIALS_QUOTER_AMI_CLIENT_FACTORYHANDLER_I_H
#include "QuoterS.h"
class Stock_Factory_Handler_i : public POA_Quoter::AMI_Stock_FactoryHandler
{
public:
Stock_Factory_Handler_i (int *response_count, ::Quoter::Stock_var& result);
void get_stock (::Quoter::Stock_ptr ami_return_val);
void get_stock_excep (::Messaging::ExceptionHolder * excep_holder);
private:
int *response_count_;
::Quoter::Stock_var& result_;
};
#endif /* TAO_TUTORIALS_QUOTER_AMI_CLIENT_HANDLER_I_H */
FactoryHandler_i.cpp:
#include "FactoryHandler_i.h"
#include "ace/streams.h"
Stock_Factory_Handler_i::
Stock_Factory_Handler_i (int *response_count, ::Quoter::Stock_var& result)
: response_count_ (response_count), result_ (result)
{
}
void
Stock_Factory_Handler_i::get_stock (::Quoter::Stock_ptr ami_return_val)
{
cout << "storing result" << endl;
result_ = ami_return_val;
(*this->response_count_)++;
}
void
Stock_Factory_Handler_i::get_stock_excep (::Messaging::ExceptionHolder * excep_holder)
{
// We ignore the exception, but this counts as a response, otherwise
// the application would not finish.
cerr << "Exception raised while getting stock"
<< endl;
(*this->response_count_)++;
}
And the client.cpp, from just before the part where changes have been made:
// ...
// Create and activate the handler...
int response_count = 0;
Single_Query_Stock_Handler_i handler_i (&response_count);
Quoter::AMI_Single_Query_StockHandler_var handler =
handler_i._this ();
// Create and activate the factory handler...
Quoter::Stock_var result;
Stock_Factory_Handler_i factory_handler_i (&response_count, result);
Quoter::AMI_Stock_FactoryHandler_var factory_handler =
factory_handler_i._this();
// Send all the requests, careful with error handling
int request_count = 0;
for (int i = 2; i != argc+1; ++i) {
try {
// Get the stock object
cout << "looking up stock symbol " << argv[i] << endl;
factory->sendc_get_stock (factory_handler.in (), argv[i]);
sleep(3); // wait for a response
cout << "converting result" << endl;
Quoter::Single_Query_Stock_var stock =
Quoter::Single_Query_Stock::_narrow (result.in ());
cout << "checking result" << endl;
CORBA::Any any;
any <<= stock;
CORBA::TypeCode_var tc = any.type();
cout << tc->kind() << endl;
if (CORBA::is_nil (stock.in ())) {
cerr << "Cannot get single query interface for <"
<< argv[i] << ">" << endl;
continue;
}
cout << "reading result" << endl;
stock->sendc_get_price_and_names (handler.in ());
request_count++;
}
catch (Quoter::Invalid_Stock_Symbol &) {
cerr << "Invalid stock symbol <"
<< argv[i] << ">" << endl;
}
}
while (response_count < 2 * request_count // multiply by 2 because both handlers increment response_count
&& orb->work_pending ()) {
orb->perform_work ();
}
// ...
When running the client, the output is:
looking up stock symbol MSFT
converting result
checking result
14
Cannot get single query interface for <MSFT>
(The 14 is the typecode for Stock, that's only for debugging)
Notably missing from the above is the "storing result" message that's supposed to be printed in the FactoryHandler's get_stock() callback method. I'm at a loss as to why, since the sendc_get_stock() method doesn't produce any (immediate) errors and is basically just a copy of the StockHandler's code, and from there it's the responsibility of the AMI/ORB interface to make the callback. But the original example (with a StockHandler only) works fine.
What am I doing wrong (and how do I fix it)?
EDIT: another bit of information: on the server side, StockFactory's get_stock() method does get called.
Sorry, I've no aswer for you. But a hint, ask your question at TOA's maling list at http://www.cs.wustl.edu/~schmidt/ACE-mail.html
HTH
I think that your problem is that work_pending returns true only if the ORB has immediate work to do, so it returns false in the time after your client sent his request and before the server sends his reply.
To validate that, simply remove the && orb->work_pending() condition from the loop, and use the version of perform_work that takes a timeout argument.