[SOLVED]Add custom widget to layout?



  • I have created a custom widget that is based on QTextEdit

    @class MyWidget : public QTextEdit
    {@

    and now I want to add a method for showing this widget, but trying

    @QWidget *myWidget = new QWidget;
    QGridLayout *myLayout = new QGridLayout;
    MyWidget customWidget(myWidget);
    myLayout->addItem(customWidget); // or addWidget, same result
    myWidget->setLayout(myLayout);
    myWidget->show();@

    results in the error

    @error: no matching member function for call to 'addItem'
    myLayout->addItem(customWidget);
    ~~~~^@

    How can I show my custom QWidget?



  • Create customWidget on heap too (it's the recommended way to create the child widgets):
    @
    QWidget *myWidget = new QWidget;
    QGridLayout *myLayout = new QGridLayout;

    MyWidget* customWidget = new MyWidget(myWidget);

    myLayout->addItem(customWidget);
    @



  • Hello,

    customWidget should be a pointer.

    Try something like this:

    @MyWidget* customWidget = new MyWidget(myWidget);
    myLayout->addWidget( customWidget );
    @

    best regards,
    poorBob



  • Got it, thanks guys .)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.