Solved Add PushButtons dinamically to Layout and define functionality ?
-
Hey!
I want to create dinamically pushButtons, add them to a layout and align it to a scrollArea.
First thing: How can I do this?
Second: How can I add functionality for the pushButtons (it will be always the same)?
I tried:QPushButton *newButton = new QPushButton(ui->LineEdit->text(), this); myVBoxLayout->addWidget( newButton ); ui->scrollArea->setLayout( myVBoxLayout);
But as I expected, it didn't work.
Any suggestions?
Thanks for answers! -
@Niagarer said
But as I expected, it didn't work.
In what way didnt work ?
The buttons dont show up ?
They are out of order etc ? -
@mrjj
The programm just crashes.
But I think, I got a solution for this some seconds ago:
In the costructor:ui->scrollArea->setLayout(new QVBoxLayout);
In the function:
QPushButton *newButton = new QPushButton(ui->LineEdit->text(), this); ui->scrollArea->layout()->addWidget( newButton );
(problem is, I cant scroll in this scrollArea... :D)
-
@Niagarer
Seems ok.
You can first scroll when enough buttons are there. -
@mrjj
I addad about 30 buttons, nothing happens.
They just became smaller and smaller... -
Yes, unless u set minimumHeight on them, they will become smaller as its allowed.
newButton->setMinimumHeight(200);
-
You should not set the layout on the scroll area. You should create a widget that will be the content of the scroll area and add buttons to its layout, i.e.
QWidget* content = new QWidget(); content->setLayout(new QVBoxLayout); content->layout()->addWidget(new QPushButton(ui->LineEdit->text())); //add other buttons ui->scrollArea->setWidget(content);
-
@mrjj
Hm. When I try this, I get this:
Unfortunately still no scrolling.
-
That's because you're changing the layout of the scroll area itself. You should be changing the layout of the content.
-
Hi
Do as @Chris-Kawa says.
Im used to do it in Designer. ( so didnt spot it)
It does make a content widget for you.
You need it.
-
Yes, it works, thanks!
Now I can scroll.
I think, I got the answer for my second question, I just have to use connect.
I will update, when I am finished.
Thank you! -
@Niagarer
Yes, just connect the buttons clicked or released signal to a slot
Note that inside a slot, you can use
sender() to know the QObject that sent the signal.But it depends on what the buttons do if each should have own slot or they can share.
-
@mrjj
Ok.
Now I have a (little) problem left: I do not get it to give the Object itself to the slot.
What I need is the name of the button, because this name is similar to a name in a vector, where I have some objects (when I press the button, I want to print information about the object that has this name).
I tried it with sender() but then I can't get the name of it. -
@Niagarer
Hi
You must cast it to the expected type.
sender() is a baseclass pointer and while it can point to any QObject, you must
cast it yourself to have access.QPushButton *mybut=qobject_cast<QPushButton *>( sender() );
if (mybut) {
QString bname=mybut->objectxxx
} -
@mrjj
Jep, it works.
Thanks man, yout are a really good help! -
@Niagarer
np. Are the buttons names 100% the same as in list ?
Im wondering if you could use a QMap for the extra info lookup. -
@mrjj
Yes they are, but I choosed this way, because the vector with the objects is inside an object of a custom widget.
Here I am in MainWindow. But the objects, I need are in a custom widget class, that is a part of the window.