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. Dynamically created buttons and connecting signal to slot
Forum Updated to NodeBB v4.3 + New Features

Dynamically created buttons and connecting signal to slot

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 4 Posters 2.4k 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.
  • K Offline
    K Offline
    Kris Revi
    wrote on last edited by
    #1

    So i thought i was done with this! but then i forgot to connect button click to a slot!

    void MainWindow::updateImageFolder()
    {
        //assume the directory exists and contains some files grab every image file there
        QDir directory("./Matrix Images/32 x 32/");
        QStringList images = directory.entryList(QStringList() << "*.*" << "*.*",QDir::Files);
    
        QLayoutItem *child;
        while ((child = ui->GRID_IMAGEFOLDER_DISPLAY->takeAt(0)) != nullptr) {
            delete child->widget(); // delete the widget
            delete child;   // delete the layout item
        }
    
        signalMapper = new QSignalMapper(this);
    
        foreach(QString filename, images) {
    
            QPushButton *button = new QPushButton(this);
    
    
            connect(button, SIGNAL(clicked()), this, SLOT(selectedImageDisplay(filename)));
    
            button->setMinimumSize(32, 32);
            button->setMaximumSize(32, 32);
            button->setFixedSize(32, 32);
            button->setIcon(QIcon("./Matrix Images/32 x 32/" + filename));
            button->setIconSize(QSize(32, 32));
            button->setCursor(Qt::PointingHandCursor);
            button->setObjectName(filename);
    
            ui->GRID_IMAGEFOLDER_DISPLAY->addWidget(button);
    
        }
    }
    

    but i get this error

    QObject::connect: No such slot MainWindow::selectedImageDisplay(filename) in ..\NEONCONTROLLER\mainwindow.cpp:333
    QObject::connect:  (receiver name: 'MainWindow')
    

    and yes i have added void selectedImageDisplay(QString img); to mainwindow.h so :S

    JonBJ 1 Reply Last reply
    0
    • K Kris Revi

      So i thought i was done with this! but then i forgot to connect button click to a slot!

      void MainWindow::updateImageFolder()
      {
          //assume the directory exists and contains some files grab every image file there
          QDir directory("./Matrix Images/32 x 32/");
          QStringList images = directory.entryList(QStringList() << "*.*" << "*.*",QDir::Files);
      
          QLayoutItem *child;
          while ((child = ui->GRID_IMAGEFOLDER_DISPLAY->takeAt(0)) != nullptr) {
              delete child->widget(); // delete the widget
              delete child;   // delete the layout item
          }
      
          signalMapper = new QSignalMapper(this);
      
          foreach(QString filename, images) {
      
              QPushButton *button = new QPushButton(this);
      
      
              connect(button, SIGNAL(clicked()), this, SLOT(selectedImageDisplay(filename)));
      
              button->setMinimumSize(32, 32);
              button->setMaximumSize(32, 32);
              button->setFixedSize(32, 32);
              button->setIcon(QIcon("./Matrix Images/32 x 32/" + filename));
              button->setIconSize(QSize(32, 32));
              button->setCursor(Qt::PointingHandCursor);
              button->setObjectName(filename);
      
              ui->GRID_IMAGEFOLDER_DISPLAY->addWidget(button);
      
          }
      }
      

      but i get this error

      QObject::connect: No such slot MainWindow::selectedImageDisplay(filename) in ..\NEONCONTROLLER\mainwindow.cpp:333
      QObject::connect:  (receiver name: 'MainWindow')
      

      and yes i have added void selectedImageDisplay(QString img); to mainwindow.h so :S

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @Kris-Revi said in Dynamically created buttons and connecting signal to slot:

      connect(button, SIGNAL(clicked()), this, SLOT(selectedImageDisplay(filename)));

      To the best of my knowledge you need the parameter type not name in these macros?
      connect(button, SIGNAL(clicked()), this, SLOT(selectedImageDisplay(QString)));

      However, if you would please move over to the new style signal/slot syntax instead of these macros your QoL (& ours) would be better!

      Even when you have done that, you will find you can't do it like you're trying. You are going to need to use a C++ lambda here for the slot so as to pass the filename parameter from the local variable, because the clicked() signal does not pass such a string. Start from e.g.
      https://forum.qt.io/topic/96163/lambda-that-uses-signal-argument-to-connect-to-a-slot
      https://artandlogic.com/2013/09/qt-5-and-c11-lambdas-are-your-friend/amp/
      https://medium.com/genymobile/how-c-lambda-expressions-can-improve-your-qt-code-8cd524f4ed9f

      K 1 Reply Last reply
      2
      • JonBJ JonB

        @Kris-Revi said in Dynamically created buttons and connecting signal to slot:

        connect(button, SIGNAL(clicked()), this, SLOT(selectedImageDisplay(filename)));

        To the best of my knowledge you need the parameter type not name in these macros?
        connect(button, SIGNAL(clicked()), this, SLOT(selectedImageDisplay(QString)));

        However, if you would please move over to the new style signal/slot syntax instead of these macros your QoL (& ours) would be better!

        Even when you have done that, you will find you can't do it like you're trying. You are going to need to use a C++ lambda here for the slot so as to pass the filename parameter from the local variable, because the clicked() signal does not pass such a string. Start from e.g.
        https://forum.qt.io/topic/96163/lambda-that-uses-signal-argument-to-connect-to-a-slot
        https://artandlogic.com/2013/09/qt-5-and-c11-lambdas-are-your-friend/amp/
        https://medium.com/genymobile/how-c-lambda-expressions-can-improve-your-qt-code-8cd524f4ed9f

        K Offline
        K Offline
        Kris Revi
        wrote on last edited by
        #3

        @JonB ooh noo... lambda in C++ is a pain compared to python version :|

        btw i'll switch to the new connect()

        JonBJ 1 Reply Last reply
        0
        • K Kris Revi

          @JonB ooh noo... lambda in C++ is a pain compared to python version :|

          btw i'll switch to the new connect()

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @Kris-Revi
          I have just appended some reference links for the lambda stuff to my previous post.

          K 1 Reply Last reply
          0
          • JonBJ JonB

            @Kris-Revi
            I have just appended some reference links for the lambda stuff to my previous post.

            K Offline
            K Offline
            Kris Revi
            wrote on last edited by
            #5

            @JonB so i got it working

            connect(button, &QPushButton::clicked, [=]() { selectedImageDisplay("./Matrix Images/32 x 32/" + filename); });
            

            having 1 problem tho

            void MainWindow::selectedImageDisplay(QString img)
            {
                imageObject = new QImage();           // Make a new imageObject
                imageObject->load(img);          // Load the image from path
            
                QJsonArray RGB565;
                for(int y = 0; y < imageObject->height(); y++) {
                    const quint16 *line = reinterpret_cast<const quint16*>(imageObject->constScanLine(y));
                    for(int x = 0; x < imageObject->width(); x++)
                        RGB565 << *(line++);
                }
            
                socket.sendCommandStrip(QString("pixArt"), RGB565);
            }
            

            when it gets sent to the Matrix the pixels are out of order and the colors are way off for some reason :S can you spot why? cause i cant :S

            JonBJ Christian EhrlicherC 2 Replies Last reply
            0
            • K Kris Revi

              @JonB so i got it working

              connect(button, &QPushButton::clicked, [=]() { selectedImageDisplay("./Matrix Images/32 x 32/" + filename); });
              

              having 1 problem tho

              void MainWindow::selectedImageDisplay(QString img)
              {
                  imageObject = new QImage();           // Make a new imageObject
                  imageObject->load(img);          // Load the image from path
              
                  QJsonArray RGB565;
                  for(int y = 0; y < imageObject->height(); y++) {
                      const quint16 *line = reinterpret_cast<const quint16*>(imageObject->constScanLine(y));
                      for(int x = 0; x < imageObject->width(); x++)
                          RGB565 << *(line++);
                  }
              
                  socket.sendCommandStrip(QString("pixArt"), RGB565);
              }
              

              when it gets sent to the Matrix the pixels are out of order and the colors are way off for some reason :S can you spot why? cause i cant :S

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #6

              @Kris-Revi
              I believe you got that loop-packing code from @Bonnie in another thread, so he can come and sort it out ;-)

              While you wait for him/inspiration, do some debugging yourself: look at the first so-many byte values sent, compare against the values of those received, is there a difference-pattern? Do you even end up with same number received as sent? Your problem might be in retrieving them, packing/unpacking them, or JSONifying them.

              [P.S. You had a QSignalMapper earlier: if you're going to go down lambda route (much more powerful) you can probably get rid of that.]

              K 2 Replies Last reply
              1
              • JonBJ JonB

                @Kris-Revi
                I believe you got that loop-packing code from @Bonnie in another thread, so he can come and sort it out ;-)

                While you wait for him/inspiration, do some debugging yourself: look at the first so-many byte values sent, compare against the values of those received, is there a difference-pattern? Do you even end up with same number received as sent? Your problem might be in retrieving them, packing/unpacking them, or JSONifying them.

                [P.S. You had a QSignalMapper earlier: if you're going to go down lambda route (much more powerful) you can probably get rid of that.]

                K Offline
                K Offline
                Kris Revi
                wrote on last edited by
                #7

                @JonB that's true! i googled and got som sugestion for my problem but yea! :) removed now!

                1 Reply Last reply
                0
                • JonBJ JonB

                  @Kris-Revi
                  I believe you got that loop-packing code from @Bonnie in another thread, so he can come and sort it out ;-)

                  While you wait for him/inspiration, do some debugging yourself: look at the first so-many byte values sent, compare against the values of those received, is there a difference-pattern? Do you even end up with same number received as sent? Your problem might be in retrieving them, packing/unpacking them, or JSONifying them.

                  [P.S. You had a QSignalMapper earlier: if you're going to go down lambda route (much more powerful) you can probably get rid of that.]

                  K Offline
                  K Offline
                  Kris Revi
                  wrote on last edited by
                  #8

                  @JonB i fixed it! so i mentioned that the colors was off and pixel order so i thought why not just try

                  *imageObject = imageObject->convertToFormat(QImage::Format_RGB16);
                  

                  and that was the problem :) even if i saved the image as RGB16 upen uploading it and converting it it was not saved as RGB16 apparently

                  1 Reply Last reply
                  1
                  • K Kris Revi

                    @JonB so i got it working

                    connect(button, &QPushButton::clicked, [=]() { selectedImageDisplay("./Matrix Images/32 x 32/" + filename); });
                    

                    having 1 problem tho

                    void MainWindow::selectedImageDisplay(QString img)
                    {
                        imageObject = new QImage();           // Make a new imageObject
                        imageObject->load(img);          // Load the image from path
                    
                        QJsonArray RGB565;
                        for(int y = 0; y < imageObject->height(); y++) {
                            const quint16 *line = reinterpret_cast<const quint16*>(imageObject->constScanLine(y));
                            for(int x = 0; x < imageObject->width(); x++)
                                RGB565 << *(line++);
                        }
                    
                        socket.sendCommandStrip(QString("pixArt"), RGB565);
                    }
                    

                    when it gets sent to the Matrix the pixels are out of order and the colors are way off for some reason :S can you spot why? cause i cant :S

                    Christian EhrlicherC Online
                    Christian EhrlicherC Online
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @Kris-Revi said in Dynamically created buttons and connecting signal to slot:

                    imageObject = new QImage();

                    You leak this image object. Better create it on the stack.

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    K 1 Reply Last reply
                    4
                    • Christian EhrlicherC Christian Ehrlicher

                      @Kris-Revi said in Dynamically created buttons and connecting signal to slot:

                      imageObject = new QImage();

                      You leak this image object. Better create it on the stack.

                      K Offline
                      K Offline
                      Kris Revi
                      wrote on last edited by
                      #10

                      @Christian-Ehrlicher leak? stack what now?

                      JonBJ 1 Reply Last reply
                      0
                      • K Kris Revi

                        @Christian-Ehrlicher leak? stack what now?

                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on last edited by JonB
                        #11

                        @Kris-Revi
                        "leak" means the memory used by your new QImage() is never returned to the pool. Either delete it at the end, or @Christian-Ehrlicher is suggesting easiest is make it a QImage imageObject; variable here, which means it is on the "stack" (instead of the "heap" for newed objects). Then it gets thrown away at end of the method for sure.

                        K 1 Reply Last reply
                        2
                        • JonBJ JonB

                          @Kris-Revi
                          "leak" means the memory used by your new QImage() is never returned to the pool. Either delete it at the end, or @Christian-Ehrlicher is suggesting easiest is make it a QImage imageObject; variable here, which means it is on the "stack" (instead of the "heap" for newed objects). Then it gets thrown away at end of the method for sure.

                          K Offline
                          K Offline
                          Kris Revi
                          wrote on last edited by
                          #12

                          @JonB so make a QImage imageObject; not imageObject = new QImage() ?,

                          mrjjM 1 Reply Last reply
                          0
                          • K Kris Revi

                            @JonB so make a QImage imageObject; not imageObject = new QImage() ?,

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

                            @Kris-Revi
                            Yes exactly.

                            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