Results 1 to 11 of 11

Thread: How To Dynamically Add Widget when Clicked() signal emitted - In Qt-Designer ?

  1. #1
    Join Date
    Feb 2010
    Posts
    2
    Qt products
    Qt4
    Platforms
    MacOS X

    Question How To Dynamically Add Widget when Clicked() signal emitted - In Qt-Designer ?

    Hello, I'm quite new to QT and trying to design a Ui in QT-Designer that automatically adds a new groupBox when a pushButton is clicked. I've put everything in a Scroll area so that we can add unlimited numbers of items.

    I could find a close() slot that works... (it make the groupBox disappear)... But is there no create() slot ?

    How do I dynamically create a new GroupBox when the button is clicked ? Here's a screen shot of what I mean:



    I don't mind even complicated solutions... the important thing is that it works! Perhaps this could be achieved with the use of UiLoader ??

    What do you think?

    Any advice would be much appreciated!

    Thanks.

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: How To Dynamically Add Widget when Clicked() signal emitted - In Qt-Designer ?

    The designer is a great tool but it can't perform magic. You have to write your own slot to add new widgets. But that's not difficult. Just create a new widget and add it to the layout via QLayout::addWidget().

  3. #3
    Join Date
    Feb 2010
    Posts
    2
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: How To Dynamically Add Widget when Clicked() signal emitted - In Qt-Designer ?

    Thanks LyKurg, this seems to be the best solution! I'll try it out. Thanks a lot!

  4. #4
    Join Date
    Jan 2013
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How To Dynamically Add Widget when Clicked() signal emitted - In Qt-Designer ?

    Hi, I'm pretty new to Qt and i don't even have that much experience in programming. My problem is basicly the same like the one of rimkashox, when a pushButton is klicked, i want to automatically add a new groupbox with some items (a label, a button (to remove the groupbox) and a textfield) in it. But since I'm really new the solution doesn't help me. I could need a step by step solution.

    So how do I create a widget? Do you mean the normal way over "rightklick on the project-->add-->qt-designer-form-class-->Widget(template\forms)"? So that i can use the Qt-Designer to build my widget?

    And if this is the right way to create my widget, how exactly do i add the widget to my layout? How and where do i have to use the function "QLayout::addWidget()"? Especially in which file do i have to write it? I've got "mainwindow.h, widget.h, mainwindow.cpp, widget.cpp, mainwindow.ui, widget.ui, main.cpp". Some example code would really help me.

    And my last question, how does it work with the object names? Does Qt automatically give another name everytime i press the Button to add a new Groupbox or do i have to write a function to do this?

    I'm sorry if my questions are a bit dumb but I'm allready working for about 3 hours on this one problem and it is by far not my only problem. I would be really grateful if you could help me somehow!

  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How To Dynamically Add Widget when Clicked() signal emitted - In Qt-Designer ?

    You create a new widget at run time using the C++ new operator with an appropriate class, e.g. MyCustomFrame or QLabel. You call the layout object's addWidget() function with your newly minted widget. All this happens in the slot you have connected to the clicked() signal of the QPushButton.

    Assuming the widgets are being added to the layout of your main window it might look like this:
    Qt Code:
    1. // mainwindow.h
    2. class MainWindow: ...
    3. {
    4. ...
    5. public slots:
    6. void on_pushbutton_clicked();
    7. ...
    8. };
    9.  
    10. // mainwindow.cpp
    11.  
    12. void on_pushbutton_clicked()
    13. {
    14. MyCustomFrame *frame = new MyCustomFrame(this);
    15. ui->verticalLayout->addWidget(frame);
    16. // do other init stuff
    17. }
    To copy to clipboard, switch view to plain text mode 

  6. The following user says thank you to ChrisW67 for this useful post:

    Illidan (3rd January 2013)

  7. #6
    Join Date
    Jan 2013
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How To Dynamically Add Widget when Clicked() signal emitted - In Qt-Designer ?

    Wow thats ingenious, thank you very much

    I have one last, pretty short question. Is it possible to use a PushButton of a Dialog to add "MyCustomFrame" into the "mainwindow"? How should the path in "dialog.cpp" look like? (With path i mean "ui->verticalLayout->addWidget(frame);").

    Gratefully
    Illidan


    Added after 1 21 minutes:


    Well i guess i shouldn't call something a last question. I tried your solution and it works very well if i insert "MyCustomFrame" into a "verticalLayout". Problem is, if i insert my widget more then once or twice it gets smaller and smaller because the space is not enough. So i wanted to insert my "MyCustomFrame" into a scrollArea. But then i just get the following errors:

    Qt Code:
    1. void MainWindow::on_archiveExam_clicked()
    2. {
    3. MyCustomWidget *frame = new MyCustomWidget(this);
    4. ui->scrollAreaWidgetContents_2->addWidget(frame);
    5. }
    To copy to clipboard, switch view to plain text mode 

    Gives me the error: "C2039: 'addWidget': Is not an element of 'QWidget' "



    Qt Code:
    1. void MainWindow::on_archiveExam_clicked()
    2. {
    3. MyCustomWidget *frame = new MyCustomWidget(this);
    4. ui->scrollArea->addWidget(frame);
    5. }
    To copy to clipboard, switch view to plain text mode 

    Gives me the error: "C2039: 'addWidget': Is not an element of 'QScrollArea' "

    Any advice that could help me?

    Grateful
    Illidan
    Last edited by Illidan; 3rd January 2013 at 13:18.

  8. #7
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How To Dynamically Add Widget when Clicked() signal emitted - In Qt-Designer ?

    QScrollArea have only one widget and a method is setWidget(QWidget *widget).

  9. #8
    Join Date
    Jan 2013
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How To Dynamically Add Widget when Clicked() signal emitted - In Qt-Designer ?

    Well ye thats what i also found out but with setWidget i can't add more than one widget to the QScrollArea and i can't find any solution of how to add several CustomWidgets without QScrollArea. Isn't there an easy solution to do this? Also complex solutions would be ok, just need some solution that works (allready found some solutions at the internet but none of them worked for me).

  10. #9
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How To Dynamically Add Widget when Clicked() signal emitted - In Qt-Designer ?

    You add the new widget to the layout of the widget that is being managed by the QScrollArea, not the QScrollArea itself.

  11. #10
    Join Date
    Jan 2013
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How To Dynamically Add Widget when Clicked() signal emitted - In Qt-Designer ?

    Well if i enter the new widget to the layout within the QScrollArea it still doesn't work. The Layout seems to have a constant size and isn't getting greater, when i insert more Elements. But i think i found another way to solve my problem without dynamically adding custom widgets. I'll use a dynamic list with a signal and a special id for each entry, which opens a new window with the information i wanna get from/save into the backend.

    Thx very much for your help

  12. #11
    Join Date
    Jan 2013
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How To Dynamically Add Widget when Clicked() signal emitted - In Qt-Designer ?

    Little addition: Found my problem, i just had to set a minimum Size to the widget i add, then it works.

Similar Threads

  1. Replies: 1
    Last Post: 7th December 2009, 18:49
  2. signal emitted when I zoom
    By mastupristi in forum Qwt
    Replies: 1
    Last Post: 8th July 2009, 17:02
  3. Signal emitted more than once?
    By dbrmik in forum Qt Programming
    Replies: 3
    Last Post: 13th March 2009, 12:44
  4. Replies: 1
    Last Post: 29th April 2008, 12:12
  5. Custom Widget - clicked() signal
    By ak in forum Newbie
    Replies: 3
    Last Post: 13th November 2006, 08:35

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.