Results 1 to 20 of 29

Thread: Passing Pixmaps between MainWindow and Dialog

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 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,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 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,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 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,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 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,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 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..

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.