Creating matrix of label in constructor



  • Hi i'm new of the forum and of the qt library
    i'm italian so... sorry for my english

    i'm trying to create a matrix of label in the constructor of a form class, here the code:
    @PlayForm::PlayForm() {
    widget.setupUi(this);
    labelmat = new QLabel[9];
    int X = 70, Y = 30;
    for(int i = 0; i < 9; i++){
    labelmat[i] = new QLabel(this);
    labelmat[i].setObjectName(QString::fromUtf8("label_" + i));
    labelmat[i].setGeometry(X,Y,81,121);
    labelmat[i].setPixmap(QPixmap(QString::fromUtf8("dist/Debug/GNU-Linux-x86/retro_3.png")));
    if(X < 310)
    X += 120;
    else
    X = 70;
    if(Y < 310)
    Y += 140;
    else
    Y = 30;
    }
    }@

    But here's the error of the compiler:
    @PlayForm.cpp:16:38: error: no match for ‘operator=’ in ‘(((PlayForm)this)->PlayForm::labelmat + ((long unsigned int)(((long unsigned int)i) * 40ul))) = (QFlagsQt::WindowType(0u), (operator new(40u), (<statement>, ((QLabel*)<anonymous>))))’
    PlayForm.cpp:16:38: note: candidate is:
    /usr/include/qt4/QtGui/qlabel.h:165:5: note: QLabel& QLabel::operator=(const QLabel&)
    /usr/include/qt4/QtGui/qlabel.h:165:5: note: no known conversion for argument 1 from ‘QLabel*’ to ‘const QLabel&’
    PlayForm.cpp: In destructor ‘virtual PlayForm::~PlayForm()’:
    PlayForm.cpp:33:26: error: type ‘class QLabel’ argument given to ‘delete’, expected pointer@

    Thanks all for the answer

    EDIT:
    There is an error also in the distructor, there is the code:
    @PlayForm::~PlayForm() {
    for(int i = 0; i < 9; i++)
    delete labelmat[i];
    delete[] labelmat;
    }@


  • Moderators

    You're trying to copy between a label pointer and a label in your line 6 above.

    More importantly, try to avoid using arrays of widgets. You're much better off using Qt's" container classes.":http://developer.qt.nokia.com/doc/qt-4.8/containers.html

    In your case you could use:
    @

    QList<QLabel*> labelmat;

    ...

    for (int i = 0; i < 9; i++)
    {
    QLabel * tmplabel = new QLabel(this);
    tmplabel->setStuff(...);
    tmplabel->setMoreStuff(...);

    labelmat.append(tmplabel);
    ...
    }
    @

    Brain to keyboard, YMMV

    Since the QLabels are parented to this, you don't need to worry about deleting them in the destructor. The parent window's destructor will take care of deleting its children.



  • I see 1 mistake in line 6 of your code.
    labelmat[i]=new QLabel(this);
    you are assigning a QLabel pointer to a Qlabel object...labelmat[i] is not a pointer to Qlabel it is object of QLabel.to correctly do it...i think you should write
    (labelmat+i)=newQLabel(this); //only the array name which is a
    //pointer to Qlabel.

    and one another thing you don't need to delete any QT widget that is given the parent in its arguments so the destructor is unnecessary.because QT frees the memory for you automatically.



  • yes of course Sir mlong corrected me too.thank you sir/



  • [quote author="WellSaid" date="1329245855"]
    i'm trying to create a matrix of label in the constructor of a form class[/quote]

    [quote author="mlong" date="1329249322"]More importantly, try to avoid using arrays of widgets. You're much better off using Qt's" container classes.":http://developer.qt.nokia.com/doc/qt-4.8/containers.html
    [/quote]

    I am wondering why don't you use "QGridLayout":http://developer.qt.nokia.com/doc/qt-4.8/qgridlayout.html? In my opinion it will be much easier to maintain a matrix of labels using QGridLayout.



  • if i want to keep a unsigned for each label:
    @
    struct Card{
    QLabel ilabel;
    unsigned status;
    };

    class PlayForm : public QDialog {
    Q_OBJECT
    Card* ImageMatrix;
    public:
    PlayForm();
    virtual ~PlayForm();
    public slots:
    void Pause();
    void Quit();
    private:
    Ui::PlayForm widget;
    };
    @

    In the constructor:
    @
    PlayForm::PlayForm() {
    widget.setupUi(this);
    ImageMatrix = new Card[9];
    int X = 70, Y = 30;
    for(int i = 0; i < 9; i++){
    ImageMatrix[i].ilabel = new QLabel(this);
    ImageMatrix[i].ilabel.setObjectName(QString::fromUtf8("label_" + i));
    ImageMatrix[i].ilabel.setGeometry(X,Y,81,121);
    ImageMatrix[i].ilabel.setPixmap(QPixmap(QString::fromUtf8("dist/Debug/GNU-Linux-x86/retro_3.png")));
    if(X < 310)
    X += 120;
    else{
    X = 70;
    if(Y < 310)
    Y += 140;
    else

                Y = 30;
        }
    }
    

    }
    @

    Error:
    @
    PlayForm.cpp:16:48: error: no match for ‘operator=’ in ‘(((PlayForm*)this)->PlayForm::ImageMatrix + ((long unsigned int)(((long unsigned int)i) * 48ul)))->Card::ilabel = (QFlagsQt::WindowType(0u), (operator new(40u), (<statement>, ((QLabel*)<anonymous>))))’
    PlayForm.cpp:16:48: note: candidate is:
    /usr/include/qt4/QtGui/qlabel.h:165:5: note: QLabel& QLabel::operator=(const QLabel&)
    /usr/include/qt4/QtGui/qlabel.h:165:5: note: no known conversion for argument 1 from ‘QLabel*’ to ‘const QLabel&’
    @

    Why the same error?

    EDIT:
    It works now, here the new code of the constructor:
    @
    PlayForm::PlayForm() {
    widget.setupUi(this);
    ImageMatrix = new Card[16];
    int X = 70, Y = 30;
    for(int i = 0; i < 16; i++){
    ImageMatrix[i].ilabel = new QLabel(this);
    ImageMatrix[i].ilabel -> setObjectName(QString::fromUtf8("label_" + i));
    ImageMatrix[i].ilabel -> setGeometry(X,Y,81,121);
    ImageMatrix[i].ilabel -> setPixmap(QPixmap(QString::fromUtf8("dist/Debug/GNU-Linux-x86/retro_3.png")));
    //QObject::connect(ImageMatrix[i].ilabel, SIGNAL(clicked()), this, SLOT(Modifica(i)));
    ImageMatrix[i].status = 0; //Coperta
    if(X < 430)
    X += 120;
    else{
    X = 70;
    if(Y < 450)
    Y += 140;
    else
    Y = 30;
    }
    }
    }
    @


Log in to reply
 

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