Results 1 to 4 of 4

Thread: Loading image from subclass QGraphicsview

  1. #1
    Join Date
    Jun 2013
    Posts
    46
    Thanks
    24
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Windows

    Question Loading image from subclass QGraphicsview

    How do I upload an image to a graphicsview from a subclass of QGraphicsview

    I created new class "TestGV" as a child class of QGraphicsView
    I promoted my graphicsView widget on form to "TestGV" class

    Where do I create the scene in the mainwindow class or the "TestGV" class
    This is what i got, but does not work
    Qt Code:
    1. definitions in public "TesGv Class"
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. //In mainWindow Class -> PushButton "Upload Image"
    2. ui->graphicsView->getImageFileName(); // call function in subclass to get and set image
    3. ui->graphicsView->scene = new QGraphicsScene(this);
    4. ui->graphicsView->setScene(ui->graphicsView->scene)
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. //In subClass "TestGV" "getImageFileName
    2. QFileDialog dialog(this, "Upload");
    3. dialog.setNameFilter(tr("Images (*.png *.jpg *.bmp *)"));
    4. dialog.setViewMode(QFileDialog::Detail);
    5. QString imageFileName1;
    6. if(dialog.exec())
    7. {
    8. imageFileName1 = dialog.selectedFiles().first();
    9. }
    10.  
    11. image1.load(imageFileName1);
    12. image1 = image1.convertToFormat(QImage::Format_RGB32);
    13.  
    14. QRgb colour;
    15. for (int f1=0; f1<image1.width(); f1++) {
    16. for (int f2=0; f2<image1.height(); f2++) {
    17. colour = image1.pixel(f1, f2);
    18. image1.setPixel(f1, f2, QColor((qRed(colour) + qGreen(colour) + qBlue(colour))/3).rgb());
    19. }
    20. }
    21. QPixmap pixmap;
    22. pixmap.fromImage(image1);
    23. scene = new QGraphicsScene(this);
    24. newImage = scene->addPixmap(pixmap);
    To copy to clipboard, switch view to plain text mode 

    The above code does display Dialogbox & user can select image of choice
    But nothing is uploaded
    How do I set the scene from the subClass "testGV"
    And do the uploading also from "TestGV"

    Kindly provide me with example code

    Kind Regards

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Loading image from subclass QGraphicsview

    Qt Code:
    1. //In mainWindow Class -> PushButton "Upload Image"
    2. ui->graphicsView->scene = new QGraphicsScene(this);
    3. ui->graphicsView->setScene(ui->graphicsView->scene)
    4. ui->graphicsView->getImageFileName(); // call function in subclass to get and set image
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. //In subClass "TestGV" "getImageFileName
    2. QFileDialog dialog(this, "Upload");
    3. dialog.setNameFilter(tr("Images (*.png *.jpg *.bmp *)"));
    4. dialog.setViewMode(QFileDialog::Detail);
    5. QString imageFileName1;
    6. if(dialog.exec())
    7. {
    8. imageFileName1 = dialog.selectedFiles().first();
    9. }
    10.  
    11. image1.load(imageFileName1);
    12. image1 = image1.convertToFormat(QImage::Format_RGB32);
    13.  
    14. QRgb colour;
    15. for (int f1=0; f1<image1.width(); f1++) {
    16. for (int f2=0; f2<image1.height(); f2++) {
    17. colour = image1.pixel(f1, f2);
    18. image1.setPixel(f1, f2, QColor((qRed(colour) + qGreen(colour) + qBlue(colour))/3).rgb());
    19. }
    20. }
    21.  
    22. QPixmap pixmap;
    23. pixmap.fromImage(image1);
    24. //scene = new QGraphicsScene(this);
    25. newImage = scene->addPixmap(pixmap);
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. The following user says thank you to Santosh Reddy for this useful post:

    2lights (26th August 2013)

  4. #3
    Join Date
    Jun 2013
    Posts
    46
    Thanks
    24
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Windows

    Thumbs up Re: Loading image from subclass QGraphicsview

    Thanks,


    Based on QDebug output on width & height of image
    my QImage works
    my QPixmap does not get initialised at all
    Qt Code:
    1. pixmap.fromImage(image1);
    To copy to clipboard, switch view to plain text mode 


    Loading from QImage directly... works
    Qt Code:
    1. newImage = scene->addPixmap(QPixmap::fromImage(image1));
    To copy to clipboard, switch view to plain text mode 
    not sure if i'll need Qpixmap later though
    Would like to know why QPixmap is not working accordingly


    Kind Regards
    Thanks
    Last edited by 2lights; 26th August 2013 at 09:09.

  5. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Loading image from subclass QGraphicsview

    fromImage() is a static member function, you need to copy the converted pixmap into a variable (you might have ignored the compiler warning, or may be even disabled them). Anyways this will work
    Qt Code:
    1. QPixmap pixmap;
    2. pixmap = pixmap.fromImage(image1);
    To copy to clipboard, switch view to plain text mode 
    and even better is this
    Qt Code:
    1. QPixmap pixmap;
    2. pixmap = QPixmap::fromImage(image1);
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  6. The following user says thank you to Santosh Reddy for this useful post:

    2lights (26th August 2013)

Similar Threads

  1. Replies: 4
    Last Post: 15th February 2013, 10:55
  2. Loading Image
    By navi1084 in forum Qt Programming
    Replies: 2
    Last Post: 27th June 2011, 05:07
  3. Loading a raw image into QImage
    By Luc4 in forum Qt Programming
    Replies: 6
    Last Post: 16th May 2010, 15:20
  4. QGraphicsItem subclass access to QGraphicsView size
    By rubenvb in forum Qt Programming
    Replies: 4
    Last Post: 23rd January 2010, 21:36
  5. Image Loading
    By kavinsiva in forum Qt Programming
    Replies: 2
    Last Post: 11th August 2009, 08:00

Tags for this Thread

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.