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. How to add a custom widget manually(in code) to QMainWindow
Forum Updated to NodeBB v4.3 + New Features

How to add a custom widget manually(in code) to QMainWindow

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 6.6k 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.
  • M Offline
    M Offline
    marty1885
    wrote on last edited by
    #1

    Hi. I have run into a probleam that I can't add my custom widget manually to QMainWindow.
    I originally designed and add it to QMainWindow as this video
    https://www.youtube.com/watch?v=-K-VU9I4ffY

    But I now want to add my widget in code to have more flexibility. But how?
    I tried
    layout()->addWidget(trackEditor);//how I do that in QT4

    But it gave me
    QMainWindowLayout::addItem: Please use the public QMainWindow API instead

    how can I do this in QT5?

    Thanks

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andreyc
      wrote on last edited by
      #2

      Welcome to DevNet!

      Take a look on Qt5 widgets examples.
      @

      MainWindow::MainWindow(...)
      {
      TrackEditor* trackEditor = new TrackEditor;
      QVBoxLayout *layout = new QVBoxLayout;
      layout->addWidget(trackEditor);
      setLayout(layout);
      }
      @

      1 Reply Last reply
      0
      • I Offline
        I Offline
        Ildar
        wrote on last edited by
        #3

        Try like this:
        @
        ui->mainLayout->addWidget(trackEditor);
        @

        Change 'mainLayout' to actually name of your layout.

        1 Reply Last reply
        0
        • M Offline
          M Offline
          marty1885
          wrote on last edited by
          #4

          Thanks, that works. But is there any way to add the widget without using QLayout? I prefer setting the location of my widget using x and y.

          Thanks for helping.

          1 Reply Last reply
          0
          • I Offline
            I Offline
            Ildar
            wrote on last edited by
            #5

            bq. But is there any way to add the widget without using QLayout?

            Yes, just set widget as parent. For example:
            @
            MyWidget *myWidget = new MyWidget(ui->mainWidget)
            @

            But, using layouts is more properly for many reasons.

            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