Dynamic Objects

You can create new PerlQt objects on the fly. You can use QGridLayout to have the objects automatically positioned and sized. Qt also has a special function sender() that returns a pointer to the QObject that sent the SIGNAL. In this example we know the SIGNAL is only sent by a QPushButton so we just use the pointer as if it is a QPushButton.

Download this as a working example. Dynamic Objects

package DynamicObjects;
use Qt;
use DynamicObjectsGUI;
use Qt::isa "DynamicObjectsGUI";
use Qt::slots
    OnCreateAButton => [],
    OnDynamicButton => [];
 
sub NEW
{
    my $self = shift;
    $self->SUPER::NEW(@_);
 
    this->{ButtonRow} = 0;
    this->{ButtonColumn} = 0;
   this->{frame3Layout} = Qt::GridLayout(frame3, 1, 1, 11, 6, this->{frame3Layout});
 
    this->connect(this->pushButton1, SIGNAL('clicked()'), SLOT('OnCreateAButton()'));        
}
 
sub OnCreateAButton
{
    $ButtonName = this->lineEdit1->text();
    $TempButton = Qt::PushButton(frame3, $ButtonName);
    this->connect($TempButton, SIGNAL('clicked()'), SLOT('OnDynamicButton()'));        
    this->{frame3Layout}->addWidget($TempButton, this->{ButtonRow}, this->{ButtonColumn});
    $TempButton->setText($ButtonName);
    $TempButton->show();
 
    this->{ButtonColumn}++;
    if(this->{ButtonColumn} > 4)
    {
        this->{ButtonColumn} = 0;
        this->{ButtonRow}++;    
    }
}
 
sub OnDynamicButton
{
    $QPushButton =    sender();
    this->textLabel3->setText("You clicked on ".$QPushButton->name());
}
 
1;
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License