Results 1 to 11 of 11

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

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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)

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
  •  
Qt is a trademark of The Qt Company.