Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Writing static method returning an input from the user [solved]
Forum Update on Monday, May 27th 2025

Writing static method returning an input from the user [solved]

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 1.9k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    agiorla
    wrote on 25 Oct 2011, 13:48 last edited by
    #1

    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 ?

    1 Reply Last reply
    0
    • F Offline
      F Offline
      Franzk
      wrote on 25 Oct 2011, 15:38 last edited by
      #2

      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.

      "Horse sense is the thing a horse has which keeps it from betting on people." -- W.C. Fields

      http://www.catb.org/~esr/faqs/smart-questions.html

      1 Reply Last reply
      0
      • A Offline
        A Offline
        agiorla
        wrote on 25 Oct 2011, 16:39 last edited by
        #3

        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 Reply Last reply
        0

        1/3

        25 Oct 2011, 13:48

        • Login

        • Login or register to search.
        1 out of 3
        • First post
          1/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved