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. [SOLVED]convert const QImage & to QImage *
Forum Updated to NodeBB v4.3 + New Features

[SOLVED]convert const QImage & to QImage *

Scheduled Pinned Locked Moved General and Desktop
10 Posts 6 Posters 9.9k Views 1 Watching
  • 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.
  • E Offline
    E Offline
    endhck
    wrote on last edited by
    #1

    I want to use this function:
    @myLabel->setPixmap(QPixmap::fromImage(myImage));@

    In my header file I created @QImage *myImage;@
    and, in cpp file I initialized it @myImage = new QImage(900, 900, QImage::Format_RGB32);@
    It is necessary because the size of image is not always 900x900. The size is taken from a file.

    fromImage() function takes a parameter in const QImage & type, but in my program it must be QImage *, as I said above.
    I used like this
    @myLabel->setPixmap(QPixmap::fromImage(&myImage))@
    this code does not give any build error, but during execution gives access violation error.

    How can I use it, or how can convert?

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dangelog
      wrote on last edited by
      #2

      But why are you using a pointer to a QImage? And that code SHOULD give you an error, since the type passed to fromImage is "QImage **" which of course is not compatible with a "const QImage &".

      Software Engineer
      KDAB (UK) Ltd., a KDAB Group company

      1 Reply Last reply
      0
      • V Offline
        V Offline
        vsorokin
        wrote on last edited by
        #3

        Are you sure that you initialize myImage pointer strongly before call QPixmap::fromImage?
        Also, are you sure myLabel already created?
        You can start app under debugger and looking what happened.

        --
        Vasiliy

        1 Reply Last reply
        0
        • JohanSoloJ Offline
          JohanSoloJ Offline
          JohanSolo
          wrote on last edited by
          #4

          I think you could read directly the file you want in the QImage using something like "this":http://doc.trolltech.com/4.7/qimage.html#QImage-9.

          `They did not know it was impossible, so they did it.'
          -- Mark Twain

          1 Reply Last reply
          0
          • E Offline
            E Offline
            endhck
            wrote on last edited by
            #5

            because, I created it in header file and initialized in cpp file.
            @QImage *myImage;@ // in header file
            @myimage = new QImage(900, 900, QImage::Format_RGB32);@ // in cpp file

            I tried to write
            @QImage myImage;@ instead of @QImage myImage;@
            but it gives error because of initialization.

            Is there any way of using it without pointer?

            1 Reply Last reply
            0
            • G Offline
              G Offline
              giesbert
              wrote on last edited by
              #6

              [quote author="peppe" date="1312980799"]But why are you using a pointer to a QImage? And that code SHOULD give you an error, since the type passed to fromImage is "QImage **" which of course is not compatible with a "const QImage &".[/quote]

              so the code must be then:

              @
              myLabel->setPixmap(QPixmap::fromImage(*myImage))
              @

              A reference requires an object not a pointer.

              @
              class X* p; // is a pointer
              @

              To dereference a pointer to an object, you must put a star in front:

              @
              class X {...};

              X* p;
              X obj;
              obj = *p;
              X& ref1 = obj; // correct, reference to obejct
              X& ref2 = p; // correct, reference to dereferenced pointer
              X& ref3 = p; // error, as the reference is not a pointer!
              X
              & ref4 = p; // correct, as the reference references a pointer to X --> modifying ref4 modifies p, not the object behind p!
              @

              Nokia Certified Qt Specialist.
              Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

              1 Reply Last reply
              0
              • JohanSoloJ Offline
                JohanSoloJ Offline
                JohanSolo
                wrote on last edited by
                #7

                [quote author="endhck" date="1312981242"]Is there any way of using it without pointer?[/quote]

                It's hard to say whether you really need a pointer or not from the lines you gave us... A priori you don't need one.

                `They did not know it was impossible, so they did it.'
                -- Mark Twain

                1 Reply Last reply
                0
                • E Offline
                  E Offline
                  endhck
                  wrote on last edited by
                  #8

                  yes, it is my fault.
                  I written this part as
                  @myLabel->setPixmap(QPixmap::fromImage(*myImage))@

                  It does not give build error, it gives error during execution "Access violation"

                  It show me, the error is in that part:
                  @bool QCoreApplicationPrivate::sendThroughObjectEventFilters(QObject *receiver, QEvent *event)
                  {
                  Q_Q(QCoreApplication);
                  if (receiver != q) {
                  for (int i = 0; i < receiver->d_func()->eventFilters.size(); ++i) { // in this part
                  register QObject *obj = receiver->d_func()->eventFilters.at(i);
                  if (!obj)
                  continue;
                  if (obj->d_func()->threadData != receiver->d_func()->threadData) {
                  qWarning("QCoreApplication: Object event filter cannot be in a different thread.");
                  continue;
                  }
                  if (obj->eventFilter(receiver, event))
                  return true;
                  }
                  }
                  return false;
                  }@

                  1 Reply Last reply
                  0
                  • E Offline
                    E Offline
                    endhck
                    wrote on last edited by
                    #9

                    Thanks guys,

                    the problem was not about the QImage.
                    I think it is an initialization problem, firstly I created the label and the image in the constructor,
                    after I filled the image and called the setPixmap function. It gave me error.
                    Now, I created the label and the image, and also called setPixmap function in the constructor.
                    After I filled the image and called setPixmap again. It works correctly.

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      mlong
                      wrote on last edited by
                      #10

                      Be sure and change the title of the thread to add [Solved]. Thanks!

                      Software Engineer
                      My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

                      1 Reply Last reply
                      0

                      • Login

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