1
use Test::More tests => 34;
10
my $app = Qt::Application( \@ARGV );
13
my $widget = Qt::Widget();
15
is ( Devel::Peek::SvREFCNT($widget), 1, 'refcount' );
16
# Test Qt::String marshalling
17
my $wt = 'Qt::String marshalling works!';
18
$widget->setWindowTitle( $wt );
19
is ( $widget->windowTitle(), $wt, 'Qt::String' );
23
my $widget = Qt::Widget();
24
# Test a string that has non-latin characters
28
$widget->setWindowTitle( $wt );
29
is ( $widget->windowTitle(), $wt, 'Qt::String unicode' );
34
# Test int marshalling
35
my $widget = Qt::Widget();
37
$widget->resize( $int, $int );
38
is ( $widget->height(), $int, 'int' );
40
# Test marshalling to int from enum value
41
my $textFormat = Qt::TextCharFormat();
42
$textFormat->setFontWeight( Qt::Font::Bold() );
43
is ( $textFormat->fontWeight(), ${Qt::Font::Bold()}, 'enum to int' );
47
# Test double marshalling
49
my $doubleValidator = Qt::DoubleValidator( $double, $double * 2, 5, undef );
50
is ( $doubleValidator->bottom(), $double, 'double' );
51
is ( $doubleValidator->top(), $double * 2, 'double' );
55
# Test bool marshalling
56
my $widget = Qt::Widget();
57
my $bool = !$widget->isEnabled();
58
$widget->setEnabled( $bool );
59
is ( $widget->isEnabled(), $bool, 'bool' );
63
# Test int* marshalling
64
my ( $x1, $y1, $w1, $h1, $x2, $y2, $w2, $h2 ) = ( 5, 4, 50, 40 );
65
my $rect = Qt::Rect( $x1, $y1, $w1, $h1 );
66
$rect->getRect( $x2, $y2, $w2, $h2 );
75
# Test unsigned int marshalling
76
my $label = Qt::Label();
77
my $hcenter = ${Qt::AlignHCenter()};
78
my $top = ${Qt::AlignTop()};
79
$label->setAlignment(Qt::AlignHCenter() | Qt::AlignTop());
80
my $alignment = $label->alignment();
81
is( $alignment, $hcenter|$top, 'unsigned int' );
85
# Test unsigned int * marshalling
86
my $option = Qt::StyleOption();
87
my $state = Qt::Style::State_On() | Qt::Style::State_HasFocus();
88
$option->setState( $state );
89
is( $option->state(), ${$state}, 'unsigned int *' );
93
# Test char and uchar marshalling
94
my $char = Qt::Char( Qt::Int(87) );
95
is ( $char->toAscii(), 87, 'signed char' );
96
$char = Qt::Char( Qt::Uchar('f') );
97
is ( $char->toAscii(), ord('f'), 'unsigned char' );
98
$char = Qt::Char( 'f', 3 );
99
is ( $char->row(), 3, 'unsigned char' );
100
is ( $char->cell(), ord('f'), 'unsigned char' );
104
# Test uchar* marshalling
106
my $green = chr(128);
108
my $alpha = chr(255);
109
my $val = $red . $green . $blue . $alpha;
112
my $buffer = $val x ($w*$h);
113
my $image = Qt::Image( $buffer, $w, $h, Qt::Image::Format_RGB32() );
114
my $result = $image->pixel(0,0);
115
is( $result, ord($red) << 0 | ord($green) << 8 | ord($blue) << 16 | ord($alpha) << 24, 'unsigned char*' );
119
# Test short, ushort, and long marshalling
120
my $shortSize = length( pack 'S', 0 );
123
my $block = Qt::ByteArray();
124
my $stream = Qt::DataStream($block, Qt::IODevice::ReadWrite());
125
no warnings qw(void);
126
$stream << Qt::Short($num);
127
my $streamPos = $stream->device()->pos();
128
$stream->device()->seek(0);
129
$stream >> Qt::Short($gotNum);
131
is ( $gotNum, $num, 'signed short' );
134
no warnings qw(void);
135
$stream->device()->seek(0);
136
$stream << Qt::Ushort($num);
137
$stream->device()->seek(0);
138
$stream >> Qt::Ushort($gotNum);
140
is ( $gotNum, $num, 'unsigned short' );
141
is ( $streamPos, $shortSize, 'long' );
145
# Test ushort marshalling
146
my $tcpSocket = Qt::TcpSocket(undef);
148
$tcpSocket->setPeerPort($port);
149
is( $tcpSocket->peerPort(), $port, 'unsigned short' );
153
# Test length of QByteArray::data
154
my $buf = Qt::Buffer();
155
$buf->open( Qt::Buffer::ReadWrite() );
156
my $var = "Hello, \0World!";
157
$buf->write( $var, length $var );
159
my $ba = $buf->read( 14 );
160
is( length( $ba->data() ), length( $var ), "QByteArray::data length" );
164
# Test double& marshalling
165
my @dimensions = ( 97.84, 105.934, 43.9025, 408.32 );
166
my $rect = Qt::RectF( @dimensions );
167
my ( $left, $top, $right, $bottom );
168
$rect->getRect( $left, $top, $right, $bottom );
169
is_deeply( [ $left, $top, $right, $bottom ], \@dimensions, 'double&' );
174
my $action1 = Qt::Action( 'foo', undef );
175
my $action2 = Qt::Action( 'bar', undef );
176
my $action3 = Qt::Action( 'baz', undef );
178
# Add some stuff to them...
179
$action1->{The} = 'quick';
180
$action2->{brown} = 'fox';
181
$action3->{jumped} = 'over';
183
my $actions = [ $action1, $action2, $action3 ];
185
my $widget = Qt::Widget();
186
$widget->addActions( $actions );
188
my $gotactions = $widget->actions();
190
is_deeply( $actions, $gotactions, 'marshall_ItemList<>' );
195
my $w = Qt::ColumnView();
196
$w->setModel(Qt::DirModel());
198
$w->setColumnWidths( $widths );
199
is_deeply( $w->columnWidths, $widths, 'marshall_QIntList' );
203
# Test ambiguous list call
204
my $strings = [ qw( The quick brown fox jumped over the lazy dog ) ];
205
my $var = Qt::Variant( $strings );
206
my $newStrings = $var->toStringList();
207
is_deeply( $strings, $newStrings, 'Ambiguous list resolution' );
211
# Test marshall_ValueListItem ToSV
212
my $shortcut1 = Qt::KeySequence( 'Enter' );
213
my $shortcut2 = Qt::KeySequence( 'Tab' );
214
my $shortcut3 = Qt::KeySequence( 'Space' );
215
my $shortcuts = [ $shortcut1, $shortcut2, $shortcut3 ];
216
my $action = Qt::Action( 'Foobar', undef );
218
$action->setShortcuts( $shortcuts );
219
my $gotshortcuts = $action->shortcuts();
221
is( scalar @{$gotshortcuts}, scalar @{$shortcuts}, 'marshall_ValueListItem<> FromSV array size' );
222
is_deeply( [ map{ $gotshortcuts->[$_]->matches( $shortcuts->[$_] ) } 0..$#{$gotshortcuts} ],
223
[ map{ Qt::KeySequence::ExactMatch() } (0..$#{$gotshortcuts}) ],
224
'marshall_ValueListItem<> FromSV array content' );
228
my $tree = Qt::TableView( undef );
229
my $model = Qt::DirModel();
231
$tree->setModel( $model );
232
my $top = $model->index( Qt::Dir::currentPath() );
233
$tree->setRootIndex( $top );
235
my $selectionModel = $tree->selectionModel();
236
my $child0 = $top->child(0,0);
237
my $child1 = $top->child(0,1);
238
my $child2 = $top->child(0,2);
239
my $child3 = $top->child(0,3);
240
$selectionModel->select( $child0, Qt::ItemSelectionModel::Select() );
241
$selectionModel->select( $child1, Qt::ItemSelectionModel::Select() );
242
$selectionModel->select( $child2, Qt::ItemSelectionModel::Select() );
243
$selectionModel->select( $child3, Qt::ItemSelectionModel::Select() );
245
my $selection = $selectionModel->selection();
246
my $indexes = $selection->indexes();
248
# Run $indexes->[0] == $child0, which should return '1', for each returned
250
is_deeply( [ map{ eval "\$indexes->[$_] == \$child$_" } (0..$#{$indexes}) ],
251
[ map{ 1 } (0..$#{$indexes}) ],
252
'marshall_ValueListItem<> ToSV' );
256
# Test QHash marshalling
257
# This will test EITHER QMap<QString,QVariant> or
258
# QHash<QString,QVariant>
259
# On my system with Qt 4.6.1 and perl 5.10.0, it calls the QMap one
261
string => Qt::Variant( Qt::String( 'bar' ) ),
262
integer => Qt::Variant( Qt::Int( 5 ) )
265
my $variant = Qt::Variant( $hash );
266
my $rethash = $variant->value();
267
is_deeply( $rethash, $hash, 'QMap<QString,QVariant>' );
271
# Test Qt::Object::findChildren
272
my $widget = Qt::Widget();
273
my $childWidget = Qt::Widget($widget);
274
$childWidget->setObjectName('childWidget');
275
my $childPushButton = Qt::PushButton($childWidget);
276
my $children = $widget->findChildren('Qt::Widget');
277
is_deeply( $children, [$childWidget, $childPushButton], 'Qt::Object::findChildren' );
278
$children = $widget->findChildren('Qt::Widget', 'childWidget');
279
is_deeply( $children, [$childWidget], 'Qt::Object::findChildren' );
280
$children = $widget->findChildren('Qt::PushButton');
281
is_deeply( $children, [$childPushButton], 'Qt::Object::findChildren' );
285
# Test Qt::IODevice::read
286
my $buf = Qt::Buffer();
287
$buf->open( Qt::Buffer::ReadWrite() );
288
my $var = "Hello, \0World!";
289
$buf->write( $var, length $var );
292
my $size = $buf->read( \$str, length $var );
294
is( $str, $var, "Qt::IODevice::read(char*, qint64)" );
297
my $ba = $buf->read( length $var );
298
is( $ba->data(), $var, 'Qt::IODevice::read(qint64)' );