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;
    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});
    if(this->{ButtonColumn} > 4)
        this->{ButtonColumn} = 0;
sub OnDynamicButton
    $QPushButton =    sender();
    this->textLabel3->setText("You clicked on ".$QPushButton->name());
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License