Writing static method returning an input from the user [solved]
-
wrote on 25 Oct 2011, 13:48 last edited by
Dear all
I would like to write a static method which returns an input selected by the user, the same way QFileDialog::getExistingDirectory(...) and other static methods works. More precisely, here is (a snapshot) of what I want to do.
I want to display an image. The user then double clicks on a point in the image, and I want the pixel value under the point where the mouse has clicked. So basically I want this method
[code]static quint8 getPixelInImage(const QImage & img)[/code]
I already have a derivation of an ImageDisplay class (given in example on this site) and a PixelSelectorLabel which overloads the mouseDoubleClickEvent( QMouseEvent * event ) method.
I can quite easily create a short program that opens a picture, shows it to me, then I can double-click on it and it shows me the value of the pixel I got. However, I am still confused about how a static function which does the same thing should be written.
Here is a bit of (simplified) architecture I have
[code]Class ImageDisplay : public QMainWindow
{
...
public :
quint8 value ;
bool done ;
public slot :
void open(const QImage & img) { ... }
void catchValue(quint8 & v) { value = v ; done = true ; emit finished() }
...
signals:
void finished() ;
...
static quint8 getPixelInImage(const QImage & img)
{
PixelSelectorLabel * selector = new PixelSelectorLabel() ;
ImageDisplay display(selector, img) // builds the display with the label and opens the picture
connect(selector, SIGNAL(sendValue(quint8 &)), &display, SLOT(catchValue(quint8 &))) ;
connect(&display, SIGNAL(finished()), &display, SLOT(hide())) ;
return display.value ;
} ;
[/code][code]Class PixelSelectorLabel : public QLabel
{
...
protected slots :
void mouseDoubleClickEvent ( QMouseEvent * event ) { ... ; emit sentValue(pixel) ; } // where pixel is the value selected in the image after double-clicking
signals :
void sendValue(quint8 &) ;
}[/code]I think that I am missing something somewhere, but I don't see how to connect the signals so that everything is fine... any idea ?
-
wrote on 25 Oct 2011, 15:38 last edited by
The static functions in the vain of QFileDialog::getOpenFileName() use a local event loop, no signals or slots.
@
static quint8 pixelInImage(const QImage &img)
{
YourDialog dlg;
// do some preparation
if (dlg.exec() == QDialog::Accepted)
return dlg.thePixel();
else
return 0;
}
@Note that this approach can cause "odd behavior":http://labs.qt.nokia.com/2010/02/23/unpredictable-exec/ if an event loop is running inside of another one.
-
wrote on 25 Oct 2011, 16:39 last edited by
Hello ! I struggled a little bit to understand how a QDialog works, but I have managed finally to do it. Thanks for the help !
1/3