Re: [gtkmm] Timer Question



Dean Kutryk wrote:
I want an object to send a signal to another object (a control class)
after a timed interval, beginning when the object is created. After the
time has expired, the object will send a signal, than it will be
eligible for destruction. At any rate, how would I manage the time
feature in Gtkmm or Gtk+? Do I have to use the system clock or
something?


Dean,

Keep in mind I'm new to Gtkmm and Gtk+ programming, but here is how I solved a need to have a timeout without using system calls. Using a system call was my first idea also.

This is from the GUI level of a program that I'm working on. I wanted to have the search entry to do a search on the database if the user pauses in typing.

My comments in the program explain what's going on. Something that is not obvious (unless you read the programmers reference manual), is that when on_txtSearch_timeout returns 'false' it causes the timer to stop or disconnect the timeout. If 'true' is returned, the timeout would happen again in 1.5 seconds.

Jeff

//
// on_txtSearch_changed
//
// Called whenever the content of the search box is changed.  This
// callback sets a timer so that when the user pauses while typing,
// a new search can be performed.
//
// {}TODO: When the user presses ENTER/RETURN the search should happened
// immediately.
//
void winMain::on_txtSearch_changed()
{
	// Need only a single (static) copy of the timeout connection.
	// The search box changes often as the user types in
	// information.
	// {}TODO: Mayby 'myconnection' needs to be class global so
	// that other methods in the class can control the timeout.

	static SigC::Connection myconnection;
	
	// If the user types again before the timeout, disconnect the
	// callback, otherwise the program will do a new search with
	// every keystroke!
	if (myconnection.connected())
	{
		myconnection.disconnect();
	}
	
	// Set the timeout {}TODO: '1500' is 1.5 seconds and needs to
	// be a program parameter.
	myconnection = Glib::signal_timeout().connect(slot(*this,
&winMain::on_txtSearch_timeout), 1500);
}

//
// The timeout happened.  Get the text and run the search.
//
bool winMain::on_txtSearch_timeout()
{
	cout << "on_txtSearch_timeout()\n";
	
	cout << "Searching for: '" << txtSearch->get_text() << "'" << endl;
	
	return false; // to disconnect this callback.
}






[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]