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. Signal issues in QStyledItemDelegate
Forum Updated to NodeBB v4.3 + New Features

Signal issues in QStyledItemDelegate

Scheduled Pinned Locked Moved General and Desktop
18 Posts 6 Posters 4.0k Views 3 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.
  • V Offline
    V Offline
    vicky12
    wrote on last edited by
    #2

    Try To Give Connect Statement In CreateEditor Itself and Try.

    1 Reply Last reply
    0
    • G gabor53

      Hi,
      I have a QStyledItemDelegate where I use a button to replace image in the StandardItemModel.

      In createEditor I create the button like this:

       if((index.column () == 3))//image
              {
                  QPushButton *button = new QPushButton(parent) ;
                  button->setMaximumWidth (100);
                  return button;
              }
      

      In setEditorData I have

         QPushButton *button = qobject_cast<QPushButton*>(editor);
          if(button)
              {
                  button->setStyleSheet ("background-color: rgba(255, 255, 255, 0);");
      
                  connect(button, SIGNAL(clicked()),this,SLOT(getNewPix()));
      
                  QPixmap retPix;
                  qDebug() << "case 3 bytearray size: " << fixByteArray.size ();
      
                  if((retPix.loadFromData (fixByteArray)) == true)
                      {
                          qDebug() << "retPix loaded!";
                          qDebug() << "retPix sizse: " << retPix.size ();
                      }
                  else
                      {
                          qDebug() << "retPix is NOT loaded!";
                      }
                  return;
              }
      

      The problem is that when I click on the button the signal - slot line is disregarded and processing continues on the next line instead of going to getNewPix. Is this because it doesn't even notice that I clicked on the button, or the signal - slot line is incorrect? Thank you for your help.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #3

      @gabor53 You should check the return value of connect:

      qDebug() << connect(button, SIGNAL(clicked()),this,SLOT(getNewPix()));
      

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      G 1 Reply Last reply
      0
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by VRonin
        #4

        You are doing in the delegate stuff that should be done in the editor widget.
        create an image editor widget, put the button in there, move getNewPix in there and use the delegate just to create that widget and retrieve data from it

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        1 Reply Last reply
        2
        • G gabor53

          Hi,
          I have a QStyledItemDelegate where I use a button to replace image in the StandardItemModel.

          In createEditor I create the button like this:

           if((index.column () == 3))//image
                  {
                      QPushButton *button = new QPushButton(parent) ;
                      button->setMaximumWidth (100);
                      return button;
                  }
          

          In setEditorData I have

             QPushButton *button = qobject_cast<QPushButton*>(editor);
              if(button)
                  {
                      button->setStyleSheet ("background-color: rgba(255, 255, 255, 0);");
          
                      connect(button, SIGNAL(clicked()),this,SLOT(getNewPix()));
          
                      QPixmap retPix;
                      qDebug() << "case 3 bytearray size: " << fixByteArray.size ();
          
                      if((retPix.loadFromData (fixByteArray)) == true)
                          {
                              qDebug() << "retPix loaded!";
                              qDebug() << "retPix sizse: " << retPix.size ();
                          }
                      else
                          {
                              qDebug() << "retPix is NOT loaded!";
                          }
                      return;
                  }
          

          The problem is that when I click on the button the signal - slot line is disregarded and processing continues on the next line instead of going to getNewPix. Is this because it doesn't even notice that I clicked on the button, or the signal - slot line is incorrect? Thank you for your help.

          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #5

          @gabor53 said in Signal issues in QStyledItemDelegate:

          The problem is that when I click on the button the signal - slot line is disregarded and processing continues on the next line instead of going to getNewPix

          i actually don't understand this?
          What does getNewPix() actually do?

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          VRoninV G 2 Replies Last reply
          0
          • raven-worxR raven-worx

            @gabor53 said in Signal issues in QStyledItemDelegate:

            The problem is that when I click on the button the signal - slot line is disregarded and processing continues on the next line instead of going to getNewPix

            i actually don't understand this?
            What does getNewPix() actually do?

            VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by VRonin
            #6

            @raven-worx said in Signal issues in QStyledItemDelegate:

            What does getNewPix() actually do?

            If you check other topics it's basically a QFileDialog::getOpenFileName

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            1 Reply Last reply
            0
            • raven-worxR raven-worx

              @gabor53 said in Signal issues in QStyledItemDelegate:

              The problem is that when I click on the button the signal - slot line is disregarded and processing continues on the next line instead of going to getNewPix

              i actually don't understand this?
              What does getNewPix() actually do?

              G Offline
              G Offline
              gabor53
              wrote on last edited by
              #7

              @raven-worx
              It is supposed to get the file and convert it into a QPixmap.

              1 Reply Last reply
              0
              • jsulmJ jsulm

                @gabor53 You should check the return value of connect:

                qDebug() << connect(button, SIGNAL(clicked()),this,SLOT(getNewPix()));
                
                G Offline
                G Offline
                gabor53
                wrote on last edited by
                #8

                @jsulm
                I added

                qDebug() << "qDebug connect: " << connect(button, SIGNAL(clicked()),this,SLOT(getNewPix()));
                
                

                and the message was
                qDebug connect: true.

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  gabor53
                  wrote on last edited by gabor53
                  #9

                  I changed the connect like this:

                              QByteArray fixFile;
                              connect(button,&QPushButton::clicked, [=] ()
                              {
                                   fixFile = getNewPix ();
                              });
                  

                  I get a message immediately saying "member function getNewPix not viable: 'this' argument has type 'const myDelegate' but function is not marked const.

                  getNewPix looks like this:

                  const QByteArray myDelegate::getNewPix()
                  {
                      qDebug() << "Entered getNewPix!";
                      QString sPath = "C:/";
                  
                  //    fileDialog = getOpenFileName (Q_NULLPTR,sPath);
                  
                      fileName = QFileDialog::getOpenFileName(Q_NULLPTR,
                                                              tr("Finding Friend's replacement Image"),sPath, tr("Image Files (*.png *.jpg *.bmp)"));
                  
                  
                      if(fileName == "")
                          {
                              qDebug() << "No image was chosen.";
                  //            QMessageBox::warning (this,"Error 1039","No image was choosen!");
                          }
                      else
                          {
                              qDebug() << "Image was chosen!";
                          }
                  
                      QFile file(fileName);
                      if(file.open (QIODevice::ReadOnly))
                          {
                              qDebug() << "File  is open!";
                              fixByteArray = file.readAll (); //QByteArray
                              qDebug() << "Delegate file size: " << fixByteArray.size ();
                  
                          }
                      else
                          {
                              qDebug() << "File is not open!";
                          }
                      return fixByteArray;
                  }
                  

                  What should I change in this connect (or function) to make this work?
                  Thank you.

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

                    Hi,

                    AFAICS your getNewPix function doesn't modify anything in your myDelegate class so you can either make it static or const. Static might make more sense since you also don't depend on anything from this class.

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

                    G 1 Reply Last reply
                    0
                    • SGaistS SGaist

                      Hi,

                      AFAICS your getNewPix function doesn't modify anything in your myDelegate class so you can either make it static or const. Static might make more sense since you also don't depend on anything from this class.

                      G Offline
                      G Offline
                      gabor53
                      wrote on last edited by
                      #11

                      @SGaist
                      How can I make it static or or const? I tried to follow the instructions but I ended up with the same error message.

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

                        What did you modify in your code ?

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

                        G 1 Reply Last reply
                        0
                        • SGaistS SGaist

                          What did you modify in your code ?

                          G Offline
                          G Offline
                          gabor53
                          wrote on last edited by gabor53
                          #13

                          @SGaist
                          Under setEditorData I have the following:

                              QPushButton *button = qobject_cast<QPushButton*>(editor);
                              if(button)
                                  {
                                      button->setStyleSheet ("background-color: rgba(255, 255, 255, 0);");
                          
                                      QByteArray fixFile;
                                      connect(button,&QPushButton::clicked, [=] ()
                                      {
                                          fixByteArray =  getNewPix ();
                                      }); 
                          
                                      qDebug() << "fixFileName: " << fixFile;
                          
                                      QPixmap retPix;
                                      qDebug() << "case 3 bytearray size: " << fixByteArray.size ();
                          
                                      if((retPix.loadFromData (fixByteArray)) == true)
                                          {
                                              qDebug() << "retPix loaded!";
                                              qDebug() << "retPix sizse: " << retPix.size ();
                                          }
                                      else
                                          {
                                              qDebug() << "retPix is NOT loaded!";
                                          }
                                      return;
                                  }
                          

                          getNewPix() looks like this:

                          QByteArray myDelegate::getNewPix()
                          {
                              {
                                  qDebug() << "Entered getNewPix!";
                                  QString sPath = "C:/";
                          
                                  //    fileDialog = getOpenFileName (Q_NULLPTR,sPath);
                          
                          
                                  const QString fileName = QFileDialog::getOpenFileName(Q_NULLPTR,
                                                           tr("Finding Friend's replacement Image"),sPath, tr("Image Files (*.png *.jpg *.bmp)"));
                          
                          
                                  if(fileName == "")
                                      {
                                          qDebug() << "No image was chosen.";
                                      }
                                  else
                                      {
                                          qDebug() << "Image was chosen!";
                                      }
                          
                                  QFile file(fileName);
                                  if(file.open (QIODevice::ReadOnly))
                                      {
                                          qDebug() << "File  is open!";
                          
                                          fixByteArray = file.readAll ();
                          
                                          qDebug() << "Delegate file size: " << fixByteArray.size ();
                                          return fixByteArray;
                          
                                      }
                                  else
                                      {
                                          qDebug() << "File is not open!";
                                      }
                                  return fixByteArray;
                              }
                          
                          };
                          
                          

                          I get 2 error messages:
                          1st at the fixByteArray = getNewPix (); line:
                          C:\Programming\Projects\Folkfriends_1_0\mydelegate.cpp:188: error: passing 'const myDelegate' as 'this' argument discards qualifiers [-fpermissive]
                          fixByteArray = getNewPix ();
                          ^
                          2nd at fixByteArray = file.readAll (); is technically the same error:
                          C:\Programming\Projects\Folkfriends_1_0\mydelegate.cpp:572: error: passing 'const QByteArray' as 'this' argument discards qualifiers [-fpermissive]
                          fixByteArray = file.readAll ();
                          ^
                          Thank you.

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

                            Looks like fixByteArray is a class member which is not needed following your function implementation.

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

                            G 1 Reply Last reply
                            0
                            • SGaistS SGaist

                              Looks like fixByteArray is a class member which is not needed following your function implementation.

                              G Offline
                              G Offline
                              gabor53
                              wrote on last edited by
                              #15

                              @SGaist
                              Is there any other way I can load the file into fixByteArray?

                              1 Reply Last reply
                              0
                              • VRoninV Offline
                                VRoninV Offline
                                VRonin
                                wrote on last edited by
                                #16

                                Since you are having issues with this it's probably faster and safer to implement it in the right way.

                                Build whatever you want your editor delegate to be as a QWidget subclass and then create it inside the createEditor method of your delegate.

                                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                                ~Napoleon Bonaparte

                                On a crusade to banish setIndexWidget() from the holy land of Qt

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

                                  Make fixByteArray local since you return its content from the function.

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

                                  G 1 Reply Last reply
                                  0
                                  • SGaistS SGaist

                                    Make fixByteArray local since you return its content from the function.

                                    G Offline
                                    G Offline
                                    gabor53
                                    wrote on last edited by
                                    #18

                                    @SGaist
                                    Under setEditorData I created the following code that actually works:

                                      QPushButton *button = qobject_cast<QPushButton*>(editor);
                                    
                                        if(button)
                                            {
                                                connect(button,&QPushButton::clicked, [=] ()
                                                {
                                                    qDebug() << "Entered getNewPix!";
                                                    QString sPath = "C:/";
                                                    const QString fileName = QFileDialog::getOpenFileName(Q_NULLPTR,
                                                                             tr("Finding Friend's replacement Image"),sPath, tr("Image Files (*.png *.jpg *.bmp)"));
                                    
                                                    qDebug() << "fileName in setEditorData: " << fileName; //this works
                                    
                                                    if(fileName == "")
                                                        {
                                                            qDebug()    << "No image was chosen.";
                                                        }
                                    
                                                    QFile file(fileName);
                                                    if(file.open (QIODevice::ReadOnly))
                                                        {
                                                            qDebug()   << "File is open!";
                                    
                                                            QPixmap pix2;
                                                            if(pix2.load (fileName) == true)
                                                                {
                                                                    qDebug() << "Pixmap is loaded.";
                                                                }
                                                        }
                                                });
                                    
                                                return ;
                                            }
                                    

                                    The issue is that I need pix2 in setModelData. Here it is declared in the if loop and works without any issues but I can't use it anywhere else. But if I declare it outside of the loop I keep getting the following error message:

                                    C:\Programming\Projects\Folkfriends_1_0\mydelegate.cpp:152: error: passing 'const QPixmap' as 'this' argument discards qualifiers [-fpermissive]
                                    if(pix2.load (fileName) == true)

                                    How can I avoid this error?
                                    Thank you.

                                    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