Results 1 to 6 of 6

Thread: dynamically create QLabels

  1. #1
    Join Date
    Jul 2007
    Posts
    11
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default dynamically create QLabels

    I am trying to create QLabels's depending on the amount of fields in a give file which will change. The NumFieldsH(hRfmfile) is a c function that that returns the number of fields as an int.

    Qt Code:
    1. /* Create the data layout */
    2. dataGroupBox = new QGroupBox;
    3. gridGroupBox->setTitle(tr("Buttons"));
    4. datalayout = new QGridLayout;
    5.  
    6. QLabel field[NumFieldsH(hRfmfile)];
    7. for(i = 0; i < NumFieldsH(hRfmfile); i++){
    8.  
    9. field[i] = new QLabel("test");
    10. datalayout->addWidget(field[i],i,1);
    11. }
    12.  
    13. dataGroupBox->setLayout(datalayout);
    To copy to clipboard, switch view to plain text mode 

    the header
    Qt Code:
    1. public:
    2. QGroupBox *dataGroupBox;
    3. QGridLayout *datalayout;
    To copy to clipboard, switch view to plain text mode 
    Im getting errors when i compile this for the 2 lines in the loop, what am I doing wrong?

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: dynamically create QLabels

    Qt Code:
    1. QLabel field[NumFieldsH(hRfmfile)];
    To copy to clipboard, switch view to plain text mode 
    You are declaring a array of QLabel on stack, when doing so, you need to give a constant value as size.

    If you want a variable size array, or don't know the size at compile time, then this is the very reason to use dynamic memory from heap.

    Something like this
    Qt Code:
    1. QList<QLabel *> field;
    2. int size = NumFieldsH(hRfmfile); // this way NumFieldsH(hRfmfile) is called only once
    3. for(i = 0; i < size ; i++){ // if NumFieldsH(hRfmfile) is use in for() then it is called for every iteration,which is not good is NumFieldsH(hRfmfile) if trying to read a IO device (file etc)
    4.  
    5. field.append(new QLabel("test"));
    6. datalayout->addWidget(field[i], i, 1);
    7. }
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to Santosh Reddy for this useful post:

    jeffmetal (18th June 2011)

  4. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: dynamically create QLabels

    You forgot to tell us what the errors are.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #4
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: dynamically create QLabels

    I'm guessing:
    Qt Code:
    1. field[i] = new QLabel("test");
    To copy to clipboard, switch view to plain text mode 
    : cannot convert 'QLabel *' to 'QLabel' in assignment
    Qt Code:
    1. datalayout->addWidget(field[i],i,1);
    To copy to clipboard, switch view to plain text mode 
    : no matching function to call to QGridLayout::addWidget(QLabel,int,int), candidates are QGridLayout::addWidget(QWidget*,int,int)
    or something like that
    btw. I'm not commenting on solution, because Santosh code should work

    ----
    edit2:
    You are declaring a array of (...) on stack, when doing so, you need to give a constant value as size.
    Depends on the compiler. For example in g++ 4.5.x following code is perfectly valid:
    Qt Code:
    1. #include <iostream>
    2. #include <stdlib.h>
    3.  
    4. int main( int argc, char ** argv ){
    5. if( argc > 1 ){
    6. int x[ atoi(argv[1]) ];
    7. std::cout << sizeof(x)/sizeof(int); // should print a value given in program argument
    8. }
    9. return 0;
    10. }
    To copy to clipboard, switch view to plain text mode 
    This feature is called variable-length arrays (VLA), and is part of C99 standard supported by gcc.
    Last edited by stampede; 18th June 2011 at 00:18.

  6. #5
    Join Date
    Jun 2011
    Location
    Białystok, Poland
    Posts
    13
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: dynamically create QLabels

    Qt Code:
    1. QLabel field[NumFieldsH(hRfmfile)];
    2. for(i = 0; i < NumFieldsH(hRfmfile); i++){
    3.  
    4. field[i] = new QLabel("test");
    5. datalayout->addWidget(field[i],i,1);
    To copy to clipboard, switch view to plain text mode 

    I suggest to write it like this:

    Qt Code:
    1. QLabel* field[NumFieldsH(hRfmfile)];
    2. for(i = 0; i < NumFieldsH(hRfmfile); i++){
    3.  
    4. field[i] = new QLabel("test");
    5. datalayout->addWidget(field[i],i,1);
    To copy to clipboard, switch view to plain text mode 

    and I'm guessing You're getting non-scalar exception

    better way is the Santosh solution, with QList...

  7. #6
    Join Date
    Jul 2007
    Posts
    11
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: dynamically create QLabels

    thanks you santosh for the help I did it the way you recommended and it worked I also added another row of QlineEdits that i have called data. The problem I am now having is how do I go about setting a new value for the QLineEdits in another method.

    I cant seem to access them using data[i].setValue("test"); to work i get the error

    Qt Code:
    1. /usr/lib64/qt4/include/QtGui/qwidget.h: In member function ‘void Dialog::update()’:
    2. /usr/lib64/qt4/include/QtGui/qwidget.h:672: error: ‘QWidgetData* QWidget::data’ is private
    3. dialog.cpp:234: error: within this context
    4. dialog.cpp:234: error: ‘class QWidgetData’ has no member named ‘setValue’
    To copy to clipboard, switch view to plain text mode 

    im assuming this is because the data Lineedits are being declared in the constructor and are out of the scope of my new method.

Similar Threads

  1. Replies: 1
    Last Post: 29th November 2010, 17:22
  2. Dynamically create SVG with QWebElement
    By yawar in forum Qt Programming
    Replies: 0
    Last Post: 11th April 2010, 03:53
  3. finding dynamically create object
    By abrou in forum Newbie
    Replies: 2
    Last Post: 5th March 2008, 21:17
  4. Replies: 8
    Last Post: 20th April 2007, 00:37
  5. dynamically create folders with QT
    By eleanor in forum Newbie
    Replies: 3
    Last Post: 13th March 2007, 11:40

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.