Results 1 to 3 of 3

Thread: Can we create an array of widgets?

  1. #1
    Join Date
    Apr 2017
    Posts
    55
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Can we create an array of widgets?

    I have a gui built from creator/designer. There are MANY Qtextedit boxes on it. I would be nice if these Qtextedit boxes were in an array ( qtextedit(idx) so I could load it within a loop in code rather than explicitly calling out each widget name. Is there such a capability?

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Can we create an array of widgets?

    You can always create an array of QWidget pointers, but not QWidget instances themselves. This is because QWidget has no public copy constructor or assignment operator.

    Qt Code:
    1. // You can do this:
    2.  
    3. QVector< QWidget * > widgetVector;
    4.  
    5. // but not this:
    6.  
    7. QVector< QWidget > forbiddenVec;
    To copy to clipboard, switch view to plain text mode 

    You can't create such a vector of pointers using Qt Designer; you can only do it in code. You -can- use Qt Designer to lay out your GUI using individual widgets, and then after you call setupUi() in the form's constructor, you can push copies of the pointers (not new widgets) onto a list or vector if it is more convenient to access them by index:

    Qt Code:
    1. MyWidget::MyWidget( QWidget * parent ) : BaseWidget( parent )
    2. {
    3.  
    4. ui->setupUi();
    5.  
    6. widgetVector.push_back( ui->textEdit0 );
    7. widgetVector.push_back( ui->textEdit1 );
    8. // and so on
    9.  
    10. // later, you can get the nth text edit by
    11. QTextEdit * pNth = widgetVector[ n ];
    12.  
    13. // which is the same as
    14. QTextEdit * pNth = ui->textEditN;
    15.  
    16. }
    To copy to clipboard, switch view to plain text mode 
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Apr 2017
    Posts
    55
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Can we create an array of widgets?

    That is excellent. Saves alot of code for me.

Similar Threads

  1. How to create lineEdits as an array?
    By Wolletje in forum Newbie
    Replies: 10
    Last Post: 20th November 2016, 08:13
  2. Array of widgets
    By maider in forum Qt Programming
    Replies: 2
    Last Post: 20th November 2009, 14:13
  3. How to Create control Array in QT4
    By umulingu in forum Qt Programming
    Replies: 4
    Last Post: 8th October 2009, 16:31
  4. How To Create Array of Widgets in Qt4?
    By redhat in forum Newbie
    Replies: 4
    Last Post: 27th April 2009, 11:37
  5. Create an array of Widgets
    By gt.beta2 in forum Qt Tools
    Replies: 4
    Last Post: 25th February 2009, 20:44

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.