David Dibben | 12 May 11:30 2004
Picon

Re: [TFUI] testing Qt code


On Wed, 12 May 2004 00:46:05 -0700 (PDT)
Phlip <phlipcpp@...> wrote:
> > reveal.  Calling processEvents() means  that no user
> > interaction with the
> > window is possible, only viewing.
> Thanks! This would soon appear, as-is, on its
> appropriate Wiki.

I am afraid that the version I posted only partially works. The
processEvents call only processes events that have already been sent, so
if displaying the window generates other events (which it will) then those
events won't get processed by the call. One solution is to call
processEvents several times in a row in the reveal function.

 
> Can you run processEvents in a loop?

Yes. In fact this is its main use. The documented reason that 
processEvents exists is so that you can call it periodically during a long
calculation to allow the gui to be refeshed or allow the user to cancel
the calculation without having to resort to running the calculation in a
separate thread. Something like:

  while (stuff_still_to_do) {
      do_some_stuff();
      qApp->processEvents();
  }

>  - Temporary Interactive Test
> 
> That means, while the window is revealed, you click on
> it and see what happens. Maybe you invoke a breakpoint
> or a trace statement.

OK, a first attempt at this could look something like below. This code
could almost certainly do with some improvement but it shows one way of
doing it.

After showing the window a local event loop is entered using
qApp->enter_loop(); This allows full interaction to take place with the
revealed window. To stop the interaction we need to call
qApp->exit_loop();
so the code below displays a second small dialog with a single push button
which when pressed stops the interaction.

Of course, if you prefer to stop the interaction by killing the program
you can simplify the code by just  calling either qApp->enter_loop(); or
qApp->exec() after showing the window. In this case you don't need to
bother with the stop dialog. However, if you wanted to place tests _after_
the call to reveal then clearly this won't be a suitable solution.

#include <qdialog.h>
#include <qapplication.h>
#include <qpushbutton.h>
#include <qlayout.h>

// a simple dialog with a single push button.
class StopDialog : public QDialog
{
	Q_OBJECT
public:
		
	StopDialog();
				
public slots:
	
	void	startInteration();				
	void 	stopInteraction();
				
private:					
	
	QPushButton*	fStopButton;	
	bool			fInLoop;	
					
};

StopDialog::StopDialog()
{
	// create a pushbutton to stop the interaction
	fStopButton = new QPushButton(this);
	fStopButton->setText("Stop Interaction");

	// Qt uses layouts to position the widgets
	QHBoxLayout* layout = new QHBoxLayout(this);
	layout->addWidget(fStopButton);
	
	// call the stopInteraction when the button is clicked
	connect(fStopButton, SIGNAL(clicked()), this, SLOT(stopInteraction()));
	
	fInLoop = false;
}
	
void StopDialog::stopInteraction()
{
	if (fInLoop == true) {
		// exit the local event loop
		qApp->exit_loop();
		fInLoop = false;
	}
}

void StopDialog::startInteration()
{
	if (fInLoop == false) {
		fInLoop = true;
		// enter a local event loop
		qApp->enter_loop();
	}
}

void revealInteractive(QWidget* widget)
{
	// show the widget
	widget->show();
	
	// create a "stop" dialog box
	StopDialog dialog;
	// show the dialog
	dialog.show();
	// start interaction. This function call will only return when
	// stopInteraction has been called
	dialog.startInteration();
}

int main(int argc, char** argv)
{
	QApplication app(argc, argv);
	
	//create an empty dialog object
	QDialog* dialog = new QDialog();
	
	// reveal the dialog
	revealInteractive(dialog);	
		
	
	return EXIT_SUCCESS;
}

// everything in this example is in one file so we we need this line for
// Qt's MOC system to work.
#include "reveal.moc"

------------------------ Yahoo! Groups Sponsor ---------------------~-->
Yahoo! Domains - Claim yours for only $14.70
http://us.click.yahoo.com/Z1wmxD/DREIAA/yQLSAA/nhFolB/TM
---------------------------------------------------------------------~->

To unsubscribe, email:
TestFirstUserInterfaces-unsubscribe@...

Yahoo! Groups Links

<*> To visit your group on the web, go to:
     http://groups.yahoo.com/group/TestFirstUserInterfaces/

<*> To unsubscribe from this group, send an email to:
     TestFirstUserInterfaces-unsubscribe@...

<*> Your use of Yahoo! Groups is subject to:
     http://docs.yahoo.com/info/terms/


Gmane