Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QString Pointer Problem



  • Hello,

    I’ve been rowing for weeks and I need help!!

    I created an object that takes as parameter in its constructor an array of Qstring.

    The goal is to recover several Qstring (from the main window), to copy them in another Qstring of this object (Buffer of the secondary window) in order to modify the Qstring of the buffer...

    It works except that when I edit a Qstring, it changes it directly in the main window!

    So I realized that I modified the Qstring pointed from the main window is name the Qstring copy from the secondary window!

    I tried several methods but nothing to do....

    Here is the code:

    le .h

    code_text
    
    #ifndef SETUPLOOP_H
    #define SETUPLOOP_H
     
     
    #include <QtWidgets>
    #include "tool.h"
     
    class setupLoop : public QDialog
    {
        Q_OBJECT
    public:
        setupLoop(QString *_name, int _nLoop, QFont _fontText, QWidget *parent);
        ~setupLoop();
     
    public slots:
        void renameLoop();
        void updateLabelLoop(int index);
        void messageBoxWarning();
     
    private slots:
     
        void save();
     
    private:
     
        void quit();
     
        QComboBox *listeDeroulanteInsert[14];
        QComboBox *listeDeroulanteLoop;
     
        QLabel *labelLoop;
        QLabel *labelListeLoop[14];
     
        tool *setSizeText;
        tool *lenghtStringPixel;
     
        QFont fontText;
     
        QString *name;
     
        int pixelsWide;
        int nLoop;
        int heightLabelLoop;
    };
     
    #endif // SETUPLOOP_H
    

    le .cpp

    	
    #include "setuploop.h"
     
    void setupLoop::save(){close();}
     
    void setupLoop::quit()
        {
        //for(int x=0; x<nLoop; x++){name[x] = nameBuff[x];}
                        close();}
     
    void setupLoop::messageBoxWarning()
    {
        int choix = QMessageBox::warning(this, "", "Save the changes made to the 'loop parameter' before closing",
                                     QMessageBox::Yes | QMessageBox::No);
     
        if(choix == QMessageBox::Yes) {save();}
        else if(choix == QMessageBox::No) {quit();}
    }
     
    void setupLoop::updateLabelLoop(int index){labelLoop->setText(QString("Loop n°%1 :").arg(index+1));}
     
    void setupLoop::renameLoop()
        {
            tool *renameText = new tool(this);
     
            int index = listeDeroulanteLoop->currentIndex();
     
            if (renameText->setRenameText(name[index],"Rename Loop"))
                {
                    name[index] = renameText->getStringBuff();
                    listeDeroulanteLoop->setItemText(index,name[index]);
                    listeDeroulanteLoop->setSizeAdjustPolicy (QComboBox :: AdjustToContents);
     
                    pixelsWide = lenghtStringPixel->getLenghtStringPixel(name, fontText, nLoop);
     
                    labelListeLoop[index]->setText(name[index]);
     
                    for (int x = 0; x < nLoop; x++) {setSizeText->setSizeTextObject(labelListeLoop[x], pixelsWide, heightLabelLoop);}
                }
        }
     
    setupLoop::setupLoop(QString *_name, int _nLoop, QFont _fontText, QWidget *parent = 0) : QDialog(parent)
        {
            setWindowTitle("Setup Loop");
            setMinimumWidth(350);
     
            name = _name;
     
            fontText = _fontText;
            nLoop = _nLoop;
            heightLabelLoop = 22;
     
            pixelsWide = lenghtStringPixel->getLenghtStringPixel(name, fontText, _nLoop);
     
            setSizeText = new tool(this);
            lenghtStringPixel = new tool(this);
     
            labelLoop = new QLabel();
            updateLabelLoop(0);
            labelLoop->setFont(fontText);//.....Police du label
     
            listeDeroulanteLoop = new QComboBox();//.....Création liste déroulante Bank
            listeDeroulanteLoop->setFont(fontText);//......Police de la liste deroulante
            listeDeroulanteLoop->setSizeAdjustPolicy (QComboBox :: AdjustToContents);
            listeDeroulanteLoop->setCursor(Qt::PointingHandCursor);
     
            QVBoxLayout *layoutListeLoop = new QVBoxLayout();
     
            for(int x=0; x< 14; x++)
                {
     
                    QLabel *num = new QLabel(QString("%1 :").arg(x+1));
     
                    setSizeText->setSizeTextObject(num, 22, heightLabelLoop);
     
                    labelListeLoop[x] = new QLabel(name[x]);
     
                    setSizeText->setSizeTextObject(labelListeLoop[x], pixelsWide, heightLabelLoop);
     
                    listeDeroulanteLoop->addItem(name[x]);
     
                    listeDeroulanteInsert[x] = new QComboBox();
                    listeDeroulanteInsert[x]->addItem("Loop");
                    listeDeroulanteInsert[x]->addItem("Insert");
                    listeDeroulanteInsert[x]->setSizeAdjustPolicy (QComboBox :: AdjustToContents);
     
                    QHBoxLayout *layoutLineLoop = new QHBoxLayout();
                    layoutLineLoop->setAlignment(Qt::AlignLeft);
                    layoutLineLoop->addWidget(num);
                    layoutLineLoop->addWidget(labelListeLoop[x]);
                    layoutLineLoop->addWidget(listeDeroulanteInsert[x]);
     
                    layoutListeLoop->addLayout(layoutLineLoop);
                }
     
            QPushButton *bpRename = new QPushButton("Rename");//.....creation du bouton renomer les bank
            //BpRenameBank->setFont(QFont("",t_police_preset,QFont::DemiBold));
            bpRename->setCursor(Qt::PointingHandCursor);
     
            connect(bpRename, SIGNAL(clicked()), this, SLOT(renameLoop()));//.....Connexion du clic du bouton
            connect(listeDeroulanteLoop, SIGNAL(currentIndexChanged(int)), this, SLOT(updateLabelLoop(int)));//.....Connexion du clic du bouton
     
            QHBoxLayout *layoutHorizontal = new QHBoxLayout();
            layoutHorizontal->setAlignment(Qt::AlignLeft);
            layoutHorizontal->addWidget(labelLoop);
            layoutHorizontal->addWidget(listeDeroulanteLoop);
            layoutHorizontal->addWidget(bpRename);
     
            QGroupBox *group = new QGroupBox();
            group->setLayout(layoutHorizontal);
     
            QGroupBox *groupListe = new QGroupBox();
            groupListe->setLayout(layoutListeLoop);
     
            QPushButton *bpSave = new QPushButton("Save");
            connect(bpSave, SIGNAL(clicked()), this, SLOT(save()));
     
            QPushButton *bpQuit = new QPushButton("Quit");
            connect(bpQuit, SIGNAL(clicked()), this, SLOT(messageBoxWarning()));
     
            QHBoxLayout *layoutButton = new QHBoxLayout();
            layoutButton->setAlignment(Qt::AlignRight);
            layoutButton->addWidget(bpSave);
            layoutButton->addWidget(bpQuit);
     
            QVBoxLayout *layoutMain = new QVBoxLayout(this);
            layoutMain->addWidget(group);
            layoutMain->addWidget(groupListe);
            layoutMain->addLayout(layoutButton);
        }
     
    setupLoop::~setupLoop(){}
    

    l'appel de la fonction dans la fenetre principal

    
    	
    QString nameLoop[20];
     
    void TW_S_Rck::ShowSetupLoop()
        {
            setupLoop *setupLoopObject = new setupLoop(nameLoop, nFunctionLoop, fontText, this);
            setupLoopObject->exec();
        }
    
    

    Merci d'avance pour votre aide.

    Cordialement, Antoine



  • Why don't you use a QStringList?



  • @Ant02 said in QString Pointer Problem:

    So I realized that I modified the Qstring pointed from the main window is name the Qstring copy from the secondary window!

    This is what usually happens, when you work with pointers :-)
    So you just want to pass the QString (List/Array) from MainWindow to your QDialog without changing the QString in MainWindow when it gets changed in your QDialog?
    Why don't you pass just the raw text (Pass QString as value not as pointer)?

    You could pass one QStringList (as @Leonardo already said) with all your QString params to your Dialog

    I've seen that you are using std c arrays for QComboBoxes and QLabels:
    It's better in general to use one of the Qt containers like QVector or QList, etc. to store QObjects since it provides some additional Qt features and it's easier to handle inside Qt as well.



  • @Leonardo

    Hello, I tried the Qlist, but I haven’t done enough research on this feature yet to make it work.
    I’ll take a closer look.

    Thank you for your answer.

    Regards, Antoine



  • @Pl45m4

    Hi,

    As expected I tested the Qvector>, it works quite well but I still have a problem!
    I get the Qvector> and are pointing in the secondary window, I change the Qstring of the Qvector>, it works, but I can’t change then the Qvector> of the main window via are pointing...
    Here is the code:
    le .h

    #ifndef SETUPLOOP_H
    #define SETUPLOOP_H
    
    
    #include <QtWidgets>
    #include "tool.h"
    
    class setupLoop : public QDialog
    {
        Q_OBJECT
    public:
        setupLoop(QVector<QString> _name, int _nLoop, QFont _fontText, QWidget *parent);
        ~setupLoop();
    
    public slots:
        void renameLoop();
        void updateLabelLoop(int index);
        void messageBoxWarning();
    
    private slots:
    
        void save();
    
    private:
    
        void quit();
    
        int nLoop;
    
        QComboBox *listeDeroulanteInsert[14];
        QComboBox *listeDeroulanteLoop;
    
        QLabel *labelLoop;
        QLabel *labelListeLoop[14];
    
        tool *setSizeText;
        tool *lenghtStringPixel;
    
        QFont fontText;
    
        QVector<QString> name;
        QVector<QString> *memonames;
    
        int pixelsWide;
        //int nLoop;
        int heightLabelLoop;
    };
    
    #endif // SETUPLOOP_H
    

    le .cpp

    #include "setuploop.h"
    
    void setupLoop::save()
        {
        memonames = name;
    /*
            for(int x=0; x< nLoop; x++)
                {
                memonames[x]= name;
                }
    */
            close();
        }
    
    void setupLoop::quit(){close();}
    
    void setupLoop::messageBoxWarning()
    {
        int choix = QMessageBox::warning(this, "", "Save the changes made to the 'loop parameter' before closing",
                                     QMessageBox::Yes | QMessageBox::No);
    
        if(choix == QMessageBox::Yes) {save();}
        else if(choix == QMessageBox::No) {quit();}
    }
    
    void setupLoop::updateLabelLoop(int index){labelLoop->setText(QString("Loop n°%1 :").arg(index+1));}
    
    void setupLoop::renameLoop()
        {
            tool *renameText = new tool(this);
    
            int index = listeDeroulanteLoop->currentIndex();
    
            if (renameText->setRenameText(name[index],"Rename Loop"))
                {
                    name[index] = renameText->getStringBuff();
                    listeDeroulanteLoop->setItemText(index,name[index]);
                    listeDeroulanteLoop->setSizeAdjustPolicy (QComboBox :: AdjustToContents);
    
                    pixelsWide = lenghtStringPixel->getLenghtStringPixel(name, fontText, nLoop);
    
                    labelListeLoop[index]->setText(name[index]);
    
                    for (int x = 0; x < nLoop; x++) {setSizeText->setSizeTextObject(labelListeLoop[x], pixelsWide, heightLabelLoop);}
                }
        }
    
    setupLoop::setupLoop(QVector<QString> _name, int _nLoop, QFont _fontText, QWidget *parent = 0) : QDialog(parent)
        {        
            setWindowTitle("Setup Loop");
            setMinimumWidth(350);
    
            fontText = _fontText;
            nLoop = _nLoop;
            heightLabelLoop = 22;
    
            for(int x=0; x<nLoop; x++)
                {
                    name.append(_name[x]);
                }
    
            memonames = &_name;
    
            pixelsWide = lenghtStringPixel->getLenghtStringPixel(name, fontText, _nLoop);
    
            setSizeText = new tool(this);
            lenghtStringPixel = new tool(this);
    
            labelLoop = new QLabel();
            updateLabelLoop(0);
            labelLoop->setFont(fontText);//.....Police du label
    
            listeDeroulanteLoop = new QComboBox();//.....Création liste déroulante Bank
            listeDeroulanteLoop->setFont(fontText);//......Police de la liste deroulante
            listeDeroulanteLoop->setSizeAdjustPolicy (QComboBox :: AdjustToContents);
            listeDeroulanteLoop->setCursor(Qt::PointingHandCursor);
    
            QVBoxLayout *layoutListeLoop = new QVBoxLayout();
    
            for(int x=0; x< nLoop; x++)
                {
    
                    QLabel *num = new QLabel(QString("%1 :").arg(x+1));
    
                    setSizeText->setSizeTextObject(num, 22, heightLabelLoop);
    
                    labelListeLoop[x] = new QLabel(name[x]);
    
                    setSizeText->setSizeTextObject(labelListeLoop[x], pixelsWide, heightLabelLoop);
    
                    listeDeroulanteLoop->addItem(name[x]);
    
                    listeDeroulanteInsert[x] = new QComboBox();
                    listeDeroulanteInsert[x]->addItem("Loop");
                    listeDeroulanteInsert[x]->addItem("Insert");
                    listeDeroulanteInsert[x]->setSizeAdjustPolicy (QComboBox :: AdjustToContents);
    
                    QHBoxLayout *layoutLineLoop = new QHBoxLayout();
                    layoutLineLoop->setAlignment(Qt::AlignLeft);
                    layoutLineLoop->addWidget(num);
                    layoutLineLoop->addWidget(labelListeLoop[x]);
                    layoutLineLoop->addWidget(listeDeroulanteInsert[x]);
    
                    layoutListeLoop->addLayout(layoutLineLoop);
                }
    
            QPushButton *bpRename = new QPushButton("Rename");//.....creation du bouton renomer les bank
            //BpRenameBank->setFont(QFont("",t_police_preset,QFont::DemiBold));
            bpRename->setCursor(Qt::PointingHandCursor);
    
            connect(bpRename, SIGNAL(clicked()), this, SLOT(renameLoop()));//.....Connexion du clic du bouton
            connect(listeDeroulanteLoop, SIGNAL(currentIndexChanged(int)), this, SLOT(updateLabelLoop(int)));//.....Connexion du clic du bouton
    
            QHBoxLayout *layoutHorizontal = new QHBoxLayout();
            layoutHorizontal->setAlignment(Qt::AlignLeft);
            layoutHorizontal->addWidget(labelLoop);
            layoutHorizontal->addWidget(listeDeroulanteLoop);
            layoutHorizontal->addWidget(bpRename);
    
            QGroupBox *group = new QGroupBox();
            group->setLayout(layoutHorizontal);
    
            QGroupBox *groupListe = new QGroupBox();
            groupListe->setLayout(layoutListeLoop);
    
            QPushButton *bpSave = new QPushButton("Save");
            connect(bpSave, SIGNAL(clicked()), this, SLOT(save()));
    
            QPushButton *bpQuit = new QPushButton("Quit");
            connect(bpQuit, SIGNAL(clicked()), this, SLOT(messageBoxWarning()));
    
            QHBoxLayout *layoutButton = new QHBoxLayout();
            layoutButton->setAlignment(Qt::AlignRight);
            layoutButton->addWidget(bpSave);
            layoutButton->addWidget(bpQuit);
    
            QVBoxLayout *layoutMain = new QVBoxLayout(this);
            layoutMain->addWidget(group);
            layoutMain->addWidget(groupListe);
            layoutMain->addLayout(layoutButton);
    
        }
    
    setupLoop::~setupLoop(){}
    

    Thank you for your help,
    Regards, Antoine


  • Lifetime Qt Champion

    Hi
    You define it as
    QVector<QString> _name
    in
    setupLoop::setupLoop(QVector<QString> _name,

    if you mean to modify it there it should be
    tupLoop::setupLoop(QVector<QString> &_name,
    as to give a reference ( a pointer) to the original vector.
    else setupLoop has a copy.


Log in to reply