PDA

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



rimkashox
26th February 2010, 19:26
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:

http://i46.tinypic.com/2mhbkif.png

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. :)

Lykurg
26th February 2010, 20:32
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().

rimkashox
27th February 2010, 14:50
Thanks LyKurg, this seems to be the best solution! I'll try it out. Thanks a lot!

Illidan
2nd January 2013, 22:49
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! :)

ChrisW67
3rd January 2013, 05:19
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:


// mainwindow.h
class MainWindow: ...
{
...
public slots:
void on_pushbutton_clicked();
...
};

// mainwindow.cpp

void on_pushbutton_clicked()
{
MyCustomFrame *frame = new MyCustomFrame(this);
ui->verticalLayout->addWidget(frame);
// do other init stuff
}

Illidan
3rd January 2013, 13:18
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:



void MainWindow::on_archiveExam_clicked()
{
MyCustomWidget *frame = new MyCustomWidget(this);
ui->scrollAreaWidgetContents_2->addWidget(frame);
}

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





void MainWindow::on_archiveExam_clicked()
{
MyCustomWidget *frame = new MyCustomWidget(this);
ui->scrollArea->addWidget(frame);
}

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

Any advice that could help me? :)

Grateful
Illidan

Lesiok
3rd January 2013, 14:18
QScrollArea have only one widget and a method is setWidget(QWidget *widget).

Illidan
3rd January 2013, 17:14
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).

ChrisW67
3rd January 2013, 23:52
You add the new widget to the layout of the widget that is being managed by the QScrollArea, not the QScrollArea itself.

Illidan
7th January 2013, 11:08
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 :)

Illidan
8th January 2013, 20:30
Little addition: Found my problem, i just had to set a minimum Size to the widget i add, then it works.