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. Creating matrix of label in constructor
Forum Updated to NodeBB v4.3 + New Features

Creating matrix of label in constructor

Scheduled Pinned Locked Moved General and Desktop
6 Posts 4 Posters 3.6k 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.
  • W Offline
    W Offline
    WellSaid
    wrote on last edited by
    #1

    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;
    }@

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mlong
      wrote on last edited by
      #2

      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.

      Software Engineer
      My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

      1 Reply Last reply
      0
      • S Offline
        S Offline
        shahid.pk
        wrote on last edited by
        #3

        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.

        Love to learn....

        1 Reply Last reply
        0
        • S Offline
          S Offline
          shahid.pk
          wrote on last edited by
          #4

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

          Love to learn....

          1 Reply Last reply
          0
          • L Offline
            L Offline
            leon.anavi
            wrote on last edited by
            #5

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

            http://anavi.org/

            1 Reply Last reply
            0
            • W Offline
              W Offline
              WellSaid
              wrote on last edited by
              #6

              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;
              }
              }
              }
              @

              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