Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED]Add custom widget to layout?

[SOLVED]Add custom widget to layout?

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 6.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ealioneE Offline
    ealioneE Offline
    ealione
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • ZlatomirZ Offline
      ZlatomirZ Offline
      Zlatomir
      wrote on last edited by
      #2

      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);
      @

      https://forum.qt.io/category/41/romanian

      1 Reply Last reply
      0
      • P Offline
        P Offline
        poorBob
        wrote on last edited by
        #3

        Hello,

        customWidget should be a pointer.

        Try something like this:

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

        best regards,
        poorBob

        1 Reply Last reply
        0
        • ealioneE Offline
          ealioneE Offline
          ealione
          wrote on last edited by
          #4

          Got it, thanks guys .)

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved