Page 1 of 2 12 LastLast
Results 1 to 20 of 29

Thread: Passing Pixmaps between MainWindow and Dialog

  1. #1
    Join Date
    Dec 2007
    Posts
    32
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Passing Pixmaps between MainWindow and Dialog

    Good day people.

    Im an undergraduate student whose thesis is an Image Viewer to be used for Plant Disease Assessment. I am using Qt4 for this project.

    I would like to ask about how to pass QImage objects from the MainWindow to a dialog. Specifically, i would like to display the current QImage object from my main window to a brightness control dialog (to be shown as a preview).

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Passing Pixmaps between MainWindow and Dialog

    Provide a custom method in the dialog class taking a pixmap as an argument and make the method assign the image to the label (or other widget) used to display it.

  3. #3
    Join Date
    Dec 2007
    Posts
    32
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Passing Pixmaps between MainWindow and Dialog

    Quote Originally Posted by wysota View Post
    Provide a custom method in the dialog class taking a pixmap as an argument and make the method assign the image to the label (or other widget) used to display it.
    Im so sorry wysota, but I dont quite get what you are suggesting.

    Are you suggesting that I code a function that accepts pixmaps (or has another QImage object that would hold the copy of the object in the main window?), and then put the "clone" pixmap to the Image preview label in the dialog?

    Please bear with me..

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Passing Pixmaps between MainWindow and Dialog

    Qt Code:
    1. class MyDialog : public QDialog {
    2. Q_OBJECT
    3. public:
    4. MyDialog(QWidget *parent=0) : QDialog(parent){
    5. QVBoxLayout *l = new QVBoxLayout(this);
    6. label = new QLabel;
    7. l->addWidget(label);
    8. }
    9. void setPixmap(const QPixmap &px){
    10. label->setPixmap(px);
    11. }
    12. private:
    13. QLabel *label;
    14. };
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Dec 2007
    Posts
    32
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Passing Pixmaps between MainWindow and Dialog

    Hello wysota,

    Thanks for replying. I tried the above code, and in addition i used the below code when calling the dialog:

    Qt Code:
    1. void MainWindow::show_brightnessDialog()
    2. {
    3. brDialog *dialog = new brDialog(this);
    4. dialog->show();
    5. dialog->setPixmap(QPixmap::fromImage(image));
    6. }
    To copy to clipboard, switch view to plain text mode 

    It compiled successfully then I tried to load a jpeg image. When I invoke the brDialog, the whole program quits to the desktop without any error. How come?

    I also forgot to mention that the image preview label (prevLabel) is a tad small and i want the image to fit inside so should I add a QScrollArea widget to hold the QLabel prevLabel?

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Passing Pixmaps between MainWindow and Dialog

    You probably have an invalid pointer somewhere. Use a debugger to find out.

  7. #7
    Join Date
    Dec 2007
    Posts
    32
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Passing Pixmaps between MainWindow and Dialog

    Could the QImage image object be the problem?

    I think after I load the file using the below code, the image object somehow gets destroyed after the function. What do you think?

    Qt Code:
    1. void MainWindow::open()
    2. {
    3. QString fileName = QFileDialog::getOpenFileName(this,tr("Open File"), QDir::currentPath(), tr("Image Files (*.jpg *.jpeg *.bmp *.gif *.png)"));
    4. if (!fileName.isEmpty()) {
    5. QImage image(fileName);
    6. if (image.isNull()) {
    7. QMessageBox::critical(this, tr("Image View"), tr("Cannot load %1.").arg(fileName));
    8. return;
    9. }
    10. imageLabel->setPixmap(QPixmap::fromImage(image));
    11. scaleFactor = 1.0;
    12. closeAction->setEnabled(true);
    13. imageLabel->adjustSize();
    14.  
    15. imagelabFile.convertToRGBImage(image); //If loading is ok, create an imagelabfile copy of the QImage
    16. brightnessAction->setEnabled(true);
    17. QMessageBox::information(this, tr("Successful Conversion"),tr("ImageLab image loaded. Ready for processing."));
    18. }
    19. }
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Passing Pixmaps between MainWindow and Dialog

    Quote Originally Posted by ramstormrage View Post
    Could the QImage image object be the problem?
    I don't think so.

    I think after I load the file using the below code, the image object somehow gets destroyed after the function. What do you think?
    I'd say imageLabel might be NULL or uninitialized.

    I really suggest to use a debugger to find out. Why guess if you can check it out?

  9. #9
    Join Date
    Dec 2007
    Posts
    32
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Passing Pixmaps between MainWindow and Dialog

    Sorry but Im not familiar with using a debugger for a Qt application. Could you kindly walk me through?

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Passing Pixmaps between MainWindow and Dialog

    Are you familiar with using a debugger for a non-Qt application? It's exactly the same...

  11. #11
    Join Date
    Dec 2007
    Posts
    32
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Passing Pixmaps between MainWindow and Dialog

    Hello again wysota,

    Thanks for replying.

    Hmm.. No Im not familiar with any debugger.. I have gdb here, then I tried to run my release.. I proceeded up to the point where I invoke my brightnessDialog.. Then the expected happens, the program suddenly quits.. GDB reported that program exited with error code 01, meaning abnormal termination right?

    What should I be checking right now? Sorry for the troubles..

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Passing Pixmaps between MainWindow and Dialog

    Get a backtrace (bt command in gdb). Oh, and make sure the application is compiled in debug mode.

  13. #13
    Join Date
    Dec 2007
    Posts
    32
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Passing Pixmaps between MainWindow and Dialog

    Hello again wysota,

    Thanks for replying.
    I tried to get a backtrace but gdb told me there were 'No Backtrace.'. And yup, its already compiled in debug mode.

  14. #14
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Passing Pixmaps between MainWindow and Dialog

    In that case it probably didn't crash or you did something incorrectly. Could you explain step by step what you did and what results you get after each step starting from launching the application?

  15. #15
    Join Date
    Dec 2007
    Posts
    32
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Passing Pixmaps between MainWindow and Dialog

    Ok..Before I run gdb I navigate first to the directory where the program is located.

    1. In the console, I type gdb then proceed with "pwd" command to double check the working directory.
    <gdb> -> Working directory ....
    2. I type "file GUI.exe" then press return
    <gdb> -> Reading symbols from gui.exe .. done.
    3. I type "run" then press return
    <gdb> -> Starting program ...
    4. In the program, I use the file dialog to select a jpeg image in My Documents folder.
    5. After loading, the picture, my program responds with a Info box.
    <gdb> -> Still blank..
    6. Then I use the Adjust Brightness option from the Tools Menu.
    7. Program quits unexpectedly.
    <gdb> -> Program exited with code 01.
    8. I type "bt".
    <gdb> -> No stack.

  16. #16
    Join Date
    Dec 2007
    Posts
    32
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Passing Pixmaps between MainWindow and Dialog

    I didnt get a segmentation fault btw..

  17. #17
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Passing Pixmaps between MainWindow and Dialog

    Quote Originally Posted by ramstormrage View Post
    I didnt get a segmentation fault btw..
    Yes, I can see that. My guess is your program asserted. It seems you didn't build it in debug mode (in terms of debugging info being embedded into your app) after all. Please double check that. You can set a breakpoint in the function you expect to cause problems and manually step through the code to see where it breaks.

  18. #18
    Join Date
    Dec 2007
    Posts
    32
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Passing Pixmaps between MainWindow and Dialog

    Hello again, Thanks for replying..
    Ok I'll try that..

    Anyways, my previous hunch was right, the QImage image object gets destroyed after the MainWindow:pen() function. In the showBrightnessDialog(), I tried to put another
    QImage image(fileName); and it proceeded and displayed the pixmap inside the label as planned.

    Just to need know why? Any ideas?

  19. #19
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Passing Pixmaps between MainWindow and Dialog

    If the image object was going out of scope, you wouldn't be able to compile the application. It's not a heap based object, it can't be deleted "somewhere in the meantime". The image might be null, but that by itself wouldn't cause your application to crash unless you tried to iterate its pixels or something.

  20. #20
    Join Date
    Dec 2007
    Posts
    32
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Passing Pixmaps between MainWindow and Dialog

    Quote Originally Posted by wysota View Post
    It seems you didn't build it in debug mode (in terms of debugging info being embedded into your app) after all. Please double check that.
    Hello again,
    I need help again on this one.
    If I need to build the application in debug mode, I should include -g flag during compilation right? But I let qmake do the compiling for me.

    This is how i compile my program in debug mode (after I Built Debug Libraries)
    qmake -project
    qmake GUI.pro "CONFIG+=debug"
    make

    Am I doing something wrong?

Similar Threads

  1. Communication between MainWindow and a dialog
    By Backslash in forum Newbie
    Replies: 9
    Last Post: 3rd August 2007, 05:27
  2. Replies: 2
    Last Post: 23rd May 2007, 04:51
  3. Opening a Dialog from a MainWindow FileMenu
    By nbkhwjm in forum Newbie
    Replies: 4
    Last Post: 17th April 2007, 13:24
  4. Replies: 3
    Last Post: 23rd July 2006, 19:02

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.