[Qt-interest] QGridLayout addWidget
Pradeep K V
pradeep.vijayan at gmail.com
Wed Sep 29 16:55:07 CEST 2010
Hi,
This works for me ( Qt 4.6.3 / Windows 7 ):
#include <QApplication>
#include <QGridLayout>
#include <QHash>
#include <QLabel>
class W : public QWidget
{
public:
W( QWidget* parent = NULL )
: QWidget( parent )
{
for( int i = 0; i < 9; ++i )
mQHash.insert( i, new QLabel( "Label:" + QString::number(
i + 1 ), this ) );
QGridLayout* gridLayout = new QGridLayout( this );
int key = 0;
for( int i = 0; i < 3; ++i )
for( int j = 0; j < 3; ++ j )
gridLayout->addWidget( mQHash.value( key++ ), i, j );
}
private:
QHash<int, QWidget*> mQHash;
};
int main( int argc, char* argv[] )
{
QApplication a( argc, argv );
W w;
w.show();
return a.exec();
}
Regards,
Pradeep K Vijayan
On 29 September 2010 12:48, Leonardo M. Ramé <l.rame at griensu.com> wrote:
> Thanks Alex, indeed, I was using a QHash<int, QMyWidget *>.
>
> Why don't you think it's a good idea to make a QHash? I need a
> collection-like object where I can search items by an identifier, and
> QHash seems to be doing the job.
>
> Anyway, the SigSegv is still there :(.
>
> --
> Leonardo M. Ramé
> http://leonardorame.blogspot.com
>
> On 2010-09-27 18:31:28 -0700, Malyushytsky, Alex wrote:
>> Your code look suspicious.
>>
>> First You can't insert pointer (obtained from new QMyWidget() ) into
>> QHash<int, QMyWidget>, second I don't think it is a good idea to make QHash from the QObject.
>>
>> You may try to use QHash<int, QMyWidget*> as a storage.
>> If you already done so find the reason you get SigSegv.
>>
>> Alex
>>
>>
>>
>> -----Original Message-----
>> From: qt-interest-bounces at trolltech.com [mailto:qt-interest-bounces at trolltech.com] On Behalf Of Leonardo M. Ramé
>> Sent: Monday, September 27, 2010 12:12 PM
>> To: Qt Interest
>> Subject: [Qt-interest] QGridLayout addWidget
>>
>> Hi, I'm trying to replace the widget of a "cell" in a QGridLayout,
>> but if I use the code below, I get a SigSegv:
>>
>> myLayout->addWidget(m_widgets.value(0), row, col);
>>
>> m_widgets is a QHash<int, QMyWidget>, where I have inserted one item
>> using insert(0, new QMyWidget()).
>>
>> The layout is a 3x3 grid, so I want to replace the cell (row, col) with
>> a different widget.
>>
>> How can I do this?.
>>
>> --
>> Leonardo M. Ramé
>> http://leonardorame.blogspot.com
>>
>> _______________________________________________
>> Qt-interest mailing list
>> Qt-interest at trolltech.com
>> http://lists.trolltech.com/mailman/listinfo/qt-interest
>>
>>
>> ---------------------------------------------------------------------------------------------------
>> Weidlinger Associates, Inc. made the following annotations.
>>
>> “This message and any attachments are solely for the intended recipient and may contain confidential or privileged information. If you are not the intended recipient, any disclosure, copying, use, or distribution of the information included in this message and any attachments is prohibited. If you have received this communication in error, please notify us by reply e-mail and immediately and permanently delete this message and any attachments. Thank you.”
>>
>> “Please consider our environment before printing this email.”
>>
>> _______________________________________________
>> Qt-interest mailing list
>> Qt-interest at trolltech.com
>> http://lists.trolltech.com/mailman/listinfo/qt-interest
>
>
>
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest
>
More information about the Qt-interest-old
mailing list