Results 1 to 2 of 2

Thread: [SOLVED]Adding dynamically created widgets to layout error

  1. #1
    Join Date
    Jun 2014
    Posts
    13
    Thanks
    1
    Qt products
    Qt5

    Default [SOLVED]Adding dynamically created widgets to layout error

    I'm creating an array of widgets and I'm trying to add them to the main layout but I get an error.

    Qt Code:
    1. QLabel *colorBoxes = new QLabel[numLights];
    2. for(int i = 0; i < numLights; i++)
    3. {
    4. colorBoxes[i].setText(description[i]);
    5. ui->mainLayout->addWidget(colorBoxes[i]);
    6. }
    To copy to clipboard, switch view to plain text mode 



    It says it can't convert QLabel to QWidget*. It worked before in another project, I have no idea why this is doing it now?


    apparently to make it work, I needed to add it as
    Qt Code:
    1. ui->mainLayout->addWidget(colorBoxes[i].window());
    To copy to clipboard, switch view to plain text mode 
    Last edited by bnosam; 7th April 2015 at 03:47.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: [SOLVED]Adding dynamically created widgets to layout error

    You have an array of type QLabel. QLayout::addWidget() requires a pointer to a widget. QLabel* would be such a pointer.

    So you can either change the type of the array or take the addres of each item in the array.

    Might even be worth considering usage of a container, e.g. QVector, it is easy to forget to use the delete[] operator when deallocating an array.

    Cheers,
    _

Similar Threads

  1. Adding widgets to layout dynamically
    By anbu01 in forum Newbie
    Replies: 2
    Last Post: 15th May 2014, 13:41
  2. trouble when moving dynamically created custom Widgets
    By chocolateSteak in forum Qt Programming
    Replies: 1
    Last Post: 16th September 2013, 01:38
  3. Replies: 3
    Last Post: 11th August 2011, 18:16
  4. Replies: 5
    Last Post: 19th April 2010, 00:31
  5. Adding widgets dynamically
    By miw in forum Qt Programming
    Replies: 1
    Last Post: 23rd July 2009, 11:31

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.