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. Passing QPixmap Object Between Main and Sub Forms

Passing QPixmap Object Between Main and Sub Forms

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 916 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.
  • R_IrudezuR Offline
    R_IrudezuR Offline
    R_Irudezu
    wrote on last edited by
    #1

    I saw some samples but connect() mechanism looked weird a bit to me. I'm trying to understand it.

    Currently I have a QLabel in MainWindow, and it has a QPixmap (covered with image).

    Let's say there is a button and it has button_clicked() event. When button clicked, it should pass MainWindow's QLabel to sub form. So i need to pass this QLabel(as image) to another form and show it in another form's QLabel (as image).

    I think it's a good way to use connect(), SLOT & SIGNAL mechanism, like i said i'm trying to understand it. Or maybe there's a better way to do pass QLabel to another form. I'm waiting for your helpful and explanatory answers.

    Keizoku wa chikaranari.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Something is not clear, do you want to move the QLabel around or the image it contains ? If the later, where does that image come from ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      R_IrudezuR 1 Reply Last reply
      1
      • SGaistS SGaist

        Hi,

        Something is not clear, do you want to move the QLabel around or the image it contains ? If the later, where does that image come from ?

        R_IrudezuR Offline
        R_IrudezuR Offline
        R_Irudezu
        wrote on last edited by
        #3

        @SGaist second one, the image comes from a database (blob data).

        At Main Form:

        //strQuery contains how i get blob data from a local Database
        QByteArray outByteArray = strQuery.value(0).toByteArray();
        QPixmap outPixmap = QPixmap();
        outPixmap.loadFromData( outByteArray );
        QPixmap pixmap = outPixmap;
        
        QImage image;
        image = image.scaledToWidth(ui->QLabel1->width(), Qt::SmoothTransformation);
        ui->QLabel1->QLabel::setPixmap(pixmap);
        //until here, i loaded blob data to a QLabel
        
        

        The QLabel shows blob data, it's OK. Now i want to show same blob data in other form. Yes i can do with the same way, get from db again but that's not way i want.

        I just need to show QLabel's image in sub form. Did i explain well?

        Keizoku wa chikaranari.

        mrjjM 1 Reply Last reply
        0
        • R_IrudezuR R_Irudezu

          @SGaist second one, the image comes from a database (blob data).

          At Main Form:

          //strQuery contains how i get blob data from a local Database
          QByteArray outByteArray = strQuery.value(0).toByteArray();
          QPixmap outPixmap = QPixmap();
          outPixmap.loadFromData( outByteArray );
          QPixmap pixmap = outPixmap;
          
          QImage image;
          image = image.scaledToWidth(ui->QLabel1->width(), Qt::SmoothTransformation);
          ui->QLabel1->QLabel::setPixmap(pixmap);
          //until here, i loaded blob data to a QLabel
          
          

          The QLabel shows blob data, it's OK. Now i want to show same blob data in other form. Yes i can do with the same way, get from db again but that's not way i want.

          I just need to show QLabel's image in sub form. Did i explain well?

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #4

          @R_Irudezu
          Hi
          So the sub form should also have a label to hold image (pixmap) as
          moving mainwindows actual label will be odd.
          Lets call it subforms label for sublabel.
          so you add a function to subform for setting it
          ( as the UI is private from outside)
          like
          void SubFormName::setImage(QPixmap pix) {
          ui->subLabel->setPixmap(pix);
          }

          and then when in Main form, where/when u show sub form
          SubForm * mysub=new SubForm(this);
          // we call our function to transfer image to sub form
          mysub->setImage( ui->QLabel1->pixmap() );
          mysub->show();

          note you need to delete subform again at some point!
          using a QDialog as subForm would make this easier if
          its ok, that mainwindow is blocked while sub form is open.

          R_IrudezuR 1 Reply Last reply
          2
          • mrjjM mrjj

            @R_Irudezu
            Hi
            So the sub form should also have a label to hold image (pixmap) as
            moving mainwindows actual label will be odd.
            Lets call it subforms label for sublabel.
            so you add a function to subform for setting it
            ( as the UI is private from outside)
            like
            void SubFormName::setImage(QPixmap pix) {
            ui->subLabel->setPixmap(pix);
            }

            and then when in Main form, where/when u show sub form
            SubForm * mysub=new SubForm(this);
            // we call our function to transfer image to sub form
            mysub->setImage( ui->QLabel1->pixmap() );
            mysub->show();

            note you need to delete subform again at some point!
            using a QDialog as subForm would make this easier if
            its ok, that mainwindow is blocked while sub form is open.

            R_IrudezuR Offline
            R_IrudezuR Offline
            R_Irudezu
            wrote on last edited by R_Irudezu
            #5

            @mrjj when i do

            mysub->setImage( ui->QLabel1->pixmap() );
            

            ui keyword is underlined and this error:

            no viable conversion from const QPixmap * to QPixmap

            I solved it with create a new const QPixmap *;

            const QPixmap *pixMap = ui->QLabel1->pixmap();
            //then
            mysub->setImage( ui->QLabel1->pixmap() );
            

            It's ok now. Many thanks @Lifetime-Qt-Champion

            Keizoku wa chikaranari.

            mrjjM 1 Reply Last reply
            0
            • R_IrudezuR R_Irudezu

              @mrjj when i do

              mysub->setImage( ui->QLabel1->pixmap() );
              

              ui keyword is underlined and this error:

              no viable conversion from const QPixmap * to QPixmap

              I solved it with create a new const QPixmap *;

              const QPixmap *pixMap = ui->QLabel1->pixmap();
              //then
              mysub->setImage( ui->QLabel1->pixmap() );
              

              It's ok now. Many thanks @Lifetime-Qt-Champion

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @R_Irudezu
              Ah sorry, my bad.
              I thought it would return a copy.
              but its
              const QPixmap * pixmap() const
              Should be fine as a copy is made so even if original image should
              be removed, the subForm has its own.

              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