Signal issues in QStyledItemDelegate



  • 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.



  • Try To Give Connect Statement In CreateEditor Itself and Try.


  • Moderators

    @gabor53 You should check the return value of connect:

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


  • 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


  • Moderators

    @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?



  • @raven-worx said in Signal issues in QStyledItemDelegate:

    What does getNewPix() actually do?

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



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



  • @jsulm
    I added

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

    and the message was
    qDebug connect: true.



  • 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.


  • Lifetime Qt Champion

    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.



  • @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.


  • Lifetime Qt Champion

    What did you modify in your code ?



  • @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.


  • Lifetime Qt Champion

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



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



  • 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.


  • Lifetime Qt Champion

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



  • @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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.